Class: Producteev::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/producteev/api.rb

Constant Summary collapse

@@instance =
Api.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



8
9
10
# File 'lib/producteev/api.rb', line 8

def self.instance
  return @@instance
end

Instance Method Details

#api_set(apikey, secret, debug = false) ⇒ Object



12
13
14
15
16
# File 'lib/producteev/api.rb', line 12

def api_set(apikey, secret, debug = false)
  @@apikey = apikey
  @@secret = secret
  @debug = debug
end

#generate_signature(parameters) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/producteev/api.rb', line 20

def generate_signature(parameters)
  signature = ""

  parameters = parameters.sort_by { |name, value| name.to_s }
  #sort the hash alphabetically and make an array

  parameters.each { |key,value|
    if (value.kind_of? String or value.kind_of? Integer)
      signature = "#{signature}#{key}#{value}"
    end
  }

  signature = "#{signature}#{@@secret}"
  signature = Digest::MD5.hexdigest(signature)
  return signature
end

#send_request(path, options = {}, token = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/producteev/api.rb', line 38

def send_request(path, options={},token = nil)
  options.merge!({:api_key => @@apikey})

  if token != nil
    options.merge!({:token => token})
  end
  options.merge!({:api_sig => generate_signature(options)})

  if @debug
    response = self.class.get(path, {:query => options, :debug_output => $stderr})
  else
    response = self.class.get(path, {:query => options})
  end

  if response.success?
    return JSON.parse(response.body)
  else
    raise response.response
  end
end