judofyr / ruby-oembed

oEmbed for Ruby

This URL has Read+Write access

ruby-oembed / lib / oembed / response.rb
100644 59 lines (48 sloc) 1.315 kb
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
module OEmbed
  class Response
    METHODS = [:define_methods!, :provider, :field, :fields]
    attr_reader :fields, :provider, :format
    
    def self.create_for(raw, provider, format = :json)
      fields = OEmbed::Formatters.convert(format, raw)
      
      resp_type = case fields['type']
        when 'photo' then OEmbed::Response::Photo
        when 'video' then OEmbed::Response::Video
        when 'link' then OEmbed::Response::Link
        when 'rich' then OEmbed::Response::Rich
        else self
      end
      
      resp_type.new(fields, provider)
    end
    
    def initialize(fields, provider)
      @fields = fields
      @provider = provider
      define_methods!
    end
    
    def field(m)
      @fields[m.to_s]
    end
    
    def video?
      is_a?(OEmbed::Response::Video)
    end
    
    def photo?
      is_a?(OEmbed::Response::Photo)
    end
    
    def link?
      is_a?(OEmbed::Response::Link)
    end
    
    def rich?
      is_a?(OEmbed::Response::Rich)
    end
    
    private
    
    def define_methods!
      @fields.keys.each do |key|
        next if METHODS.include?(key.to_sym) || key[0,2]=="__" || key[-1]==??
        class << self
          self
        end.send(:define_method, key) do
          @fields[key]
        end
      end
    end
  end
end