finapps 0.22.4.pre → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,16 +5,20 @@ module FinApps
5
5
 
6
6
  # @return [Array<Hash>, Array<String>]
7
7
  def list
8
+ logger.debug "##{__method__.to_s} => Started"
9
+
8
10
  end_point = Defaults::END_POINTS[:categories_list]
9
11
  logger.debug "##{__method__.to_s} => end_point: #{end_point}"
10
12
 
11
13
  path = end_point
12
14
  logger.debug "##{__method__.to_s} => path: #{path}"
13
15
 
14
- categories, error_messages = @client.send(path, :get)
16
+ categories, error_messages = @client.send_request(path, :get)
17
+
18
+ logger.debug "##{__method__.to_s} => Completed"
15
19
  return categories, error_messages
16
20
  end
17
21
 
18
22
  end
19
23
  end
20
- end
24
+ end
@@ -5,33 +5,80 @@ module FinApps
5
5
  include FinApps::Logging
6
6
  include FinApps::REST::Connection
7
7
 
8
- attr_reader :connection, :users, :institutions, :user_institutions,
9
- :transactions, :categories,
10
- :budget_models, :budget_calculation, :budgets, :cashflows,
11
- :alert, :alert_definition, :alert_settings, :alert_preferences,
12
- :rule_sets
13
-
14
8
  # @param [String] company_identifier
15
9
  # @param [String] company_token
16
10
  # @param [Hash] options
17
11
  # @return [FinApps::REST::Client]
18
12
  def initialize(company_identifier, company_token, options = {})
13
+ logger.debug "##{__method__.to_s} => Started"
14
+
19
15
  @config = DEFAULTS.merge! options
20
- if @config[:logger_tag].present?
21
- Logging.tag= @config[:logger_tag]
22
- logger.info "##{__method__.to_s} => Custom tag for logs: #{@config[:logger_tag]}"
23
- end
16
+ logger_config @config
24
17
 
25
- set_up_logger_level @config[:log_level]
26
- logger.info "##{__method__.to_s} => Current logger level: #{SEVERITY_LABEL[logger.level]}"
18
+ @company_credentials = {:company_identifier => company_identifier, :company_token => company_token}
19
+ @company_credentials.validate_required_strings!
27
20
 
28
- @company_credentials = {:company_identifier => company_identifier,
29
- :company_token => company_token}
30
- @connection = set_up_connection(@company_credentials, @config)
31
- logger.debug "##{__method__.to_s} => Connection initialized"
21
+ logger.debug "##{__method__.to_s} => Completed"
22
+ end
23
+
24
+ def connection
25
+ @connection ||= set_up_connection(@company_credentials, @config)
26
+ end
27
+
28
+ def users
29
+ @users ||= FinApps::REST::Users.new self
30
+ end
31
+
32
+ def institutions
33
+ @institutions ||= FinApps::REST::Institutions.new self
34
+ end
35
+
36
+ def user_institutions
37
+ @user_institutions ||= FinApps::REST::UserInstitutions.new self
38
+ end
39
+
40
+ def transactions
41
+ @transactions ||= FinApps::REST::Transactions.new self
42
+ end
32
43
 
33
- set_up_resources
34
- logger.debug "##{__method__.to_s} => All resources initialized"
44
+ def categories
45
+ @categories ||= FinApps::REST::Categories.new self
46
+ end
47
+
48
+ def budget_models
49
+ @budget_models ||= FinApps::REST::BudgetModels.new self
50
+ end
51
+
52
+ def budget_calculation
53
+ @budget_calculation ||= FinApps::REST::BudgetCalculation.new self
54
+ end
55
+
56
+ def budgets
57
+ @budgets ||= FinApps::REST::Budgets.new self
58
+ end
59
+
60
+ def cashflows
61
+ @cashflows ||= FinApps::REST::Cashflows.new self
62
+ end
63
+
64
+ def alert
65
+ @alert ||= FinApps::REST::Alert.new self
66
+ end
67
+
68
+ def alert_definition
69
+ @alert_definition ||= FinApps::REST::AlertDefinition.new self
70
+ end
71
+
72
+ def alert_settings
73
+ @alert_settings ||= FinApps::REST::AlertSettings.new self
74
+ end
75
+
76
+ def alert_preferences
77
+ @alert_preferences ||= FinApps::REST::AlertPreferences.new self
78
+ end
79
+
80
+ def rule_sets
81
+ @rule_sets ||= FinApps::REST::Relevance::Rulesets.new self
35
82
  end
