Class: Biffbot::Base

Inherits:
Hash
  • Object
show all
Includes:
Hashie::Extensions::Coercion
Defined in:
lib/biffbot/base.rb

Direct Known Subclasses

Analyze, Article, Bulk, Custom, Image, Product

Instance Method Summary collapse

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

Parameters:

  • url (String)

    The url to pass to diffbot

  • token (String)

    Diffbot API token

  • type (String)

    API to use

Returns:

  • (String)

    a formatted url with your api key, endpoint and input url



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/#{options[: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

Parameters:

  • token (String) (defaults to: Biffbot.token)

    Override Biffbot.token with another token

  • type (String) (defaults to: 'article')

    pass the class type you’re working with

  • url (String) (defaults to: '')

    The url of the page you’re working with

  • options (Hash) (defaults to: {})

    An hash of options

Returns:

  • (Hash)


15
16
17
18
19
20
# File 'lib/biffbot/base.rb', line 15

def parse token = Biffbot.token, type = 'article', url = '', options = {}
  url = parse_options(options, generate_url(CGI.escape(url), token, type, options[: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

Parameters:

  • options (Hash) (defaults to: {})

    An hash of options

  • request (String) (defaults to: '')

    The url to append options to

Returns:

  • (String)

    a formatted url with options merged into the input url



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/biffbot/base.rb', line 46

def parse_options options = {}, request = ''
  options.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