Class: Finix::Client
- Inherits:
-
Object
- Object
- Finix::Client
- Defined in:
- lib/finix/client.rb
Constant Summary collapse
- DEFAULT_CONFIG =
{ :logging_level => 'WARN', :connection_timeout => 60, :read_timeout => 60, :logger => nil, :ssl_verify => true, :faraday_adapter => Faraday.default_adapter, }
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
Instance Method Summary collapse
- #build_conn ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #method_missing(method, *args, &block) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
23 24 25 26 |
# File 'lib/finix/client.rb', line 23 def initialize(={}) @config = DEFAULT_CONFIG.merge build_conn end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/finix/client.rb', line 59 def method_missing(method, *args, &block) href_or_uri = args[0] args[0] = "#{@config[:root_url]}/#{href_or_uri}" unless href_or_uri =~ /\A#{URI::regexp(%w(http https))}\z/ if is_http_method? method # TODO use auth property conn.basic_auth(@config[:user], @config[:password]) unless @config[:user].nil? and @config[:password].nil? conn.send method, *args do |req| req.headers['Content-Type'] = 'application/json' req.headers['Content-Type'] = 'multipart/form-data' if ((not req.body.nil?) and req.body.key?('file')) end else super method, *args, &block end end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
21 22 23 |
# File 'lib/finix/client.rb', line 21 def config @config end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
20 21 22 |
# File 'lib/finix/client.rb', line 20 def conn @conn end |
Instance Method Details
#build_conn ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/finix/client.rb', line 28 def build_conn if config[:logger] logger = config[:logger] else logger = Logger.new(STDOUT) logger.level = Logger.const_get(config[:logging_level].to_s) end Faraday::Response.register_middleware :handle_api_errors => lambda { Faraday::Response::RaiseApiError } = { :request => { :open_timeout => config[:connection_timeout], :timeout => config[:read_timeout] }, :ssl => { :verify => @config[:ssl_verify] # Only set this to false for testing } } @conn = Faraday.new(@config[:url], ) do |cxn| cxn.request :multipart cxn.request :url_encoded cxn.request :json cxn.response :logger, logger cxn.response :handle_api_errors cxn.response :json cxn.adapter config[:faraday_adapter] end conn.headers['User-Agent'] = "finix-ruby/#{Finix::VERSION}" end |