Class: Novofon::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

balance, callback, checknumber, direct_numbers, direct_numbers_available, direct_numbers_countries, direct_numbers_country, direct_numbers_order, pbx_internal, pbx_record, pbx_record_request, 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.



12
13
14
15
# File 'lib/novofon/client.rb', line 12

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

Class Method Details

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



55
56
57
58
59
# File 'lib/novofon/client.rb', line 55

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

Instance Method Details

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



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

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 Novofon::Error, "Error [HTTP #{response.status}]: #{result[:message]} " unless result[:status] == "success"

  result&.except("status")
rescue ActiveSupport::JSON.parse_error
  raise Novofon::Error, "Response is not JSON: #{response.body}"
end