lib/sourcescrub/utils/request.rb in sourcescrub-0.1.1 vs lib/sourcescrub/utils/request.rb in sourcescrub-0.1.2

- old
+ new

@@ -1,8 +1,9 @@ # frozen_string_literal: true require 'faraday' +require 'logger' module Sourcescrub # Utils module Utils # Key Reminder @@ -17,37 +18,32 @@ # Limit of requests per second: 30 # # def get(uri, *args) - response = Faraday.new( - url: API_URI, - headers: headers, - request: { - timeout: 10, - open_timeout: 5 - }, - params: args[0] || {} - ).get(uri) + response = Faraday.new(request_options(args)) do |faraday| + faraday.headers['Content-Type'] = 'application/json-patch+json' + faraday.adapter Faraday.default_adapter + faraday.response :logger, ::Logger.new(STDOUT), bodies: true if debug_mode? + end.get(uri) - response_body = response.body - raise Error, response_body unless response.status == 200 + raise Error, response.body unless response.status == 200 - response_body = JSON.parse(response_body) - # Processing different cases for investments - if response_body.is_a?(Array) - response_body = if response_body.empty? - {} - else - { - 'total' => response_body.size, - 'items' => response_body - } - end - end + parse_api_response(response.body).merge('headers' => response.headers) + end - response_body.merge('headers' => response.headers) + # Search endpoints + def search(uri, args) + response = Faraday.new(request_options(args)) do |faraday| + faraday.headers['Content-Type'] = 'application/json-patch+json' + faraday.adapter Faraday.default_adapter + faraday.response :logger, ::Logger.new(STDOUT), bodies: true if debug_mode? + end.post(uri, args.to_json) + + raise Error, response.body unless response.status == 200 + + parse_api_response(response.body).merge('headers' => response.headers) end # def put(uri, args) # response = Faraday.new(url: API_URI) do |faraday| # faraday.headers = headers @@ -100,9 +96,34 @@ @token = JSON.parse(response.body)['access_token'] end private + + def request_options(args) + { + url: API_URI, + headers: headers, + request: { + timeout: 10, + open_timeout: 5 + }, + params: args[0] || {} + } + end + + def parse_api_response(response_body) + response_body = JSON.parse(response_body) + + # Processing different cases for investments + return response_body unless response_body.is_a?(Array) + return {} if response_body.empty? + + { + 'total' => response_body.size, + 'items' => response_body + } + end def debug_mode? Sourcescrub.account.debug || false end end