Class: Zadarma::Client

Inherits:
Object
  • Object
show all
Extended by:
Methods
Includes:
Methods
Defined in:
lib/zadarma/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

balance, callback, direct_numbers, direct_numbers_available, direct_numbers_countries, direct_numbers_country, direct_numbers_order, documents_groups_list, pbx_internal, pbx_record, pbx_statistics, price, redirection, send_sms, set_redirect, set_sip_caller, sip, statistics

Constructor Details

#initialize(api_key, api_secret) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/zadarma/client.rb', line 10

def initialize(api_key, api_secret)
  @api_key = api_key
  @api_secret = api_secret
end

Class Method Details

.request(method, path, params = {}) ⇒ Object



57
58
59
60
61
# File 'lib/zadarma/client.rb', line 57

def request(method, path, params = {})
  # Make instance and execute request
  obj = Zadarma::Client.new(Zadarma.api_key, Zadarma.api_secret)
  obj.request(method, path, params)
end

Instance Method Details

#request(method, path, params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/zadarma/client.rb', line 15

def request(method, path, params = {})
  raise "No auth data provided" unless @api_key && @api_secret

  params.merge!(format: "json")

  response = client.send(method, "/v1" + path, params) do |request|
    request.headers["Accept"] = "application/json"
    request.headers["Authorization"] = "#{@api_key}:#{signature("/v1" + path, params)}"
  end

  result = ActiveSupport::JSON.decode(response.body).with_indifferent_access
  raise Zadarma::Error.new("Error [HTTP #{response.status}]: #{result[:message]} ") unless "success" == result[:status]

  result.except("status")

rescue ActiveSupport::JSON.parse_error
  raise Zadarma::Error.new("Response is not JSON: #{response.body}")

end