module ServiceApi::BaseFaraday
BaseFaraday
is module for Faraday api which should be included in own base Api class.
class MyApi include BaseApi private def base_url 'http://example.com' end end
Public Class Methods
new(config)
click to toggle source
Initilizer require config hash as parameter
MyApi.new(config)
# File lib/service_api/base_faraday.rb, line 24 def initialize(config) @config = config @params = {} end
Protected Instance Methods
normalize_options(mapper, *options)
click to toggle source
Mapping arguments passing to method, convert normal arguments to hash, and mapping hash keys
# File lib/service_api/base_faraday.rb, line 70 def normalize_options(mapper, *options) NormalizeOptions.new(mapper, *options).call end
params(options)
click to toggle source
Pass parameters to request
params(sample: true, api_key: 'abcd')
# File lib/service_api/base_faraday.rb, line 55 def params(options) @params = options self end
path(path)
click to toggle source
Pass path to request
path('/test/request')
# File lib/service_api/base_faraday.rb, line 46 def path(path) uri_tokens(path) self end
url()
click to toggle source
Last method called in chain which return full url request
path('/test/request').params(sample: true, api_key: 'abcd').url
# File lib/service_api/base_faraday.rb, line 64 def url "#{base_url}#{uri}#{query_params_formatted}" end
Private Instance Methods
base_url()
click to toggle source
# File lib/service_api/base_faraday.rb, line 118 def base_url raise NotImplementedError end
connection()
click to toggle source
# File lib/service_api/base_faraday.rb, line 76 def connection @connection ||= Faraday.new(url: base_url) do |builder| builder.response :xml, content_type: /\bxml$/ builder.response :json, content_type: /\bjson$/ if @config[:adapter] == :test builder.adapter @config[:adapter], @config[:adapter_options] else builder.adapter @config[:adapter] || :net_http end end end
query_params()
click to toggle source
# File lib/service_api/base_faraday.rb, line 105 def query_params @params.reject{ |request_params| token?(request_params) } end
query_params_formatted()
click to toggle source
# File lib/service_api/base_faraday.rb, line 109 def query_params_formatted params = query_params params.empty? ? '' : "?#{QueryParams.encode(params)}" end
token?(value)
click to toggle source
# File lib/service_api/base_faraday.rb, line 89 def token?(value) uri_tokens.token_values.include?(value.to_s) end
uri()
click to toggle source
# File lib/service_api/base_faraday.rb, line 101 def uri uri_tokens.uri_template.expand(@params) end
uri_kind()
click to toggle source
# File lib/service_api/base_faraday.rb, line 114 def uri_kind :default end
uri_tokens(uri = nil)
click to toggle source
# File lib/service_api/base_faraday.rb, line 93 def uri_tokens(uri = nil) if uri @uri_tokens = ServiceApi::UriTokens.new(uri_kind, uri) else @uri_tokens end end