class V2Intuity::Clients::Client
Attributes
accept[RW]
base_uri[RW]
content_type[RW]
httparty_client[RW]
org_id[RW]
token[RW]
Public Class Methods
new(attributes = {})
click to toggle source
# 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
Public Instance Methods
build_and_send_request(endpoint, options = {})
click to toggle source
# File lib/v2_intuity/clients/client.rb, line 29 def build_and_send_request(endpoint, options = {}) @request = build_request(endpoint, options) send_request_generate_response(@request) end
build_request(endpoint, options = {})
click to toggle source
# File lib/v2_intuity/clients/client.rb, line 34 def build_request(endpoint, options = {}) http_party_options = {} @query_filters = {} @headers = {} method = options[:method].to_s.nil? ? V2Intuity::Request::METHODS[:get] : options[:method] id = options[:id].to_s.nil? ? nil : options[:id] query_filters, headers = combine_query_filters_and_headers(options) http_party_options[:query_filters] = query_filters http_party_options[:headers] = headers http_party_options[:body] = options[: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: http_party_options) end
send_request(request)
click to toggle source
# 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.options[:query_filters], headers: request.options[: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.options[:query_filters], body: request.options[:body], headers: request.options[:headers]) end end
send_request_generate_response(request)
click to toggle source
# 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.message)) response end
Private Instance Methods
build_error(response)
click to toggle source
# File lib/v2_intuity/clients/client.rb, line 76 def build_error(response) json = response.g_json if json V2Intuity::RequestError.new(code: response.code, message: response.message, description: json['message']) else V2Intuity::RequestError.new(code: response.code, message: response.message) end end
combine_query_filters_and_headers(options)
click to toggle source
# File lib/v2_intuity/clients/client.rb, line 93 def combine_query_filters_and_headers(options) total_query_filters = {} total_headers = {} filter_array = options[:query_filters].is_a?(Array) ? options[:query_filters] : [] header_array = options[:headers].is_a?(Array) ? options[:headers] : [] unique_query_filters = (default_query_filters + filter_array) unique_query_filters.each do |filter| total_query_filters.merge!(filter.to_hash) end unique_headers = (default_headers + header_array) unique_headers.each do |header| total_headers.merge!(header.to_hash) end [total_query_filters, total_headers] end
default_headers()
click to toggle source
# File lib/v2_intuity/clients/client.rb, line 89 def default_headers [V2Intuity::Headers::Accept.new(value: @accept), V2Intuity::Headers::ContentType.new(value: @content_type)] end
default_query_filters()
click to toggle source
# File lib/v2_intuity/clients/client.rb, line 85 def default_query_filters [V2Intuity::QueryFilters::AccessToken.new(value: @token)] end