Class: OembedClient

Inherits:
Object
  • Object
show all
Defined in:
lib/oembed_client.rb

Constant Summary collapse

STANDARD_RESPONSE_PARAMS =
%w(type version title author_name author_url provider_name
provider_url cache_age thumbnail_url thumbnail_width thumbnail_height
width height)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, embed_options = {}) ⇒ OembedClient

Returns a new instance of OembedClient.



37
38
39
40
# File 'lib/oembed_client.rb', line 37

def initialize(url, embed_options = {})
  @url = url
  @embed_options = embed_options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/oembed_client.rb', line 82

def method_missing(name, *args, &block)
  if json.keys.include?(name.to_s)
    json[name.to_s]
  else
    super
  end
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/oembed_client.rb', line 6

def url
  @url
end

Class Method Details

.base_url(url = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/oembed_client.rb', line 20

def base_url(url = nil)
  if url.nil?
    @base_url
  else
    @base_url = url
  end
end

.create_client_class_for(url) ⇒ Object



13
14
15
16
17
18
# File 'lib/oembed_client.rb', line 13

def create_client_class_for(url)
  client_name = URI.parse(url).host.split('.')[-2].capitalize
  klass = const_set(client_name, Class.new(self))
  klass.base_url(url)
  klass
end

.default_options(options = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/oembed_client.rb', line 28

def default_options(options = nil)
  if options.nil?
    @default_options ||= {}
  else
    @default_options = options
  end
end

Instance Method Details

#embed_htmlObject



62
63
64
# File 'lib/oembed_client.rb', line 62

def embed_html
  json['html']
end

#embed_urlObject



66
67
68
# File 'lib/oembed_client.rb', line 66

def embed_url
  json['html']
end

#fetchObject



50
51
52
# File 'lib/oembed_client.rb', line 50

def fetch
  open(omembed_url)
end

#jsonObject



58
59
60
# File 'lib/oembed_client.rb', line 58

def json
  @json ||= MultiJson.decode(response)
end

#omembed_urlObject



42
43
44
# File 'lib/oembed_client.rb', line 42

def omembed_url
  self.class.base_url+'?'+params
end

#paramsObject



46
47
48
# File 'lib/oembed_client.rb', line 46

def params
  querify_params(self.class.default_options.merge(@embed_options).merge(:url => @url, :format => 'json'))
end

#responseObject



54
55
56
# File 'lib/oembed_client.rb', line 54

def response
  @response ||= fetch.read
end