Class: BillForward::Client

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options={})
  TypeCheck.verifyObj(Hash, options, 'options')
  @use_logging = options[:use_logging]
  @logger = options[:logger] || Logger.new(STDOUT)

  if options[:host]
    @host = options[: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 options[:use_proxy]
    @use_proxy = options[:use_proxy]
    @proxy_url = options[:proxy_url]
  end

  if options[:api_token]
    @api_token = options[:api_token]
  else
    @api_token = nil
    if options[:client_id] and options[:client_secret] and options[:username] and options[:password]
      @client_id = options[:client_id]
      @client_secret = options[:client_secret]
      @username = options[:username]
      @password = options[: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

  @authorization = nil
end

Class Attribute Details

.all_verbsObject

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_clientObject

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_tokenObject

Returns the value of attribute api_token.



49
50
51
# File 'lib/bill_forward/client.rb', line 49

def api_token
  @api_token
end

#hostObject

Returns the value of attribute host.



48
49
50
# File 'lib/bill_forward/client.rb', line 48

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



51
52
53
# File 'lib/bill_forward/client.rb', line 51

def logger
  @logger
end

#use_loggingObject

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.

Parameters:

  • options={}
    Hash

    Options with which to construct client

Returns:

  • (Client)

    The constructed client



83
84
85
86
# File 'lib/bill_forward/client.rb', line 83

def self.make_default_client(options)
  constructedClient = self.new(options)
  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)
  options = {
    :Authorization => "Bearer #{token}",
    :accept => 'application/json'
  }

  haspayload = @@payload_verbs.include?(verb)

  if (haspayload)
    options.update(:content_type => 'application/json')
  end

  args = [url, options]
  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