lib/zoho_hub/connection.rb in zoho_hub-0.1.56 vs lib/zoho_hub/connection.rb in zoho_hub-0.2.0

- old
+ new

@@ -1,8 +1,9 @@ # frozen_string_literal: true require 'faraday' +require 'faraday_middleware' require 'rainbow' require 'addressable' require 'zoho_hub/response' @@ -19,47 +20,41 @@ BASE_PATH = '/crm/v2/' def initialize(access_token:, api_domain: DEFAULT_DOMAIN, expires_in: 3600, refresh_token: nil) @access_token = access_token @expires_in = expires_in - @api_domain = api_domain || DEFAULT_DOMAIN + @api_domain = api_domain @refresh_token ||= refresh_token # do not overwrite if it's already set end - def get(path, params = {}, &block) + def get(path, params = {}) log "GET #{path} with #{params}" - response = with_refresh do - adapter(&block).get(path, params) - end + response = with_refresh { adapter.get(path, params) } response.body end - def post(path, params = {}, &block) + def post(path, params = {}) log "POST #{path} with #{params}" - response = with_refresh do - adapter(&block).post(path, params) - end + response = with_refresh { adapter.post(path, params) } response.body end - def put(path, params = {}, &block) + def put(path, params = {}) log "PUT #{path} with #{params}" - response = with_refresh do - adapter(&block).put(path, params) - end + response = with_refresh { adapter.put(path, params) } response.body end def access_token? - @access_token.present? + @access_token end def refresh_token? - @refresh_token.present? + @refresh_token end def log(text) return unless ZohoHub.configuration.debug? @@ -76,11 +71,11 @@ # Try to refresh the token and try again if response.invalid_token? && refresh_token? log "Refreshing outdated token... #{@access_token}" params = ZohoHub::Auth.refresh_token(@refresh_token) - @on_refresh_cb.call(params) if @on_refresh_cb.present? + @on_refresh_cb.call(params) if @on_refresh_cb @access_token = params[:access_token] http_response = yield elsif response.authentication_failure? @@ -100,10 +95,10 @@ end def adapter Faraday.new(url: base_url) do |conn| conn.headers = authorization_header if access_token? - yield conn if block_given? + conn.use FaradayMiddleware::ParseJson conn.response :json, parser_options: { symbolize_names: true } conn.response :logger if ZohoHub.configuration.debug? conn.adapter Faraday.default_adapter end end