Anonymous (owner)

Revisions

  • 3c6161 Tue Aug 26 10:16:05 -0700 2008
gist: 7300 Download_button fork
public
Public Clone URL: git://gist.github.com/7300.git
Text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module OEmbed
  class RisingCodeCache
    @cache_enabled = false
    class << self
      attr_writer :expiry, :host
      def enabled?
        @cache_enabled
      end
      def enable!
        @cache ||= MemCache.new(host, :namespace => "openuri")
        @cache_enabled = true
      end
      def disable!
        @cache_enabled = false
      end
      def disabled?
        !@cache_enabled
      end
      def get(key)
        @cache.get(key)
      end
      def set(key, value)
        @cache.set(key, value, expiry)
      end
      def expiry
        @expiry ||= 60 * 60 * 24
      end
      def alive?
        servers = @cache.instance_variable_get(:@servers) and servers.collect{|s| s.alive?}.include?(true)
      end
      def host
        @host ||= "localhost:11211"
      end
    end
  end
  class Provider
    attr_accessor :format, :name, :url, :urls, :endpoint
 
    def initialize(endpoint, format = :json)
      @endpoint = endpoint
      @urls = []
      @format = format
    end
 
    def <<(url)
      full, scheme, domain, path = *url.match(%r{([^:]*)://?([^/?]*)(.*)})
      domain = Regexp.escape(domain).gsub("\\*", "(.*?)").gsub("(.*?)\\.", "([^\\.]+\\.)?")
      path = Regexp.escape(path).gsub("\\*", "(.*?)")
      @urls << Regexp.new("^#{Regexp.escape(scheme)}://#{domain}#{path}")
    end
 
    def build(url, options = {})
      raise OEmbed::NotFound, url unless include?(url)
      query = options.merge({:url => url})
      endpoint = @endpoint.clone
 
      if format_in_url?
        format = endpoint["{format}"] = (query[:format] || @format).to_s
        query.delete(:format)
      else
        format = query[:format] ||= @format
      end
 
      query_string = "?" + query.inject("") do |memo, (key, value)|
        "#{key}=#{value}&#{memo}"
      end.chop
 
      URI.parse(endpoint + query_string).instance_eval do
        @format = format; def format; @format; end
        self
      end
    end
 
    def raw(url, options = {})
      uri = build(url, options)
      if RisingCodeCache.enabled? and RisingCodeCache::alive?
        begin
          response = RisingCodeCache::get(url)
        rescue
          response = nil
        end
      end
      unless response
        res = Net::HTTP.start(uri.host, uri.port) do |http|
          http.get(uri.request_uri)
        end
        case res
        when Net::HTTPNotImplemented
          response = Exception.new
        when Net::HTTPNotFound
          response = Exception.new
        when Net::HTTPOK
          response = res.body
        else
          response = Exception.new
        end
        RisingCodeCache::set(url, response) if RisingCodeCache.alive?
      end
      raise response if response.is_a? Exception
      StringIO.new(response)
    end
 
    def get(url, options = {})
      OEmbed::Response.create_for(raw(url, options.merge(:format => :json)), self)
    end
 
    def format_in_url?
      @endpoint.include?("{format}")
    end
 
    def include?(url)
      @urls.empty? || !!@urls.detect{ |u| u =~ url }
    end
  end
end