Class: BillForward::Client
- Inherits:
-
Object
- Object
- BillForward::Client
- Defined in:
- lib/bill_forward/client.rb
Constant Summary collapse
- @@payload_verbs =
['post', 'put']
- @@no_payload_verbs =
['get', 'delete']
- @@all_verbs =
@@payload_verbs + @@no_payload_verbs
Class Attribute Summary collapse
-
.all_verbs ⇒ Object
Returns the value of attribute all_verbs.
-
.default_client ⇒ Object
default client is a singleton client.
Instance Attribute Summary collapse
-
#api_token ⇒ Object
Returns the value of attribute api_token.
-
#host ⇒ Object
Returns the value of attribute host.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#use_logging ⇒ Object
Returns the value of attribute use_logging.
Class Method Summary collapse
-
.make_default_client(options) ⇒ Client
Constructs a client, and sets it to be used as the default client.
Instance Method Summary collapse
- #execute_request(verb, url, token, payload = nil) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/bill_forward/client.rb', line 88 def initialize(={}) TypeCheck.verifyObj(Hash, , 'options') @use_logging = [:use_logging] @logger = [:logger] || Logger.new(STDOUT) if [:host] @host = [:host] else raise ClientInstantiationException.new "Failed to initialize BillForward API Client\n" + "Required parameters: :host and either [:api_token] or all of [:client_id, :client_secret, :username, :password].\n" + "Supplied Parameters: #{options}" end if [:use_proxy] @use_proxy = [:use_proxy] @proxy_url = [:proxy_url] end if [:api_token] @api_token = [:api_token] else @api_token = nil if [:client_id] and [:client_secret] and [:username] and [:password] @client_id = [:client_id] @client_secret = [:client_secret] @username = [:username] @password = [:password] else raise ClientException.new "Failed to initialize BillForward API Client\n"+ "Required parameters: :host and either [:api_token] or all of [:client_id, :client_secret, :username, :password].\n" + "Supplied Parameters: #{options}" end end = nil end |
Class Attribute Details
.all_verbs ⇒ Object
Returns the value of attribute all_verbs.
55 56 57 |
# File 'lib/bill_forward/client.rb', line 55 def all_verbs @all_verbs end |
.default_client ⇒ Object
default client is a singleton client
58 59 60 |
# File 'lib/bill_forward/client.rb', line 58 def default_client @default_client end |
Instance Attribute Details
#api_token ⇒ Object
Returns the value of attribute api_token.
49 50 51 |
# File 'lib/bill_forward/client.rb', line 49 def api_token @api_token end |
#host ⇒ Object
Returns the value of attribute host.
48 49 50 |
# File 'lib/bill_forward/client.rb', line 48 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
51 52 53 |
# File 'lib/bill_forward/client.rb', line 51 def logger @logger end |
#use_logging ⇒ Object
Returns the value of attribute use_logging.
50 51 52 |
# File 'lib/bill_forward/client.rb', line 50 def use_logging @use_logging end |
Class Method Details
.make_default_client(options) ⇒ Client
Constructs a client, and sets it to be used as the default client.
83 84 85 86 |
# File 'lib/bill_forward/client.rb', line 83 def self.make_default_client() constructedClient = self.new() self.default_client = constructedClient end |
Instance Method Details
#execute_request(verb, url, token, payload = nil) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/bill_forward/client.rb', line 125 def execute_request(verb, url, token, payload=nil) # Enable Fiddler: if @use_proxy RestClient.proxy = @proxy_url end # content_type seems to be broken on generic execute. # darn. # RestClient::Request.execute(options) = { :Authorization => "Bearer #{token}", :accept => 'application/json' } haspayload = @@payload_verbs.include?(verb) if (haspayload) .update(:content_type => 'application/json') end args = [url, ] args.insert(1, payload) if haspayload log "#{verb.upcase} #{url}" log "headers: #{JSON.pretty_generate(options)}" log "payload: #{payload}" if haspayload RestClient.send(verb.intern, *args) end |