36
83
 
37
84
  # Performs HTTP GET, POST, UPDATE and DELETE requests.
@@ -42,12 +89,15 @@ module FinApps
42
89
  # @param [String] method
43
90
  # @param [Proc] proc
44
91
  # @return [Hash,Array<String>]
45
- def send(path, method, params = {}, &proc)
46
- raise MissingArgumentsError.new 'Missing argument: method.' if method.blank?
47
- result, error_messages = nil, nil
92
+ def send_request(path, method, params = {}, &proc)
93
+ logger.debug "##{__method__.to_s} => Started"
48
94
 
49
- begin
95
+ raise FinApps::REST::MissingArgumentsError.new 'Missing argument: path.' if path.blank?
96
+ raise FinApps::REST::MissingArgumentsError.new 'Missing argument: method.' if method.blank?
50
97
 
98
+ result, error_messages = nil, []
99
+
100
+ begin
51
101
  case method
52
102
  when :get
53
103
  response = get(path)
@@ -58,7 +108,7 @@ module FinApps
58
108
  when :delete
59
109
  response = delete(path, params)
60
110
  else
61
- raise StandardError "Method not supported: #{method}."
111
+ raise FinApps::REST::InvalidArgumentsError.new "Method not supported: #{method}."
62
112
  end
63
113
 
64
114
  if response.present?
@@ -67,33 +117,38 @@ module FinApps
67
117
  logger.error "##{__method__.to_s} => Null response found. Unable to process it."
68
118
  end
69
119
 
120
+ rescue FinApps::REST::InvalidArgumentsError => error
121
+ raise error
70
122
  rescue FinApps::REST::Error => error
71
123
  error_messages = error.error_messages
72
124
  rescue Faraday::ParsingError => error
73
- error_messages = []
74
125
  error_messages << 'Unable to parse the server response.'
75
126
  logger.error "##{__method__.to_s} => Faraday::ParsingError, #{error.to_s}"
76
127
  rescue Exception => error
77
- error_messages = []
78
128
  error_messages << 'Unexpected error.'
79
129
  logger.fatal "##{__method__.to_s} => Exception, #{error.to_s}"
80
130
  logger.fatal error
81
131
  ensure
82
132
  logger.debug "##{__method__.to_s} => Failed, error_messages: #{error_messages.pretty_inspect}" if error_messages.present?
83
133
  end
134
+
135
+ logger.debug "##{__method__.to_s} => Completed"
84
136
  return result, error_messages
85
137
  end
86
138
 
87
139
  # @param [String] user_identifier
88
140
  # @param [String] user_token
89
141
  def user_credentials!(user_identifier, user_token)
142
+ logger.debug "##{__method__.to_s} => Started"
143
+
90
144
  {:user_identifier => user_identifier, :user_token => user_token}.validate_required_strings!
91
145
  logger.debug "##{__method__.to_s} => Credentials passed validation. Attempting to set user credentials on current connection."
92
146
 
93
-
94
147
  @config[:user_identifier] = user_identifier
95
148
  @config[:user_token] = user_token
96
149
  @connection = set_up_connection(@company_credentials, @config)
150
+
151
+ logger.debug "##{__method__.to_s} => Completed"
97
152
  end
98
153
 
99
154
  private
@@ -105,10 +160,16 @@ module FinApps
105
160
  # @param [String] path
106
161
  # @return [Hash,Array<String>]
107
162
  def get(path)
163
+ logger.debug "##{__method__.to_s} => Started"
108
164
  raise MissingArgumentsError.new 'Missing argument: path.' if path.blank?
109
165
 
110
166
  logger.debug "##{__method__.to_s} => GET path:#{path}"
111
- @connection.get { |req| req.url path }
167
+ response = @connection.get do |req|
168
+ req.url path
169
+ end
170
+
171
+ logger.debug "##{__method__.to_s} => Completed"
172
+ response
112
173
  end
