Class: V2Intuity::Clients::Client
- Inherits:
-
Object
- Object
- V2Intuity::Clients::Client
- Defined in:
- lib/v2_intuity/clients/client.rb
Instance Attribute Summary collapse
-
#accept ⇒ Object
Returns the value of attribute accept.
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#httparty_client ⇒ Object
Returns the value of attribute httparty_client.
-
#org_id ⇒ Object
Returns the value of attribute org_id.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #build_and_send_request(endpoint, options = {}) ⇒ Object
- #build_request(endpoint, options = {}) ⇒ Object
-
#initialize(attributes = {}) ⇒ Client
constructor
A new instance of Client.
- #send_request(request) ⇒ Object
- #send_request_generate_response(request) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 |
# File 'lib/v2_intuity/clients/client.rb', line 9 def initialize(attributes = {}) @token = attributes[:token] raise 'Client requires token' unless @token @base_uri = attributes.fetch(:base_uri, nil) @accept = attributes.fetch(:accept, 'application/json') @content_type = attributes.fetch(:content_type, 'application/json') end |
Instance Attribute Details
#accept ⇒ Object
Returns the value of attribute accept.
5 6 7 |
# File 'lib/v2_intuity/clients/client.rb', line 5 def accept @accept end |
#base_uri ⇒ Object
Returns the value of attribute base_uri.
5 6 7 |
# File 'lib/v2_intuity/clients/client.rb', line 5 def base_uri @base_uri end |
#content_type ⇒ Object
Returns the value of attribute content_type.
5 6 7 |
# File 'lib/v2_intuity/clients/client.rb', line 5 def content_type @content_type end |
#httparty_client ⇒ Object
Returns the value of attribute httparty_client.
5 6 7 |
# File 'lib/v2_intuity/clients/client.rb', line 5 def httparty_client @httparty_client end |
#org_id ⇒ Object
Returns the value of attribute org_id.
5 6 7 |
# File 'lib/v2_intuity/clients/client.rb', line 5 def org_id @org_id end |
#token ⇒ Object
Returns the value of attribute token.
5 6 7 |
# File 'lib/v2_intuity/clients/client.rb', line 5 def token @token end |
Instance Method Details
#build_and_send_request(endpoint, options = {}) ⇒ Object
29 30 31 32 |
# File 'lib/v2_intuity/clients/client.rb', line 29 def build_and_send_request(endpoint, = {}) @request = build_request(endpoint, ) send_request_generate_response(@request) end |
#build_request(endpoint, options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/v2_intuity/clients/client.rb', line 34 def build_request(endpoint, = {}) = {} @query_filters = {} @headers = {} method = [:method].to_s.nil? ? V2Intuity::Request::METHODS[:get] : [:method] id = [:id].to_s.nil? ? nil : [:id] query_filters, headers = combine_query_filters_and_headers() [:query_filters] = query_filters [:headers] = headers [:body] = [:body] unless method == V2Intuity::Request::METHODS[:get] uri = endpoint.uri(org_id: org_id, id: id, base_uri: base_uri) V2Intuity::Request.new(method: method, uri: uri, options: ) end |
#send_request(request) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/v2_intuity/clients/client.rb', line 18 def send_request(request) case request.method when V2Intuity::Request::METHODS[:get], V2Intuity::Request::METHODS[:delete] @httparty_client.send(request.method.to_sym, request.uri, query: request.[:query_filters], headers: request.[:headers]) # puts re.request.last_uri.to_s when V2Intuity::Request::METHODS[:post], V2Intuity::Request::METHODS[:put] @httparty_client.send(request.method.to_sym, request.uri, query: request.[:query_filters], body: request.[:body], headers: request.[:headers]) end end |
#send_request_generate_response(request) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/v2_intuity/clients/client.rb', line 53 def send_request_generate_response(request) response = V2Intuity::Response.new(request: request) self.httparty_client ||= HTTParty response.define_response_and_json(send_request(request)) return response if response.response.success? error = build_error(response) response.add_errors(error) response rescue StandardError => e response.add_errors(RequestError.new(code: 400, message: e.)) response end |