class Verifalia::REST::AccountBalance

Public Class Methods

new(config, account_sid, account_token, args = {}) click to toggle source

The Verifalia::REST::AccountBalance class allow you to comminucate with Account balance Api. You don't need to instantiate this class, but use the client for autoconfiguration. # The args parameter is a hash of configuration

   # File lib/rest/account_balance.rb
10 def initialize(config, account_sid, account_token, args = {})
11   @resources = build_resources(config, account_sid, account_token)
12 end

Public Instance Methods

balance() click to toggle source

Query the Account balance

   # File lib/rest/account_balance.rb
17 def balance()
18   begin
19     response = multiplex_request do |resource|
20       resource[@unique_id].get
21     end
22     @error = nil
23     JSON.parse(response)
24   rescue => e
25     compute_error(e)
26     false
27   end
28 end
error() click to toggle source
   # File lib/rest/account_balance.rb
30 def error
31   @error
32 end

Private Instance Methods

build_resources(config, account_sid, account_token) click to toggle source
   # File lib/rest/account_balance.rb
77 def build_resources(config, account_sid, account_token)
78   opts = {
79     user: account_sid,
80     password: account_token,
81     headers: {
82       content_type: :json,
83       user_agent: "verifalia-rest-client/ruby/#{Verifalia::VERSION}"
84     }
85   }
86   config[:hosts].map do |host|
87     api_url = "#{host}/#{config[:api_version]}/account-balance"
88     RestClient::Resource.new api_url, opts
89   end
90 end
compute_error(e) click to toggle source
   # File lib/rest/account_balance.rb
50 def compute_error(e)
51   unless e.is_a? RestClient::Exception
52     @error = :internal_server_error
53   end
54 
55   case e.http_code
56     when 400
57       @error = :bad_request
58     when 401
59       @error = :unauthorized
60     when 402
61       @error = :payment_required
62     when 403
63       @error = :forbidden
64     when 404
65       @error = :not_found
66     when 406
67       @error = :not_acceptable
68     when 410
69       @error = :gone
70     when 429
71       @error = :too_many_request
72     else
73       @error = :internal_server_error
74     end
75 end
multiplex_request() { |resource| ... } click to toggle source
   # File lib/rest/account_balance.rb
36 def multiplex_request
37   @resources.shuffle.each do |resource|
38     begin
39       response = yield(resource)
40       return response
41     rescue => e
42       if ((e.is_a? RestClient::Exception) && (e.http_code != 500))
43         raise e
44       end
45     end
46   end
47   raise RestClient::Exception.new(nil, 500)
48 end