Sha256: 79a997341dc091417192d70b925094df90e9223d872653d603d86c6827eba10d

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

module OEmbed
  class Response
    METHODS = [:define_methods!, :provider, :field, :fields]
    attr_reader :fields, :provider, :format, :url

    def self.create_for(raw, provider, url, 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, url)
    end

    def initialize(fields, provider, url = nil)
      @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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-oembed-0.7.6 lib/oembed/response.rb
ruby-oembed-0.7.5 lib/oembed/response.rb