Class: XeroMin::Client

Inherits:
Object
  • Object
show all
Includes:
Urls
Defined in:
lib/xero-min/client.rb

Constant Summary collapse

@@signature =
{
  signature_method: 'HMAC-SHA1'
}
@@options =
{
  site: 'https://api.xero.com/api.xro/2.0',
  request_token_path: "/oauth/RequestToken",
  access_token_path: "/oauth/AccessToken",
  authorize_path: "/oauth/Authorize",
  headers: {
    'Accept' => 'text/xml',
    'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'
  }
}.merge(@@signature)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Urls

#url_for

Constructor Details

#initialize(consumer_key = nil, secret_key = nil, options = {}) ⇒ Client

Returns a new instance of Client.



33
34
35
36
37
# File 'lib/xero-min/client.rb', line 33

def initialize(consumer_key=nil, secret_key=nil, options={})
  @options = @@options.merge(options)
  @consumer_key, @secret_key  = consumer_key , secret_key
  self.body_proc = lambda{|body| EscapeUtils.escape_url(body)}
end

Instance Attribute Details

#body_procObject

Public : body is transformed using body_proc if present proc has one param defaults to ‘lambda{|body| EscapeUtils.escape_url(body)}`, that is url encode body



31
32
33
# File 'lib/xero-min/client.rb', line 31

def body_proc
  @body_proc
end

Class Method Details

.diagnose(response) ⇒ Object

try to doctorify failing response



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/xero-min/client.rb', line 98

def self.diagnose(response)
  diagnosis = case response.code
  when 400
    Nokogiri::XML(response.body).xpath('//Message').to_a.map{|e| e.content}.uniq.join("\n")
  when 401
    EscapeUtils.unescape_url(response.body).gsub('&', "\n")
  else
    response.body
  end
  "code=#{response.code}\n#{diagnosis}\nbody=\n#{response.body}"
end

.parse(response) ⇒ Object

Public: returns response body if Content-Type is application/pdf or a nokogiri node



86
87
88
89
90
91
92
93
94
95
# File 'lib/xero-min/client.rb', line 86

def self.parse(response)
  case content_type = response.headers_hash['Content-Type']
  when 'application/pdf'
    response.body
  when %r(^text/xml)
    Nokogiri::XML(response.body)
  else
    raise Problem, "Unsupported Content-Type : #{content_type}"
  end
end

Instance Method Details

#parse!(response) ⇒ Object

Public : parse response or die if response fails



111
112
113
# File 'lib/xero-min/client.rb', line 111

def parse!(response)
  response.success?? Client.parse(response) : raise(Problem, Client.diagnose(response))
end

#private!(private_key_file = 'keys/xero.rsa') ⇒ Object

Public : enables client to act as a private application resets previous access token if any



46
47
48
49
50
# File 'lib/xero-min/client.rb', line 46

def private!(private_key_file='keys/xero.rsa')
  @token = nil if token?
  @options.merge!({signature_method: 'RSA-SHA1', private_key_file: private_key_file})
  self
end

#request(string_or_url_for, options = {}) {|req| ... } ⇒ Object

Public : creates a signed request url of request is XeroMin::Urls.url_for sym_or_url, when string_or_url_for is not a String available options are the one of a Typhoeus::Request request is yielded to block if present first request ask for access token

Yields:

  • (req)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/xero-min/client.rb', line 57

def request(string_or_url_for, options={}, &block)
  url = (string_or_url_for.is_a?(String) ? string_or_url_for : url_for(string_or_url_for))
  options[:body] = body_proc.call(options[:body]) if (options[:body] and body_proc)
  accept_option = options.delete(:accept)
  if accept_option
    options[:headers] ||= {}
    options[:headers].merge! 'Accept' => accept_option
  end
  req = Typhoeus::Request.new(url, @options.merge(options))

  # sign request with oauth
  helper = OAuth::Client::Helper.new(req, @options.merge(consumer: token.consumer, token: token, request_uri: url))
  req.headers.merge!({'Authorization' => helper.header})
  yield req if block_given?
  req
end

#request!(sym_or_url, options = {}, &block) ⇒ Object

Public : creates and runs a request and parse! its body



81
82
83
# File 'lib/xero-min/client.rb', line 81

def request!(sym_or_url, options={}, &block)
  parse!(request(sym_or_url, options, &block).tap{|r| run(r)}.response)
end

#run(request = nil) ⇒ Object

Public : runs a request



75
76
77
78
# File 'lib/xero-min/client.rb', line 75

def run(request=nil)
  queue(request) if request
  hydra.run
end

#token?Boolean

Public returns whether it has already requested an access token

Returns:

  • (Boolean)


40
41
42
# File 'lib/xero-min/client.rb', line 40

def token?
  !!@token
end