Class: XeroMin::Client
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
-
#body_proc ⇒ Object
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.
Class Method Summary collapse
-
.diagnose(response) ⇒ Object
try to doctorify failing response.
-
.parse(response) ⇒ Object
Public: returns response body if Content-Type is application/pdf or a nokogiri node.
Instance Method Summary collapse
-
#initialize(consumer_key = nil, secret_key = nil, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#parse!(response) ⇒ Object
Public : parse response or die if response fails.
-
#private!(private_key_file = 'keys/xero.rsa') ⇒ Object
Public : enables client to act as a private application resets previous access token if any.
-
#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.
-
#request!(sym_or_url, options = {}, &block) ⇒ Object
Public : creates and runs a request and parse! its body.
-
#run(request = nil) ⇒ Object
Public : runs a request.
-
#token? ⇒ Boolean
Public returns whether it has already requested an access token.
Methods included from Urls
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.merge() @consumer_key, @secret_key = consumer_key , secret_key self.body_proc = lambda{|body| EscapeUtils.escape_url(body)} end |
Instance Attribute Details
#body_proc ⇒ Object
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
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, ={}, &block) url = (string_or_url_for.is_a?(String) ? string_or_url_for : url_for(string_or_url_for)) [:body] = body_proc.call([:body]) if ([:body] and body_proc) accept_option = .delete(:accept) if accept_option [:headers] ||= {} [:headers].merge! 'Accept' => accept_option end req = Typhoeus::Request.new(url, @options.merge()) # 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, ={}, &block) parse!(request(sym_or_url, , &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
40 41 42 |
# File 'lib/xero-min/client.rb', line 40 def token? !!@token end |