Class: InvisibleHand::API

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/invisiblehand/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger

Constructor Details

#initialize(conf = nil) ⇒ API

When initializing a new instance of ‘InvisibleHand::API`, you can specify configuration in one of three ways:

require 'invisiblehand'

# This looks first for an environment variable called
# "INVISIBLEHAND_CONFIG", which should contain a file path to a config
# YAML file. Failing that, "./invisiblehand.yml" is used, looking for
# the config YAML file in the current directory.
api = InvisibleHand::API.new

# This one takes a string argument which should be a valid file path to
# the YAML config file.
api = InvisibleHand::API.new "path/to/invisiblehand.yml"

# Or you can do a literal hash config. This requires no YAML config
# file.
api = InvisibleHand::API.new :api_key => "...", :app_id => "..."

Examples of the configuration variables you can pass in can be found in the “invisiblehand.sample.yml” file in this gem’s GitHub repository.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/invisiblehand/api.rb', line 27

def initialize conf = nil
  if conf.is_a? Hash
    @config = conf
  elsif conf.is_a? String
    @config = YAML.load_file(conf)
  else
    conf ||= ENV['INVISIBLEHAND_CONFIG'] || './invisiblehand.yml'
    @config = YAML.load_file(conf)
  end

  # The @config[:development] flag exists to bypass the app_id and app_key
  # check in this gem (not on the server) for internal testing reasons.
  if invalid_config? and !@config[:development]
    message = "Your config does not contain an app_id and app_key. " +
      "Both are required to make API calls."

    raise Error::InvalidConfig.new message, @config
  end

  @config[:protocol] = @config[:use_ssl] == false ? "http://" : "https://"
  @config[:version] ||= "1"
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/invisiblehand/api.rb', line 4

def config
  @config
end

Instance Method Details

#api_call(method, path, opts = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/invisiblehand/api.rb', line 92

def api_call method, path, opts = {}
  if !@config[:development]
    opts.merge!({
      :app_id => @config[:app_id],
      :app_key => @config[:app_key],
    })
  end

  query = url_params_from opts
  url   = "#{@config[:protocol]}#{endpoint}/v#{@config[:version]}#{path}?#{query}"

  if opts[:debug]
    debug { api_raw_request method, url }
  else
    api_raw_request method, url
  end
end

#live_price(url, opts = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/invisiblehand/api.rb', line 80

def live_price url, opts = {}
  if url =~ /http:\/\/api\.invisiblehand/
    url += url_params_from opts
    json = api_raw_request :get, url
    json["price"]
  else
    opts[:url] = url
    json = api_call :get, "/pages/live_price", opts
    json["price"]
  end
end

#page(url, opts = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/invisiblehand/api.rb', line 70

def page url, opts = {}
  response = api_call :get, "/pages/?url=#{CGI.escape(url)}", opts

  if opts[:raw]
    response
  else
    Page.new(response, self)
  end
end

#product(id, opts = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/invisiblehand/api.rb', line 60

def product id, opts = {}
  response = api_call :get, "/products/#{CGI.escape(id)}", opts

  if opts[:raw]
    response
  else
    Product.new(response, self)
  end
end

#products(opts = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/invisiblehand/api.rb', line 50

def products opts = {}
  response = api_call :get, "/products", opts

  if opts[:raw]
    response
  else
    Response.new(response, self)
  end
end