113
174
 
114
175
  # Performs an HTTP POST request.
@@ -119,13 +180,17 @@ module FinApps
119
180
  # @param [Hash] params
120
181
  # @return [Hash,Array<String>]
121
182
  def post(path, params = {})
183
+ logger.debug "##{__method__.to_s} => Started"
122
184
  raise MissingArgumentsError.new 'Missing argument: path.' if path.blank?
123
185
 
124
186
  logger.debug "##{__method__.to_s} => POST path:#{path} params:#{skip_sensitive_data params }"
125
- @connection.post do |req|
187
+ response = @connection.post do |req|
126
188
  req.url path
127
189
  req.body = params
128
190
  end
191
+
192
+ logger.debug "##{__method__.to_s} => Completed"
193
+ response
129
194
  end
130
195
 
131
196
  # Performs an HTTP PUT request.
@@ -136,13 +201,17 @@ module FinApps
136
201
  # @param [Hash] params
137
202
  # @return [Hash,Array<String>]
138
203
  def put(path, params = {})
204
+ logger.debug "##{__method__.to_s} => Started"
139
205
  raise MissingArgumentsError.new 'Missing argument: path.' if path.blank?
140
206
 
141
207
  logger.debug "##{__method__.to_s} => PUT path:#{path} params:#{skip_sensitive_data(params)}"
142
- @connection.put do |req|
208
+ response = @connection.put do |req|
143
209
  req.url path
144
210
  req.body = params
145
211
  end
212
+
213
+ logger.debug "##{__method__.to_s} => Completed"
214
+ response
146
215
  end
147
216
 
148
217
  # Performs an HTTP DELETE request.
@@ -153,32 +222,17 @@ module FinApps
153
222
  # @param [Hash] params
154
223
  # @return [Hash,Array<String>]
155
224
  def delete(path, params = {})
225
+ logger.debug "##{__method__.to_s} => Started"
156
226
  raise MissingArgumentsError.new 'Missing argument: path.' if path.blank?
157
227
 
158
228
  logger.debug "##{__method__.to_s} => DELETE path:#{path} params:#{skip_sensitive_data(params)}"
159
- @connection.delete do |req|
229
+ response = @connection.delete do |req|
160
230
  req.url path
161
231
  req.body = params
162
232
  end
163
- end
164
233
 
165
- # Initialize resources.
166
- #
167
- def set_up_resources
168
- @users ||= FinApps::REST::Users.new self
169
- @institutions ||= FinApps::REST::Institutions.new self
170
- @user_institutions ||= FinApps::REST::UserInstitutions.new self
171
- @transactions ||= FinApps::REST::Transactions.new self
172
- @categories ||= FinApps::REST::Categories.new self
173
- @budget_models ||= FinApps::REST::BudgetModels.new self
174
- @budget_calculation ||= FinApps::REST::BudgetCalculation.new self
175
- @budgets ||= FinApps::REST::Budgets.new self
176
- @cashflows ||= FinApps::REST::Cashflows.new self
177
- @alert ||= FinApps::REST::Alert.new self
178
- @alert_definition ||= FinApps::REST::AlertDefinition.new self
179
- @alert_settings ||= FinApps::REST::AlertSettings.new self
180
- @alert_preferences ||= FinApps::REST::AlertPreferences.new self
181
- @rule_sets ||= FinApps::REST::Relevance::Rulesets.new self
234
+ logger.debug "##{__method__.to_s} => Completed"
235
+ response
182
236
  end
183
237
 
184
238
  end
@@ -9,17 +9,11 @@ module FinApps
9
9
  # @param [Hash] config
10
10
  # @return [Faraday::Connection]
11
11
  def set_up_connection(company_credentials, config)
12
- company_credentials.validate_required_strings!
13
- logger.debug "##{__method__.to_s} => company_credentials were provided."
14
-
15
- host = config[:host]
16
- validate_host_url! host
17
-
18
- base_url = "#{host}/v#{API_VERSION}"
19
- logger.debug " base_url: #{base_url}"
12
+ host_url = config[:host].blank? ? DEFAULTS[:host] : config[:host]
13
+ raise InvalidArgumentsError.new "Invalid argument: host_url: #{host_url}" unless host_url.start_with?('http://', 'https://')
20
14
 
