Class: OembedClient
- Inherits:
-
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
84
85
86
87
88
89
90
|
# File 'lib/oembed_client.rb', line 84
def method_missing(name, *args, &block)
if json.keys.include?(name.to_s)
json[name.to_s]
else
super
end
end
|
Instance Attribute Details
#url ⇒ Object
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_html ⇒ Object
64
65
66
|
# File 'lib/oembed_client.rb', line 64
def embed_html
json['html']
end
|
#embed_url ⇒ Object
68
69
70
|
# File 'lib/oembed_client.rb', line 68
def embed_url
json['url']
end
|
#fetch ⇒ Object
50
51
52
|
# File 'lib/oembed_client.rb', line 50
def fetch
open(omembed_url)
end
|
#json ⇒ Object
58
59
60
61
62
|
# File 'lib/oembed_client.rb', line 58
def json
@json ||= MultiJson.decode(response)
rescue
@json = {}
end
|
#omembed_url ⇒ Object
42
43
44
|
# File 'lib/oembed_client.rb', line 42
def omembed_url
self.class.base_url+'?'+params
end
|
#params ⇒ Object
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
|
#response ⇒ Object
54
55
56
|
# File 'lib/oembed_client.rb', line 54
def response
@response ||= fetch.read
end
|