finapps_core 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +21 -0
- data/.gitignore +53 -0
- data/.hound.yml +2 -0
- data/.rspec +4 -0
- data/.rubocop.yml +271 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +98 -0
- data/LICENSE +21 -0
- data/README.md +21 -0
- data/Rakefile +2 -0
- data/finapps_core.gemspec +39 -0
- data/lib/core_extensions/object/blank.rb +69 -0
- data/lib/core_extensions/object/is_integer.rb +10 -0
- data/lib/core_extensions/string/json_to_hash.rb +10 -0
- data/lib/finapps_core.rb +30 -0
- data/lib/finapps_core/error.rb +17 -0
- data/lib/finapps_core/middleware/middleware.rb +22 -0
- data/lib/finapps_core/middleware/request/accept_json.rb +14 -0
- data/lib/finapps_core/middleware/request/no_encoding_basic_authentication.rb +21 -0
- data/lib/finapps_core/middleware/request/tenant_authentication.rb +20 -0
- data/lib/finapps_core/middleware/request/user_agent.rb +15 -0
- data/lib/finapps_core/middleware/response/custom_logger.rb +39 -0
- data/lib/finapps_core/middleware/response/raise_error.rb +46 -0
- data/lib/finapps_core/rest/base_client.rb +118 -0
- data/lib/finapps_core/rest/configuration.rb +32 -0
- data/lib/finapps_core/rest/connection.rb +35 -0
- data/lib/finapps_core/rest/credentials.rb +21 -0
- data/lib/finapps_core/rest/defaults.rb +19 -0
- data/lib/finapps_core/rest/resources.rb +62 -0
- data/lib/finapps_core/utils/loggeable.rb +14 -0
- data/lib/finapps_core/utils/parameter_filter.rb +32 -0
- data/lib/finapps_core/version.rb +4 -0
- data/lib/tasks/releaser.rake +9 -0
- data/spec/core_extensions/object/blank_spec.rb +44 -0
- data/spec/core_extensions/object/is_integer_spec.rb +17 -0
- data/spec/middleware/request/accept_json_spec.rb +12 -0
- data/spec/middleware/request/no_encoding_basic_authentication_spec.rb +32 -0
- data/spec/middleware/request/tenant_authentication_spec.rb +34 -0
- data/spec/middleware/request/user_agent_spec.rb +12 -0
- data/spec/middleware/response/raise_error_spec.rb +24 -0
- data/spec/rest/base_client_spec.rb +110 -0
- data/spec/rest/configuration_spec.rb +43 -0
- data/spec/rest/credentials_spec.rb +20 -0
- data/spec/rest/relevance_ruleset_names.json +47 -0
- data/spec/rest/timeout_spec.rb +7 -0
- data/spec/spec_helper.rb +37 -0
- data/spec/spec_helpers/client.rb +8 -0
- data/spec/support/fake_api.rb +34 -0
- data/spec/support/fixtures/error.json +5 -0
- data/spec/support/fixtures/relevance_ruleset_names.json +47 -0
- data/spec/support/fixtures/resource.json +3 -0
- data/spec/support/fixtures/resource_not_found.json +5 -0
- data/spec/support/fixtures/resources.json +11 -0
- data/spec/support/fixtures/unauthorized.json +5 -0
- data/spec/utils/parameter_filter_spec.rb +23 -0
- metadata +353 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Financial Apps
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
FinApps Ruby-Client
|
3
|
+
===================
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/finapps/ruby-client-core.svg?branch=master)](https://travis-ci.org/finapps/ruby-client-core)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/finapps/ruby-client-core/badges/gpa.svg)](https://codeclimate.com/github/finapps/ruby-client-core)
|
7
|
+
[![Test Coverage](https://codeclimate.com/github/finapps/ruby-client-core/badges/coverage.svg)](https://codeclimate.com/github/finapps/ruby-client-core/coverage)
|
8
|
+
[![Dependency Status](https://gemnasium.com/badges/github.com/finapps/ruby-client-core.svg)](https://gemnasium.com/github.com/finapps/ruby-client-core)
|
9
|
+
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://finapps.mit-license.org)
|
10
|
+
|
11
|
+
Common library for Ruby clients.
|
12
|
+
|
13
|
+
See Financial Apps API [Ruby Client][ruby-client].
|
14
|
+
|
15
|
+
|
16
|
+
-------------------
|
17
|
+
|
18
|
+
[FinancialApps.com][financialapps]
|
19
|
+
|
20
|
+
[ruby-client]: https://github.com/finapps/ruby-client
|
21
|
+
[financialapps]: https://financialapps.com
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'finapps_core/version'
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'finapps_core'
|
8
|
+
spec.version = FinAppsCore::VERSION
|
9
|
+
spec.platform = Gem::Platform::RUBY
|
10
|
+
spec.authors = ['Erich Quintero']
|
11
|
+
spec.email = ['erich@financialapps.com']
|
12
|
+
|
13
|
+
spec.summary = 'FinApps REST API ruby client - Core.'
|
14
|
+
spec.description = 'A simple library for communicating with the FinApps REST API. Core functionality.'
|
15
|
+
spec.homepage = 'https://github.com/finapps/ruby-client-core'
|
16
|
+
spec.licenses = ['MIT']
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) {|f| File.basename(f) }
|
20
|
+
spec.test_files = Dir['spec/**/*.rb']
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'faraday', '~> 0.9', '>= 0.9.2'
|
24
|
+
spec.add_runtime_dependency 'faraday_middleware', '~> 0.10', '>= 0.10.0'
|
25
|
+
spec.add_runtime_dependency 'typhoeus', '~> 1.0', '>= 1.0.2'
|
26
|
+
spec.add_runtime_dependency 'rash', '~> 0.4', '>= 0.4.0'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.11', '>= 1.11.2'
|
29
|
+
spec.add_development_dependency 'rake', '~> 11.2', '>= 11.2.2'
|
30
|
+
spec.add_development_dependency 'rspec', '~> 3.4', '>= 3.4.0'
|
31
|
+
spec.add_development_dependency 'webmock', '~> 2.1', '>= 2.1.0'
|
32
|
+
spec.add_development_dependency 'sinatra', '~> 1.4', '>= 1.4.7'
|
33
|
+
spec.add_development_dependency 'simplecov', '~> 0.11', '>= 0.11.2'
|
34
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.6.0'
|
35
|
+
spec.add_development_dependency 'rubocop', '0.45.0'
|
36
|
+
|
37
|
+
spec.extra_rdoc_files = %w(README.md LICENSE)
|
38
|
+
spec.rdoc_options = %w(--line-numbers --inline-source --title finapps-ruby-core --main README.md)
|
39
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module ObjectExtensions
|
3
|
+
refine Object do
|
4
|
+
# An object is blank if it's false, empty, or a whitespace string.
|
5
|
+
# For example, +false+, '', ' ', +nil+, [], and {} are all blank.
|
6
|
+
#
|
7
|
+
# This simplifies
|
8
|
+
#
|
9
|
+
# !address || address.empty?
|
10
|
+
#
|
11
|
+
# to
|
12
|
+
#
|
13
|
+
# address.blank?
|
14
|
+
#
|
15
|
+
# @return [true, false]
|
16
|
+
def blank?
|
17
|
+
respond_to?(:empty?) ? !!empty? : !self
|
18
|
+
end
|
19
|
+
|
20
|
+
# An object is present if it's not blank.
|
21
|
+
#
|
22
|
+
# @return [true, false]
|
23
|
+
def present?
|
24
|
+
!blank?
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns the receiver if it's present otherwise returns +nil+.
|
28
|
+
# <tt>object.presence</tt> is equivalent to
|
29
|
+
#
|
30
|
+
# object.present? ? object : nil
|
31
|
+
#
|
32
|
+
# For example, something like
|
33
|
+
#
|
34
|
+
# state = params[:state] if params[:state].present?
|
35
|
+
# country = params[:country] if params[:country].present?
|
36
|
+
# region = state || country || 'US'
|
37
|
+
#
|
38
|
+
# becomes
|
39
|
+
#
|
40
|
+
# region = params[:state].presence || params[:country].presence || 'US'
|
41
|
+
#
|
42
|
+
# @return [Object]
|
43
|
+
def presence
|
44
|
+
self if present?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
module StringExtensions
|
50
|
+
refine String do
|
51
|
+
BLANK_RE = /\A[[:space:]]*\z/
|
52
|
+
|
53
|
+
# A string is blank if it's empty or contains whitespaces only:
|
54
|
+
#
|
55
|
+
# ''.blank? # => true
|
56
|
+
# ' '.blank? # => true
|
57
|
+
# "\t\n\r".blank? # => true
|
58
|
+
# ' blah '.blank? # => false
|
59
|
+
#
|
60
|
+
# Unicode whitespace is supported:
|
61
|
+
#
|
62
|
+
# "\u00a0".blank? # => true
|
63
|
+
#
|
64
|
+
# @return [true, false]
|
65
|
+
def blank?
|
66
|
+
match BLANK_RE
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/finapps_core.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'finapps_core/version' unless defined?(FinAppsCore::VERSION)
|
3
|
+
|
4
|
+
require 'faraday'
|
5
|
+
require 'faraday_middleware'
|
6
|
+
require 'typhoeus'
|
7
|
+
require 'typhoeus/adapters/faraday'
|
8
|
+
|
9
|
+
require 'core_extensions/object/blank'
|
10
|
+
require 'core_extensions/object/is_integer'
|
11
|
+
require 'core_extensions/string/json_to_hash'
|
12
|
+
|
13
|
+
require 'finapps_core/utils/loggeable'
|
14
|
+
require 'finapps_core/utils/parameter_filter'
|
15
|
+
require 'finapps_core/error'
|
16
|
+
|
17
|
+
require 'finapps_core/middleware/request/tenant_authentication'
|
18
|
+
require 'finapps_core/middleware/request/no_encoding_basic_authentication'
|
19
|
+
require 'finapps_core/middleware/request/accept_json'
|
20
|
+
require 'finapps_core/middleware/request/user_agent'
|
21
|
+
require 'finapps_core/middleware/response/raise_error'
|
22
|
+
require 'finapps_core/middleware/middleware'
|
23
|
+
|
24
|
+
require 'finapps_core/rest/defaults'
|
25
|
+
require 'finapps_core/rest/resources'
|
26
|
+
|
27
|
+
require 'finapps_core/rest/configuration'
|
28
|
+
require 'finapps_core/rest/credentials'
|
29
|
+
require 'finapps_core/rest/connection'
|
30
|
+
require 'finapps_core/rest/base_client'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# Defines some errors to identify Exceptions within this gem
|
3
|
+
module FinAppsCore # :nodoc:
|
4
|
+
# Base error class.
|
5
|
+
class Error < StandardError; end
|
6
|
+
# Raised for existing but invalid arguments.
|
7
|
+
class InvalidArgumentsError < Error; end
|
8
|
+
# Raised whenever a required argument is missing.
|
9
|
+
class MissingArgumentsError < Error; end
|
10
|
+
|
11
|
+
# Raised whenever there is a session timeout at the API.
|
12
|
+
class ApiSessionTimeoutError < Error; end
|
13
|
+
|
14
|
+
%i(InvalidArgumentsError MissingArgumentsError ApiSessionTimeoutError).each do |const|
|
15
|
+
Error.const_set(const, FinAppsCore.const_get(const))
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'faraday' unless defined? Faraday
|
3
|
+
|
4
|
+
module FinAppsCore
|
5
|
+
module Middleware
|
6
|
+
autoload :AcceptJson, 'finapps_core/middleware/request/accept_json'
|
7
|
+
autoload :UserAgent, 'finapps_core/middleware/request/user_agent'
|
8
|
+
autoload :NoEncodingBasicAuthentication, 'finapps_core/middleware/request/no_encoding_basic_authentication'
|
9
|
+
autoload :TenantAuthentication, 'finapps_core/middleware/request/tenant_authentication'
|
10
|
+
autoload :CustomLogger, 'finapps_core/middleware/response/custom_logger'
|
11
|
+
|
12
|
+
if Faraday::Middleware.respond_to? :register_middleware
|
13
|
+
Faraday::Request.register_middleware \
|
14
|
+
accept_json: -> { AcceptJson },
|
15
|
+
user_agent: -> { UserAgent },
|
16
|
+
no_encoding_basic_authentication: -> { NoEncodingBasicAuthentication },
|
17
|
+
tenant_authentication: -> { TenantAuthentication }
|
18
|
+
Faraday::Response.register_middleware \
|
19
|
+
custom_logger: -> { CustomLogger }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module FinAppsCore
|
3
|
+
module Middleware
|
4
|
+
# This middleware sets the Accept request-header field to specify JSON as acceptable media type for the response.
|
5
|
+
class AcceptJson < Faraday::Middleware
|
6
|
+
KEY = 'Accept' unless defined? KEY
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
env[:request_headers][KEY] = 'application/json'
|
10
|
+
@app.call(env)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module FinAppsCore
|
3
|
+
module Middleware
|
4
|
+
# Adds a custom header for basic authorization.
|
5
|
+
# If the value for this header already exists, it is not overriden.
|
6
|
+
class NoEncodingBasicAuthentication < Faraday::Middleware
|
7
|
+
KEY = 'Authorization' unless defined? KEY
|
8
|
+
|
9
|
+
def initialize(app, token)
|
10
|
+
super(app)
|
11
|
+
sanitized = token.to_s.strip.delete("\n")
|
12
|
+
@header_value = "Basic #{sanitized}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
env[:request_headers][KEY] ||= @header_value
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module FinAppsCore
|
3
|
+
module Middleware
|
4
|
+
# Adds a custom header for tenant level authorization.
|
5
|
+
# If the value for this header already exists, it is not overriden.
|
6
|
+
class TenantAuthentication < Faraday::Middleware
|
7
|
+
KEY = 'X-FinApps-Token' unless defined? KEY
|
8
|
+
|
9
|
+
def initialize(app, identifier, token)
|
10
|
+
super(app)
|
11
|
+
@header_value = "#{identifier.to_s.strip}=#{token.to_s.strip}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
env[:request_headers][KEY] ||= @header_value
|
16
|
+
@app.call(env)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module FinAppsCore
|
3
|
+
module Middleware
|
4
|
+
# This middleware sets the User-Agent request-header field to identify thei client.
|
5
|
+
class UserAgent < Faraday::Middleware
|
6
|
+
KEY = 'User-Agent' unless defined? KEY
|
7
|
+
RUBY = "#{RUBY_ENGINE}/#{RUBY_PLATFORM} #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
env[:request_headers][KEY] = "finapps-ruby/#{FinAppsCore::VERSION} (#{RUBY})"
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module FinAppsCore
|
3
|
+
module Middleware
|
4
|
+
class CustomLogger < Faraday::Response::Middleware
|
5
|
+
extend Forwardable
|
6
|
+
include FinAppsCore::Utils::ParameterFilter
|
7
|
+
|
8
|
+
DEFAULT_OPTIONS = {bodies: false}.freeze
|
9
|
+
|
10
|
+
def initialize(app, logger=nil, options={})
|
11
|
+
super(app)
|
12
|
+
@logger = logger || begin
|
13
|
+
require 'logger'
|
14
|
+
::Logger.new(STDOUT)
|
15
|
+
end
|
16
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
20
|
+
|
21
|
+
def call(env)
|
22
|
+
debug "#{self.class.name}##{__method__} => URL: #{env.method.upcase} #{env.url}"
|
23
|
+
debug "#{self.class.name}##{__method__} => Request Headers: #{dump env.request_headers}"
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
def on_complete(env)
|
28
|
+
debug "#{self.class.name}##{__method__} => Response Headers: #{dump env.response_headers}"
|
29
|
+
debug "#{self.class.name}##{__method__} => Response Body: #{dump env.body}" if env.body
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def dump(value)
|
35
|
+
skip_sensitive_data(value.is_a?(Array) ? value.to_h : value).to_json
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module FinAppsCore
|
3
|
+
module Middleware
|
4
|
+
class RaiseError < Faraday::Response::Middleware # :nodoc:
|
5
|
+
using ObjectExtensions
|
6
|
+
using StringExtensions
|
7
|
+
|
8
|
+
SUCCESS_STATUSES = 200..299
|
9
|
+
CONNECTION_FAILED_STATUS = 407
|
10
|
+
API_SESSION_TIMEOUT = 419
|
11
|
+
|
12
|
+
def on_complete(env)
|
13
|
+
return if SUCCESS_STATUSES.include?(env[:status])
|
14
|
+
|
15
|
+
if env[:status] == API_SESSION_TIMEOUT
|
16
|
+
raise(FinAppsCore::Error::ApiSessionTimeoutError, 'Api Session Timed out')
|
17
|
+
elsif env[:status] == CONNECTION_FAILED_STATUS
|
18
|
+
raise(Faraday::Error::ConnectionFailed, '407 "Proxy Authentication Required"')
|
19
|
+
else
|
20
|
+
raise(Faraday::Error::ClientError, response_values(env))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def response_values(env)
|
25
|
+
{
|
26
|
+
status: env.status,
|
27
|
+
headers: env.response_headers,
|
28
|
+
body: env.body,
|
29
|
+
error_messages: error_messages(env.body)
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def error_messages(body)
|
36
|
+
return nil if body.blank?
|
37
|
+
body = body.json_to_hash if body.is_a?(String)
|
38
|
+
has_message_key?(body) ? body['messages'] : nil
|
39
|
+
end
|
40
|
+
|
41
|
+
def has_message_key?(body)
|
42
|
+
body.respond_to?(:key?) && body.key?('messages')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module FinAppsCore
|
3
|
+
module REST
|
4
|
+
# base client functionality
|
5
|
+
class BaseClient
|
6
|
+
include ::FinAppsCore::Utils::Loggeable
|
7
|
+
include ::FinAppsCore::REST::Connection
|
8
|
+
using ObjectExtensions
|
9
|
+
using StringExtensions
|
10
|
+
|
11
|
+
attr_reader :config
|
12
|
+
|
13
|
+
def initialize(options, logger=nil)
|
14
|
+
@config = FinAppsCore::REST::Configuration.new options
|
15
|
+
@logger = logger
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns an initialized Faraday connection object.
|
19
|
+
#
|
20
|
+
# @return Faraday::Connection.
|
21
|
+
def connection
|
22
|
+
@connection ||= faraday(config, logger)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Performs HTTP GET, POST, UPDATE and DELETE requests.
|
26
|
+
# You shouldn't need to use this method directly, but it can be useful for debugging.
|
27
|
+
# Returns a hash obtained from parsing the JSON object in the response body.
|
28
|
+
#
|
29
|
+
# @param [String] path
|
30
|
+
# @param [String] method
|
31
|
+
# @param [Hash] params
|
32
|
+
# @return [Hash,Array<String>]
|
33
|
+
def send_request(path, method, params={})
|
34
|
+
raise FinAppsCore::MissingArgumentsError.new 'Missing argument: path.' if path.blank?
|
35
|
+
raise FinAppsCore::MissingArgumentsError.new 'Missing argument: method.' if method.blank?
|
36
|
+
|
37
|
+
response, error_messages = execute_request(path, method, params)
|
38
|
+
result = if empty?(response)
|
39
|
+
nil
|
40
|
+
else
|
41
|
+
block_given? ? yield(response) : response.body
|
42
|
+
end
|
43
|
+
|
44
|
+
[result, error_messages]
|
45
|
+
end
|
46
|
+
|
47
|
+
# Defines methods to perform HTTP GET, POST, PUT and DELETE requests.
|
48
|
+
# Returns a hash obtained from parsing the JSON object in the response body.
|
49
|
+
#
|
50
|
+
def method_missing(method_id, *arguments, &block)
|
51
|
+
if %i(get post put delete).include? method_id
|
52
|
+
connection.send(method_id) do |req|
|
53
|
+
req.url arguments.first
|
54
|
+
req.body = arguments[1] unless method_id == :get
|
55
|
+
end
|
56
|
+
else
|
57
|
+
super
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def respond_to_missing?(method_sym, include_private=false)
|
62
|
+
[:get, :post, :put, :delete].include?(method_sym) ? true : super
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def empty?(response)
|
68
|
+
response.blank? || (response.respond_to?(:body) && response.body.blank?)
|
69
|
+
end
|
70
|
+
|
71
|
+
def execute_request(path, method, params)
|
72
|
+
errors = []
|
73
|
+
|
74
|
+
begin
|
75
|
+
response = execute_method path, method, params
|
76
|
+
|
77
|
+
rescue FinAppsCore::ApiSessionTimeoutError => error
|
78
|
+
handle_error(error)
|
79
|
+
rescue FinAppsCore::InvalidArgumentsError => error
|
80
|
+
handle_error error
|
81
|
+
rescue FinAppsCore::MissingArgumentsError => error
|
82
|
+
handle_error error
|
83
|
+
rescue Faraday::Error::ConnectionFailed => error
|
84
|
+
handle_error error
|
85
|
+
rescue Faraday::Error::ClientError => error
|
86
|
+
errors = handle_client_error(error)
|
87
|
+
end
|
88
|
+
|
89
|
+
[response, errors]
|
90
|
+
end
|
91
|
+
|
92
|
+
def handle_error(error)
|
93
|
+
logger.fatal "#{self.class}##{__method__} => #{error}"
|
94
|
+
raise error
|
95
|
+
end
|
96
|
+
|
97
|
+
def handle_client_error(error)
|
98
|
+
logger.warn "#{self.class}##{__method__} => #{error.class.name}, #{error}"
|
99
|
+
error.response.present? && error.response[:error_messages] ? error.response[:error_messages] : [error.message]
|
100
|
+
end
|
101
|
+
|
102
|
+
def execute_method(path, method, params)
|
103
|
+
case method
|
104
|
+
when :get
|
105
|
+
get(path)
|
106
|
+
when :post
|
107
|
+
post(path, params)
|
108
|
+
when :put
|
109
|
+
put(path, params)
|
110
|
+
when :delete
|
111
|
+
delete(path, params)
|
112
|
+
else
|
113
|
+
raise FinAppsCore::InvalidArgumentsError.new "Method not supported: #{method}."
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|