15
+ base_url = "#{host_url}/v#{API_VERSION}"
21
16
  timeout = config[:timeout].blank? ? DEFAULTS[:timeout] : config[:timeout]
22
- logger.debug " timeout: #{timeout}"
23
17
 
24
18
  Faraday.new(:url => base_url,
25
19
  :request => {
@@ -29,47 +23,28 @@ module FinApps
29
23
  :accept => HEADERS[:accept],
30
24
  :user_agent => HEADERS[:user_agent]}) do |conn|
31
25
 
32
- set_request_middleware(conn, company_credentials)
33
- set_basic_authentication(conn, config)
34
- set_response_middleware(conn)
35
-
36
- # Adapter (ensure that the adapter is always last.)
37
- conn.adapter :typhoeus
38
- end
39
- end
26
+ # add basic authentication header if user credentials were provided
27
+ user_identifier = config[:user_identifier]
28
+ user_token = config[:user_token]
29
+ conn.request :basic_auth, user_identifier, user_token unless user_identifier.blank? || user_token.blank?
40
30
 
41
- private
42
- def set_response_middleware(conn)
43
- conn.use FinApps::Middleware::RaiseHttpExceptions
44
- conn.response :rashify
45
- conn.response :json, :content_type => /\bjson$/
46
- conn.use FinApps::Middleware::ResponseLogger
47
- end
31
+ # company level authentication
32
+ conn.use FinApps::Middleware::ApiToken, company_credentials
48
33
 
49
- def set_request_middleware(conn, company_credentials)
50
- conn.use FinApps::Middleware::ApiToken, company_credentials
51
- conn.request :json
52
- conn.request :retry
53
- conn.request :multipart
54
- conn.request :url_encoded
55
- end
34
+ conn.request :json
35
+ conn.request :retry
36
+ conn.request :multipart
37
+ conn.request :url_encoded
38
+ conn.use FinApps::Middleware::RaiseHttpExceptions
39
+ conn.response :rashify
40
+ conn.response :json, :content_type => /\bjson$/
41
+ conn.use FinApps::Middleware::ResponseLogger
56
42
 
57
- def set_basic_authentication(conn, config)
58
- if config[:user_identifier].blank? || config[:user_token].blank?
59
- logger.debug "##{__method__.to_s} => User credentials were not provided. Authentication header not set."
60
- else
61
- conn.request :basic_auth, config[:user_identifier], config[:user_token]
62
- logger.debug "##{__method__.to_s} => Basic Authentication header set for provided user credentials."
43
+ # Adapter (ensure that the adapter is always last.)
44
+ conn.adapter :typhoeus
63
45
  end
64
46
  end
65
47
 
66
- def validate_host_url!(host_url)
67
- raise MissingArgumentsError.new 'Missing argument: host_url.' if host_url.blank?
68
- raise InvalidArgumentsError.new 'Invalid argument: host_url does not specify a valid protocol (http/https).' unless host_url.start_with?('http://', 'https://')
69
-
70
- logger.debug "##{__method__.to_s} => host [#{host_url}] passed validation."
71
- end
72
-
73
48
  end
74
49
  end
75
- end
50
+ end
@@ -27,7 +27,6 @@ module FinApps
27
27
  :users_update => 'user',
28
28
  :users_update_password => 'user/password',
29
29
  :users_delete => 'users/:public_id',
30
- :users_show => 'users/:public_id',
31
30
  :users_login => 'users/login',
32
31
 
33
32
  :relevance_rulesets_list => 'relevance/ruleset/names',
@@ -86,4 +85,4 @@ module FinApps
86
85
 
87
86
  end
88
87
  end
89
- end
88
+ end
@@ -9,6 +9,8 @@ module FinApps
9
9
  # @param [String] term
10
10
  # @return [Array<FinApps::REST::Institution>, Array<String>]
11
11
  def search(term)
12
+ logger.debug "##{__method__.to_s} => Started"
13
+
12
14
  raise MissingArgumentsError.new 'Missing argument: term.' if term.blank?
13
15
  logger.debug "##{__method__.to_s} => term: #{term}"
