active_record_api-request 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2d2693b81bbda9bd061fff4179384f6f644359d1012d9d12cabaf438025fc9f9
4
+ data.tar.gz: db410e238f262f295f164c6bb9b36557e0f2d0a6410cf582e83f4e80857930d0
5
+ SHA512:
6
+ metadata.gz: 168799989e78b675825d3ace44ecee1b6edb367d67be77f4539da037a422160049bbfa7676b906fb6da2657119d266635012c6aad2107417e9d32532dc8718de
7
+ data.tar.gz: a1d03ed591e6530fe8d90a1734710bfa836fd30e821bcddbe92554da7a6f4e204c16aad58a2b96b9f1acf54b57c5f4d1ee2f4ec71f9088b7f7f7e323f01d22b0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 samuelbirk
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # ActiveRecordApi::Request
2
+ This is a library intended to make it easy to access services using the ActiveRecordApi::RestController gem.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'active_record_api-request'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install active_record_api-request
19
+ ```
20
+
21
+ ## Usage
22
+ ### Query [index action]
23
+ ```ruby
24
+ ActiveRecordApi::Request::Methods.new(
25
+ host: 'https://advisor.beta.fullmeasureed.com/api',
26
+ path: 'integrations/integrations',
27
+ token: 'nfwv89n4wvb98bgb0ver87*******',
28
+ debug: true
29
+
30
+ ).query(params: {orginization_id: 1, type: 'IntegrationS3'})
31
+ ```
32
+
33
+ ### GET [show action]
34
+ ```ruby
35
+ ActiveRecordApi::Request::Methods.new(
36
+ host: 'https://advisor.beta.fullmeasureed.com/api',
37
+ path: 'integrations/integrations',
38
+ token: 'nfwv89n4wvb98bgb0ver87*******',
39
+ debug: true
40
+
41
+ ).get(id: 1)
42
+ ```
43
+
44
+ ### POST [create action]
45
+ ```ruby
46
+ ActiveRecordApi::Request::Methods.new(
47
+ host: 'https://advisor.beta.fullmeasureed.com/api',
48
+ path: 'integrations/integrations',
49
+ token: 'nfwv89n4wvb98bgb0ver87*******',
50
+ debug: true
51
+ ).post(payload: {orginization_id: 1, type: 'IntegrationS3'})
52
+ ```
53
+
54
+ ### PUT [create action]
55
+ ```ruby
56
+ ActiveRecordApi::Request::Methods.new(
57
+ host: 'https://advisor.beta.fullmeasureed.com/api',
58
+ path: 'integrations/integrations',
59
+ token: 'nfwv89n4wvb98bgb0ver87a*****',
60
+ debug: true
61
+ ).put(id: 1, payload: {orginization_id: 1, type: 'IntegrationS3'})
62
+ ```
63
+ ## Authentication
64
+ - If you have a token you can just provide it directly
65
+ - Alternatively, you can provide a email and password
66
+ - NOTE: Make sure you also provide an environment variable for the token path
67
+ ```ruby
68
+ ActiveRecordApi::Request::Methods.new(
69
+ host: 'https://advisor.beta.fullmeasureed.com/api',
70
+ path: 'integrations/integrations',
71
+ email: 'samuel.bi**@gmail.com',
72
+ password: 'P@ss***',
73
+ ).get(id: 1)
74
+ ```
75
+
76
+ ## Configuration
77
+ - Many of the credentials can be provided via environment variables or Rails 5.2+ credentials
78
+
79
+ ### Environment Variables
80
+ ```bash
81
+ export ACTIVE_RECORD_API_REQUEST_HOST=https://advisor.beta.fullmeasureed.com/api
82
+ export ACTIVE_RECORD_API_REQUEST_token=nfwv89n4wvb98bgb0ver87*******
83
+ export ACTIVE_RECORD_API_REQUEST_EMAIL=samuel.bi**@gmail.com
84
+ export ACTIVE_RECORD_API_REQUEST_PASSWORD=P@ss***
85
+ export ACTIVE_RECORD_API_REQUEST_TOKEN_PATH=staff-auth/token
86
+ ```
87
+ ### Rails 5.2+ Credentials
88
+ ```yaml
89
+ active_record_api_request:
90
+ host: https://advisor.beta.fullmeasureed.com/api
91
+ token: nfwv89n4wvb98bgb0ver87*******
92
+ email: samuel.bi**@gmail.com
93
+ password: P@ss***
94
+ token_path: staff-auth/token
95
+ ```
96
+
97
+ ### GET using environment variables or credentials
98
+ ```ruby
99
+ ActiveRecordApi::Request::Methods.new(
100
+ path: 'integrations/integrations',
101
+ ).get(id: 1)
102
+ ```
103
+
104
+
105
+ ## License
106
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActiveRecordApi::Request'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
@@ -0,0 +1,14 @@
1
+ require 'active_support'
2
+
3
+ module ActiveRecordApi
4
+ module Request
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :VERSION
8
+ autoload :Methods
9
+ autoload :Credentials
10
+ autoload :Connection
11
+ autoload :FaradayFollowNextLinks
12
+ autoload :FaradayCacheServiceDown
13
+ end
14
+ end
@@ -0,0 +1,60 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'faraday-http-cache'
4
+
5
+ module ActiveRecordApi
6
+ module Request
7
+ class Connection < Credentials
8
+ attr_accessor :debug, :path, :cache_store
9
+ attr_writer :full_url
10
+
11
+ def authenticated_connection
12
+ connection.headers = connection.headers.merge(headers)
13
+ connection
14
+ end
15
+
16
+ def connection
17
+ @connection ||= Faraday.new do |builder|
18
+ builder.options[:open_timeout] = 2
19
+ builder.options[:timeout] = 5
20
+ builder.request :json
21
+ builder.request :url_encoded
22
+ builder.request :retry, max: 5, interval: 0.05, interval_randomness: 0.5, backoff_factor: 2
23
+ builder.use FaradayCacheServiceDown, cache_store
24
+ builder.use :http_cache, http_cache_options
25
+ builder.use FaradayFollowNextLinks, 5
26
+ builder.response :json, content_type: /\bjson$/
27
+ builder.response :raise_error
28
+ builder.response :logger if debug
29
+ builder.adapter Faraday.default_adapter
30
+ end
31
+ end
32
+
33
+ def full_url
34
+ @full_url ||= "#{strip_trailing_slash(host)}/#{strip_trailing_slash(path)}"
35
+ end
36
+
37
+ def full_url_with_params(params: {}, id: nil)
38
+ "#{full_url}/#{id}?#{params.to_query}"
39
+ end
40
+
41
+ def symbolize_response(response)
42
+ return response if response.is_a? String
43
+ if response.is_a? Array
44
+ return response if response.first.is_a? Array
45
+ response.map!(&:deep_symbolize_keys!)
46
+ else
47
+ response.try(:deep_symbolize_keys!)
48
+ end
49
+ end
50
+
51
+ def http_cache_options
52
+ { shared_cache: false, store: cache_store }
53
+ end
54
+
55
+ def strip_trailing_slash(string)
56
+ string.sub(%r{(\/)+$}, '')
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,48 @@
1
+ require 'active_attr'
2
+
3
+ module ActiveRecordApi
4
+ module Request
5
+ class Credentials
6
+ include ActiveAttr::Model
7
+ attr_writer :host, :token, :email, :password
8
+
9
+ def token
10
+ @token ||= config('token') || connection.post("#{host}/#{token_path}", credentials).body['access_token']
11
+ end
12
+
13
+ def host
14
+ @host ||= config(:host)
15
+ end
16
+
17
+ def token_path
18
+ @token_path ||= config(:token_path)
19
+ end
20
+
21
+ def email
22
+ @email ||= config(:email)
23
+ end
24
+
25
+ def password
26
+ @password ||= config(:password)
27
+ end
28
+
29
+ def headers
30
+ {
31
+ 'Authorization' => "Bearer #{token}"
32
+ }
33
+ end
34
+
35
+ def credentials
36
+ {
37
+ email: email,
38
+ password: password,
39
+ grant_type: 'password'
40
+ }
41
+ end
42
+
43
+ def config(name)
44
+ ENV["ACTIVE_RECORD_API_REQUEST_#{name.upcase}"] || Rails.application.credentials.try(:active_record_api_request).try(:[], name)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,49 @@
1
+ module ActiveRecordApi
2
+ module Request
3
+ class FaradayCacheServiceDown < Faraday::Middleware
4
+ attr_reader :cache
5
+ attr_reader :cache_identifier
6
+
7
+ def initialize(app, cache = nil, cache_identifier = 'empty')
8
+ super(app)
9
+ @cache = cache
10
+ @cache_identifier = cache_identifier
11
+ end
12
+
13
+ def call(env)
14
+ if :get == env[:method]
15
+ make_get_request(env)
16
+ else
17
+ @app.call(env)
18
+ end
19
+ end
20
+
21
+ def create_response(env)
22
+ hash = env.to_hash
23
+ {
24
+ status: hash[:status],
25
+ body: hash[:body],
26
+ response_headers: hash[:response_headers]
27
+ }
28
+ end
29
+
30
+ def make_get_request(env)
31
+ @app.call(env).on_complete do |response_env|
32
+ cache.write(cache_key(env), create_response(response_env))
33
+ response_env
34
+ end
35
+ rescue Faraday::TimeoutError
36
+ cached_response = cache.read(cache_key(env))
37
+ raise unless cached_response.present?
38
+ env.update(cached_response)
39
+ Faraday::Response.new(env)
40
+ end
41
+
42
+ def cache_key(env)
43
+ url = env[:url].dup
44
+ url.normalize!
45
+ "service_down:#{url.request_uri}:#{cache_identifier}"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,54 @@
1
+ require 'faraday_middleware'
2
+
3
+ module ActiveRecordApi
4
+ module Request
5
+ class FaradayFollowNextLinks < Faraday::Middleware
6
+ ENV_TO_CLEAR = %i[status response response_headers].to_set.freeze
7
+
8
+ attr_reader :max_pages
9
+
10
+ def initialize(app, max_pages = nil)
11
+ super(app)
12
+ @max_pages = max_pages
13
+ end
14
+
15
+ def call(env)
16
+ if :get == env[:method]
17
+ perform_with_next_links(env, max_pages)
18
+ else
19
+ @app.call(env)
20
+ end
21
+ end
22
+
23
+ def perform_with_next_links(env, max_pages_left)
24
+ request_body = env[:body]
25
+ response = @app.call(env)
26
+ response.on_complete do |response_env|
27
+ if next_link(env).present?
28
+ raise FaradayMiddleware::RedirectLimitReached, response if max_pages_left == 0
29
+ handle_response(env, request_body, response_env, max_pages_left)
30
+ end
31
+ end
32
+ response
33
+ end
34
+
35
+ def next_link(env)
36
+ env.response.headers.fetch('x-link-next', nil)
37
+ end
38
+
39
+ def handle_response(env, request_body, response_env, max_pages_left)
40
+ return unless env[:body].is_a?(Array)
41
+ new_request_env = update_env(response_env.dup, request_body, next_link(env))
42
+ max_pages_left -= 1 unless max_pages_left.nil?
43
+ env[:body] += perform_with_next_links(new_request_env, max_pages_left).body
44
+ end
45
+
46
+ def update_env(env, request_body, next_link)
47
+ env[:url] = URI.parse(next_link)
48
+ env[:body] = request_body
49
+ ENV_TO_CLEAR.each { |key| env.delete key }
50
+ env
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveRecordApi
2
+ module Request
3
+ class Methods < Connection
4
+ def query(params: {})
5
+ symbolize_response(authenticated_connection.get("#{full_url}/", params).body)
6
+ end
7
+
8
+ def get(id:)
9
+ symbolize_response(authenticated_connection.get("#{full_url}/#{id}").body)
10
+ end
11
+
12
+ def post(payload:, params: {})
13
+ symbolize_response(authenticated_connection.post(full_url_with_params(params: params), payload).body)
14
+ end
15
+
16
+ def put(id:, payload:, params: {})
17
+ symbolize_response(authenticated_connection.put(full_url_with_params(id: id, params: params), payload).body)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecordApi
2
+ module Request
3
+ VERSION = '0.1.1'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_api-request
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Full Measure Education
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_attr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday-http-cache
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday_middleware
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: webmock
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: A ruby library for making requests to the services using the active_record_api-rest_controller
154
+ gem
155
+ email:
156
+ - devops@fullmeasureed.com
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - MIT-LICENSE
162
+ - README.md
163
+ - Rakefile
164
+ - lib/active_record_api/request.rb
165
+ - lib/active_record_api/request/connection.rb
166
+ - lib/active_record_api/request/credentials.rb
167
+ - lib/active_record_api/request/faraday_cache_service_down.rb
168
+ - lib/active_record_api/request/faraday_follow_next_links.rb
169
+ - lib/active_record_api/request/methods.rb
170
+ - lib/active_record_api/request/version.rb
171
+ homepage: https://gitlab.com/fullmeasure/public/gems/active_record_api-request
172
+ licenses:
173
+ - MIT
174
+ metadata: {}
175
+ post_install_message:
176
+ rdoc_options: []
177
+ require_paths:
178
+ - lib
179
+ required_ruby_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ requirements: []
190
+ rubyforge_project:
191
+ rubygems_version: 2.7.7
192
+ signing_key:
193
+ specification_version: 4
194
+ summary: A ruby library for making requests to the services using the active_record_api-rest_controller
195
+ gem
196
+ test_files: []