Class: Kickboxer::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/kickboxer/request.rb

Constant Summary collapse

BASE_URL =
"https://api.fullcontact.com/v2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action) ⇒ Request

Returns a new instance of Request.



11
12
13
# File 'lib/kickboxer/request.rb', line 11

def initialize(action)
  @action = action
end

Class Method Details

.run(action, options = {}) ⇒ Object



40
41
42
# File 'lib/kickboxer/request.rb', line 40

def self.run(action, options={})
  new(action).run(options)
end

Instance Method Details

#build_url(options = {}) ⇒ Object

Raises:



15
16
17
18
19
20
21
# File 'lib/kickboxer/request.rb', line 15

def build_url(options={})
  options[:apiKey] = Kickboxer.api_key
  raise NoApiKeyError unless options[:apiKey]

  querystring = options.map{|k,v| "#{ k }=#{ URI.escape v }" } * '&'
  "#{ BASE_URL }/#{ @action }.json?#{ querystring }"
end

#run(options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kickboxer/request.rb', line 23

def run(options={})
  debug = options.delete(:debug) || Kickboxer.debug

  response = open build_url(options)
  body = response.read

  if debug
    filename = debug.is_a?(String) ? debug : @action.gsub('/', '-')
    File.open("#{ filename }.json","w"){|out| out.puts body }
  end

  data = JSON.parse(body)

  code, text = response.status
  Response.new data.update(status_code: code.to_i, status_text: text)
end