Class: FreshBooks::OAuthClient

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/freshbooks.rb

Overview

OAuth 1.0 client. access token and secret must be obtained elsewhere. cf. the oauth gem

Instance Method Summary collapse

Methods included from Client

#api_url, build_xml, #method_missing, new, #post, post, xml_body

Constructor Details

#initialize(domain, consumer_key, consumer_secret, token, token_secret) ⇒ OAuthClient

Returns a new instance of OAuthClient.



182
183
184
185
186
187
188
# File 'lib/freshbooks.rb', line 182

def initialize(domain, consumer_key, consumer_secret, token, token_secret)
  @domain          = domain
  @consumer_key    = consumer_key
  @consumer_secret = consumer_secret
  @token           = token
  @token_secret    = token_secret
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FreshBooks::Client

Instance Method Details

#authObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/freshbooks.rb', line 190

def auth
  data = {
    :realm                  => '',
    :oauth_version          => '1.0',
    :oauth_consumer_key     => @consumer_key,
    :oauth_token            => @token,
    :oauth_timestamp        => timestamp,
    :oauth_nonce            => nonce,
    :oauth_signature_method => 'PLAINTEXT',
    :oauth_signature        => signature,
  }.map { |k,v| %Q[#{k}="#{v}"] }.join(',')

  { 'Authorization' => "OAuth #{data}" }
end

#nonceObject



209
210
211
# File 'lib/freshbooks.rb', line 209

def nonce
  [OpenSSL::Random.random_bytes(10)].pack('m').gsub(/\W/, '')
end

#signatureObject



205
206
207
# File 'lib/freshbooks.rb', line 205

def signature
  CGI.escape("#{@consumer_secret}&#{@token_secret}")
end

#timestampObject



213
214
215
# File 'lib/freshbooks.rb', line 213

def timestamp
  Time.now.to_i
end