Class: OembedProxy::FirstParty

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

Overview

Handles all sites with officially supported oEmbed providers

Direct Known Subclasses

Embedly

Constant Summary collapse

USER_AGENT =
'Ruby oEmbed Proxy'

Instance Method Summary collapse

Constructor Details

#initializeFirstParty

Returns a new instance of FirstParty.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/oembed_proxy/first_party.rb', line 16

def initialize
  # Import the expected first party providers.
  @pattern_hash = {}

  yaml_hash = YAML.load_file(File.expand_path('../providers/first_party.yml', __dir__))
  yaml_hash.each_value do |hsh|
    hsh['pattern_list'].each do |pattern|
      regex = Utility.clean_pattern(pattern)
      @pattern_hash[regex] = hsh['endpoint']
    end
  end
end

Instance Method Details

#get_data(url, other_params = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/oembed_proxy/first_party.rb', line 33

def get_data(url, other_params = {})
  endpoint = find_endpoint(url)
  return nil if endpoint.nil?

  uri = URI(endpoint)
  new_params = { url: url, format: 'json' }
  new_params.merge! other_params
  # Merge in existing params.
  new_params.merge! CGI.parse(uri.query) if uri.query
  uri.query = URI.encode_www_form(new_params)

  fetch(uri)
end

#handles_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/oembed_proxy/first_party.rb', line 29

def handles_url?(url)
  !find_endpoint(url).nil?
end