Module: QboApi::Connection
- Included in:
- QboApi
- Defined in:
- lib/qbo_api/connection.rb
Instance Method Summary collapse
- #authorized_json_connection(url, headers: nil) ⇒ Object
- #authorized_multipart_connection(url) ⇒ Object
- #build_connection(url, headers: nil) ⇒ Object
- #disconnect ⇒ Object
- #parse_response_body(resp) ⇒ Object
- #raw_request(method, conn:, path:, payload: nil, params: nil) ⇒ Object
- #reconnect ⇒ Object
- #request(method, path:, entity: nil, payload: nil, params: nil) ⇒ Object
- #response(resp, entity: nil) ⇒ Object
Instance Method Details
#authorized_json_connection(url, headers: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/qbo_api/connection.rb', line 8 def (url, headers: nil) headers ||= {} headers['Accept'] ||= 'application/json;charset=UTF-8' # required "we'll only accept JSON". Can be changed to any `+json` media type. headers['Content-Type'] ||= 'application/json' # required when request has a body, else harmless build_connection(url, headers: headers) do |conn| (conn) add_exception_middleware(conn) conn.request :url_encoded add_connection_adapter(conn) end end |
#authorized_multipart_connection(url) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/qbo_api/connection.rb', line 20 def (url) headers = { 'Content-Type' => 'multipart/form-data' } build_connection(url, headers: headers) do |conn| (conn) add_exception_middleware(conn) conn.request :multipart add_connection_adapter(conn) end end |
#build_connection(url, headers: nil) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/qbo_api/connection.rb', line 41 def build_connection(url, headers: nil) Faraday.new(url: url) { |conn| conn.response :detailed_logger, QboApi.logger, LOG_TAG if QboApi.log conn.headers.update(headers) if headers yield conn if block_given? } end |
#disconnect ⇒ Object
86 87 88 89 |
# File 'lib/qbo_api/connection.rb', line 86 def disconnect path = "#{APP_CONNECTION_URL}/disconnect" request(:get, path: path) end |
#parse_response_body(resp) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/qbo_api/connection.rb', line 76 def parse_response_body(resp) body = resp.body case resp.headers['Content-Type'] when /json/ then JSON.parse(body) else body end end |
#raw_request(method, conn:, path:, payload: nil, params: nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/qbo_api/connection.rb', line 54 def raw_request(method, conn:, path:, payload: nil, params: nil) path = finalize_path(path, method: method, params: params) conn.public_send(method) do |req| case method when :get, :delete req.url path when :post, :put req.url path req.body = payload.to_json else raise ArgumentError, "Unhandled request method '#{method.inspect}'" end end end |
#reconnect ⇒ Object
93 94 95 96 |
# File 'lib/qbo_api/connection.rb', line 93 def reconnect path = "#{APP_CONNECTION_URL}/reconnect" request(:get, path: path) end |
#request(method, path:, entity: nil, payload: nil, params: nil) ⇒ Object
49 50 51 52 |
# File 'lib/qbo_api/connection.rb', line 49 def request(method, path:, entity: nil, payload: nil, params: nil) raw_response = raw_request(method, conn: connection, path: path, params: params, payload: payload) response(raw_response, entity: entity) end |
#response(resp, entity: nil) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/qbo_api/connection.rb', line 68 def response(resp, entity: nil) data = parse_response_body(resp) entity ? entity_response(data, entity) : data rescue => e QboApi.logger.debug { "#{LOG_TAG} response parsing error: entity=#{entity.inspect} body=#{resp.body.inspect} exception=#{e.inspect}" } data end |