Class: Biffbot::Base
- Inherits:
-
Hash
- Object
- Hash
- Biffbot::Base
- Includes:
- Hashie::Extensions::Coercion
- Defined in:
- lib/biffbot/base.rb
Instance Method Summary collapse
-
#generate_url(url, token, type, version) ⇒ String
generate an url consisting of your api key and the endpoint you’d like to use.
-
#parse(token = Biffbot.token, type = 'article', url = '', options = {}) ⇒ Hash
a new instance of Biffbot::Base.
-
#parse_options(options = {}, request = '') ⇒ String
add the options hash to your input url.
Instance Method Details
#generate_url(url, token, type, version) ⇒ String
generate an url consisting of your api key and the endpoint you’d like to use
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/biffbot/base.rb', line 28 def generate_url url, token, type, version case type when 'analyze' url = "http://api.diffbot.com/v3/#{type}?token=#{token}&url=#{url}" when 'custom' url = "http://api.diffbot.com/v3/#{[:api_name]}?token=#{token}&url=#{url}" when 'article', 'image', 'product' url = "http://api.diffbot.com/v2/#{type}?token=#{token}&url=#{url}" url = "http://api.diffbot.com/#{version}/#{type}?token=#{token}&url=#{url}" if version == 'v2' || version == 'v3' end url end |
#parse(token = Biffbot.token, type = 'article', url = '', options = {}) ⇒ Hash
a new instance of Biffbot::Base
15 16 17 18 19 20 |
# File 'lib/biffbot/base.rb', line 15 def parse token = Biffbot.token, type = 'article', url = '', = {} url = (, generate_url(CGI.escape(url), token, type, [:version])) JSON.parse(HTTParty.get(url).body).each_pair do |key, value| self[key] = value end end |
#parse_options(options = {}, request = '') ⇒ String
add the options hash to your input url
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/biffbot/base.rb', line 46 def = {}, request = '' .each do |opt, value| case opt when :timeout, :paging, :mode request += "&#{opt}=#{value}" when :callback, :stats request += "&#{opt}" when :fields request += "&#{opt}=" + value.join(',') if value.is_a?(Array) end end request end |