14
16
 
@@ -18,12 +20,16 @@ module FinApps
18
20
  path = end_point.sub ':search_term', ERB::Util.url_encode(term)
19
21
  logger.debug "##{__method__.to_s} => path: #{path}"
20
22
 
21
- institutions, error_messages = @client.send(path, :get)
23
+ institutions, error_messages = @client.send_request(path, :get)
24
+
25
+ logger.debug "##{__method__.to_s} => Completed"
22
26
  return institutions, error_messages
23
27
  end
24
28
 
25
29
  # @param [Integer] site_id
26
30
  def form(site_id)
31
+ logger.debug "##{__method__.to_s} => Started"
32
+
27
33
  raise MissingArgumentsError.new 'Missing argument: site_id.' if site_id.blank?
28
34
  logger.debug "##{__method__.to_s} => site_id: #{site_id}"
29
35
 
@@ -33,7 +39,9 @@ module FinApps
33
39
  path = end_point.sub ':site_id', ERB::Util.url_encode(site_id)
34
40
  logger.debug "##{__method__.to_s} => path: #{path}"
35
41
 
36
- institution, error_messages = @client.send(path, :get) { |r| Institution.new(r.body) }
42
+ institution, error_messages = @client.send_request(path, :get) { |r| Institution.new(r.body) }
43
+
44
+ logger.debug "##{__method__.to_s} => Completed"
37
45
  return institution, error_messages
38
46
  end
39
47
 
@@ -44,4 +52,4 @@ module FinApps
44
52
  end
45
53
 
46
54
  end
47
- end
55
+ end
@@ -7,7 +7,7 @@ module FinApps
7
7
  class Rulesets < FinApps::REST::Resources
8
8
 
9
9
  def list
10
-
10
+ logger.debug "##{__method__.to_s} => Started"
11
11
 
12
12
  end_point = Defaults::END_POINTS[:relevance_rulesets_list]
13
13
  logger.debug "##{__method__.to_s} => end_point: #{end_point}"
@@ -15,14 +15,14 @@ module FinApps
15
15
  path = end_point
16
16
  logger.debug "##{__method__.to_s} => path: #{path}"
17
17
 
18
- results, error_messages = @client.send(path, :get)
19
-
18
+ results, error_messages = @client.send_request(path, :get)
20
19
 
20
+ logger.debug "##{__method__.to_s} => Completed"
21
21
  return results, error_messages
22
22
  end
23
23
 
24
24
  def show(ruleset_name)
25
-
25
+ logger.debug "##{__method__.to_s} => Started"
26
26
 
27
27
  raise MissingArgumentsError.new 'Missing argument: ruleset_name.' if ruleset_name.blank?
28
28
  logger.debug "##{__method__.to_s} => ruleset_name: #{ruleset_name}"
@@ -33,14 +33,14 @@ module FinApps
33
33
  path = end_point.sub ':ruleset_name', ERB::Util.url_encode(ruleset_name)
34
34
  logger.debug "##{__method__.to_s} => path: #{path}"
35
35
 
36
- results, error_messages = @client.send(path, :get)
37
-
36
+ results, error_messages = @client.send_request(path, :get)
38
37
 
38
+ logger.debug "##{__method__.to_s} => Completed"
39
39
  return results, error_messages
40
40
  end
41
41
 
42
42
  def run(params = {})
43
-
43
+ logger.debug "##{__method__.to_s} => Started"
44
44
 
45
45
  raise MissingArgumentsError.new 'Missing argument: params.' if params.blank?
46
46
  logger.debug "##{__method__.to_s} => params: #{params.inspect}"
@@ -51,9 +51,9 @@ module FinApps
51
51
  path = end_point
52
52
  logger.debug "##{__method__.to_s} => path: #{path}"
53
53
 
54
- results, error_messages = @client.send(path, :post, params)
55
-
54
+ results, error_messages = @client.send_request(path, :post, params)
56
55
 
56
+ logger.debug "##{__method__.to_s} => Completed"
57
57
  return results, error_messages
58
58
  end
59
59
 
@@ -61,4 +61,4 @@ module FinApps
61
61
  end
62
62
 
63
63
  end
64
- end
64
+ end