Class: Iframely::Requester

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

Constant Summary collapse

CACHE_KEY_PREFIX =
'iframely:'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, cache: nil, cache_options: {}, iframely_url: IFRAMELY_API_URL, oembed_url: OEMBED_API_URL) ⇒ Requester



9
10
11
12
13
14
15
16
17
18
# File 'lib/iframely/requester.rb', line 9

def initialize api_key: nil, cache: nil, cache_options: {}, iframely_url: IFRAMELY_API_URL, oembed_url: OEMBED_API_URL
  @iframely_url  = iframely_url or raise "No iframely_url specified"
  @oembed_url    = oembed_url or raise "No oembed_url specified"
  @api_key       = api_key      or raise "No api_key specified"
  @cache         = cache
  if cache
    raise "cache must be a ActiveSupport::Cache::Store" unless defined?(ActiveSupport::Cache::Store) && cache.is_a?(ActiveSupport::Cache::Store)
  end
  @cache_options = cache_options
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/iframely/requester.rb', line 7

def api_key
  @api_key
end

#cacheObject

Returns the value of attribute cache.



7
8
9
# File 'lib/iframely/requester.rb', line 7

def cache
  @cache
end

#cache_optionsObject

Returns the value of attribute cache_options.



7
8
9
# File 'lib/iframely/requester.rb', line 7

def cache_options
  @cache_options
end

#iframely_urlObject

Returns the value of attribute iframely_url.



7
8
9
# File 'lib/iframely/requester.rb', line 7

def iframely_url
  @iframely_url
end

#oembed_urlObject

Returns the value of attribute oembed_url.



7
8
9
# File 'lib/iframely/requester.rb', line 7

def oembed_url
  @oembed_url
end

Instance Method Details

#get_iframely_json(embed_url) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/iframely/requester.rb', line 20

def get_iframely_json embed_url
  fetch cache_key(:iframely, embed_url) do
    response = iframely_connection.get do |req|
      req.params['api_key'] = api_key
      req.params['url']     = embed_url
    end

    JSON.parse response.body
  end
end

#get_iframely_model(embed_url) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/iframely/requester.rb', line 31

def get_iframely_model embed_url
  json = get_iframely_json embed_url
  if json.has_key? 'error'
    raise Iframely::Error, json['error']
  else
    Iframely::Model.build json
  end
end

#get_oembed_json(embed_url) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/iframely/requester.rb', line 40

def get_oembed_json embed_url
  fetch cache_key(:oembed, embed_url) do
    response = oembed_connection.get do |req|
      req.params['api_key'] = api_key
      req.params['url']     = embed_url
    end

    JSON.parse response.body
  end
end