Class: Oembedder::Provider

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Provider

Returns a new instance of Provider.



5
6
7
8
# File 'lib/oembedder/provider.rb', line 5

def initialize(params = {})
  params[:format_type] ||= "url" # url or querystring
  @format_type = params[:format_type]
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



3
4
5
# File 'lib/oembedder/provider.rb', line 3

def endpoint
  @endpoint
end

#format_typeObject (readonly)

Returns the value of attribute format_type.



3
4
5
# File 'lib/oembedder/provider.rb', line 3

def format_type
  @format_type
end

#schemesObject (readonly)

Returns the value of attribute schemes.



3
4
5
# File 'lib/oembedder/provider.rb', line 3

def schemes
  @schemes
end

Instance Method Details

#get(request) ⇒ Object

get content



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/oembedder/provider.rb', line 30

def get(request)
  conn = Faraday.new(:url => "#{self.endpoint.scheme}://#{self.endpoint.host}") do |faraday|
    faraday.request :url_encoded
    # faraday.response :logger
    faraday.response :json, :content_type => /\bjson$/
    faraday.response :xml,  :content_type => /\bxml$/
    faraday.adapter  Faraday.default_adapter
  end

  params = request.urlparams
  params += "&format=#{request.format}" if self.format_type == "querystring"

  response = conn.get(create_request_path(request) + params)

  ProviderResponse.create(request.format, response)
end

#match_scheme?(target) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
# File 'lib/oembedder/provider.rb', line 18

def match_scheme?(target)
  target.gsub!(/\/$/, '') # delete trailing slash
  mc = self.schemes.count do |scheme|
    pattern = Regexp.escape(scheme).gsub('\\*', '[^/]+')
    match = target.match Regexp.new(pattern)

    target.size == match.to_s.size unless match.nil?
  end
  true if mc >= 1
end

#set_endpoint(endpoint) ⇒ Object



10
11
12
# File 'lib/oembedder/provider.rb', line 10

def set_endpoint(endpoint)
  @endpoint = URI.parse(endpoint)
end

#set_schemes(schemes) ⇒ Object



14
15
16
# File 'lib/oembedder/provider.rb', line 14

def set_schemes(schemes)
  @schemes = schemes
end