wrapi 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile +1 -2
- data/README.md +4 -1
- data/Rakefile +8 -0
- data/bin/cc-test-reporter.exe +0 -0
- data/lib/wrapi/configuration.rb +7 -2
- data/lib/wrapi/connection.rb +3 -3
- data/lib/wrapi/entity.rb +29 -19
- data/lib/wrapi/pagination.rb +1 -1
- data/lib/wrapi/request.rb +50 -18
- data/lib/wrapi/respond_to.rb +2 -2
- data/lib/wrapi/version.rb +1 -1
- data/wrapi.gemspec +2 -0
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9f4476458af28d2c64f885a156dbab8e4797d30992b8b52a92fbe9876456442
|
4
|
+
data.tar.gz: d1cd4782db7f857ca68c72f956fa2b061b029e545a81ff85901ea39958345b32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ced08d3424e62508e0dca3e85e30a7fd1a6d116b0a13bed7aec1d11b77234e8aaf93b8d4dce32c2fb3e3787baf95da59f1c595f7b7d318eb63cea9d8f301c6cf
|
7
|
+
data.tar.gz: c2984a92eef0e8038e43d3eb9bbc775490372dbc3eefe97b5ea41c475e77dc0d2b26ecaac64c93ee5977c2ccb5e3089cbdf2a479b748a18e84cb446e7d14ba32
|
data/CHANGELOG.md
CHANGED
@@ -15,3 +15,13 @@
|
|
15
15
|
## [0.2.0] - 2024-02-8
|
16
16
|
- implement json pagination
|
17
17
|
|
18
|
+
## [0.2.0] - 2024-02-13
|
19
|
+
- implement option to manipulate request
|
20
|
+
|
21
|
+
## [0.4.0] - 2024-02-13
|
22
|
+
- testing/code quality
|
23
|
+
authentication tests (mocked)
|
24
|
+
test string/{}/[] entities returned with mock
|
25
|
+
request tests with mock including delete/put/post
|
26
|
+
- Entity fix issues returning json arrays
|
27
|
+
Request option to return raw response
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# Wrapper API
|
2
|
+
[](https://rubygems.org/gems/wrapi)
|
3
|
+
[](https://codeclimate.com/github/jancotanis/wrapi/maintainability)
|
4
|
+
[](https://codeclimate.com/github/jancotanis/wrapi/test_coverage)
|
2
5
|
|
3
|
-
Some generic
|
6
|
+
Some generic code extracted from by a number of api wrapper gems. Internal used only.
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
data/Rakefile
CHANGED
@@ -1,12 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'bundler/gem_tasks'
|
4
|
+
require 'dotenv'
|
4
5
|
require 'rake/testtask'
|
5
6
|
|
7
|
+
Dotenv.load
|
8
|
+
|
9
|
+
system './bin/cc-test-reporter before-build'
|
10
|
+
|
6
11
|
Rake::TestTask.new(:test) do |t|
|
7
12
|
t.libs << 'test'
|
8
13
|
t.libs << 'lib'
|
9
14
|
t.test_files = FileList['test/**/*_test.rb']
|
15
|
+
at_exit do
|
16
|
+
system './bin/cc-test-reporter after-build'
|
17
|
+
end
|
10
18
|
end
|
11
19
|
|
12
20
|
require 'rubocop/rake_task'
|
Binary file
|
data/lib/wrapi/configuration.rb
CHANGED
@@ -2,10 +2,15 @@
|
|
2
2
|
#require_relative './version'
|
3
3
|
|
4
4
|
module WrAPI
|
5
|
+
|
5
6
|
# Defines constants and methods related to configuration
|
7
|
+
# If configuration is overridden, please add following methods
|
8
|
+
# @see [self.extended(base)] to initialize the Configuration
|
9
|
+
# If additional options are added, please overide
|
10
|
+
# @see [reset] to initialize variables
|
11
|
+
# @see [options] to return the correct set of options
|
6
12
|
module Configuration
|
7
|
-
# An array of valid keys in the options hash when configuring a {
|
8
|
-
|
13
|
+
# An array of valid keys in the options hash when configuring a {WrAPI::API}
|
9
14
|
VALID_OPTIONS_KEYS = [
|
10
15
|
:access_token,
|
11
16
|
:token_type,
|
data/lib/wrapi/connection.rb
CHANGED
@@ -33,7 +33,7 @@ module WrAPI
|
|
33
33
|
'User-Agent': user_agent
|
34
34
|
},
|
35
35
|
url: endpoint
|
36
|
-
}.merge(connection_options)
|
36
|
+
}.merge(connection_options || {})
|
37
37
|
end
|
38
38
|
|
39
39
|
# callback method to setup api authorization
|
@@ -52,9 +52,9 @@ module WrAPI
|
|
52
52
|
connection.response :logger, logger, { headers: true, bodies: true } do |l|
|
53
53
|
# filter json content
|
54
54
|
l.filter(/("password":")(.+?)(".*)/, '\1[REMOVED]\3')
|
55
|
-
l.filter(/("
|
55
|
+
l.filter(/("[Aa]ccess_?[Tt]oken":")(.+?)(".*)/, '\1[REMOVED]\3')
|
56
56
|
# filter header content
|
57
|
-
l.filter(/(client-secret
|
57
|
+
l.filter(/(client[-_]secret[:=].)([^&]+)/, '\1[REMOVED]')
|
58
58
|
l.filter(/(Authorization:.)([^&]+)/, '\1[REMOVED]')
|
59
59
|
end
|
60
60
|
end
|
data/lib/wrapi/entity.rb
CHANGED
@@ -6,40 +6,37 @@ module WrAPI
|
|
6
6
|
class Entity
|
7
7
|
attr_reader :attributes
|
8
8
|
|
9
|
+
# factory method to create entity or array of entities
|
10
|
+
def self.create(attributes)
|
11
|
+
|
12
|
+
if attributes.is_a? Array
|
13
|
+
Entity.entify(attributes)
|
14
|
+
else
|
15
|
+
Entity.new(attributes) if attributes
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
9
19
|
def initialize(attributes)
|
10
20
|
@_raw = attributes
|
11
21
|
|
12
22
|
case attributes
|
13
23
|
when Hash
|
14
24
|
@attributes = attributes.clone.transform_keys(&:to_s)
|
15
|
-
when Array
|
16
|
-
# make deep copy
|
17
|
-
@attributes = entify(attributes)
|
18
25
|
else
|
19
26
|
@attributes = attributes.clone
|
20
27
|
end
|
21
28
|
end
|
22
29
|
|
23
30
|
def method_missing(method_sym, *arguments, &block)
|
24
|
-
len = arguments.length
|
25
31
|
# assignment
|
26
32
|
if (method = method_sym[/.*(?==\z)/m])
|
27
|
-
raise! ArgumentError, "wrong number of arguments (given #{
|
33
|
+
raise! ArgumentError, "wrong number of arguments (given #{arguments.length}, expected 1)", caller(1) unless arguments.length == 1
|
28
34
|
|
29
35
|
@attributes[method] = arguments[0]
|
30
36
|
elsif @attributes.include? method_sym.to_s
|
31
|
-
|
32
|
-
case r
|
33
|
-
when Hash
|
34
|
-
@attributes[method_sym.to_s] = self.class.new(r)
|
35
|
-
when Array
|
36
|
-
# make deep copy
|
37
|
-
@attributes[method_sym.to_s] = r = entify(r)
|
38
|
-
r
|
39
|
-
else
|
40
|
-
r
|
41
|
-
end
|
37
|
+
accessor(method_sym.to_s)
|
42
38
|
else
|
39
|
+
# delegate to hash
|
43
40
|
@attributes.send(method_sym, *arguments, &block)
|
44
41
|
end
|
45
42
|
end
|
@@ -55,10 +52,23 @@ module WrAPI
|
|
55
52
|
def to_json(options = {})
|
56
53
|
@_raw.to_json
|
57
54
|
end
|
58
|
-
|
59
|
-
def
|
55
|
+
|
56
|
+
def accessor(method)
|
57
|
+
case @attributes[method]
|
58
|
+
when Hash
|
59
|
+
@attributes[method] = self.class.new(@attributes[method])
|
60
|
+
when Array
|
61
|
+
# make deep copy
|
62
|
+
@attributes[method] = Entity.entify(@attributes[method])
|
63
|
+
else
|
64
|
+
@attributes[method]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.entify(a)
|
60
69
|
a.map do |item|
|
61
|
-
item.is_a?(Hash) ? self.class.new(item) : item
|
70
|
+
#item.is_a?(Hash) ? self.class.new(item) : item
|
71
|
+
Entity.create(item)
|
62
72
|
end
|
63
73
|
end
|
64
74
|
end
|
data/lib/wrapi/pagination.rb
CHANGED
data/lib/wrapi/request.rb
CHANGED
@@ -7,26 +7,37 @@ module WrAPI
|
|
7
7
|
module Request
|
8
8
|
|
9
9
|
# Perform an HTTP GET request and return entity incase format is :json
|
10
|
-
|
11
|
-
|
12
|
-
:
|
10
|
+
# @return if format is :json and !raw an [Entity] is returned, otherwhise the response body
|
11
|
+
def get(path, options = {}, raw=false)
|
12
|
+
response = request(:get, path, options) do |request|
|
13
|
+
yield(request) if block_given?
|
14
|
+
end
|
15
|
+
entity_response(response, raw)
|
13
16
|
end
|
14
17
|
|
15
18
|
# Perform an HTTP GET request for paged date sets response
|
16
|
-
|
19
|
+
# @return nil if block given, otherwise complete concatenated json result set
|
20
|
+
def get_paged(path, options = {}, request_labda = nil)
|
17
21
|
raise ArgumentError,
|
18
22
|
"Pages requests should be json formatted (given format '#{format}')" unless :json.eql? format
|
19
23
|
|
20
24
|
result = []
|
21
25
|
pager = create_pager
|
22
26
|
while pager.more_pages?
|
23
|
-
response = request(:get, path, options.merge(pager.page_options))
|
24
|
-
|
25
|
-
|
26
|
-
if
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
response = request(:get, path, options.merge(pager.page_options)) do |req|
|
28
|
+
request_labda.call(req) if request_labda
|
29
|
+
end
|
30
|
+
if d = pager.class.data(response.body)
|
31
|
+
d = Entity.create(d)
|
32
|
+
if block_given?
|
33
|
+
yield(d)
|
34
|
+
else
|
35
|
+
if d.is_a? Array
|
36
|
+
result += d
|
37
|
+
else
|
38
|
+
result << d
|
39
|
+
end
|
40
|
+
end
|
30
41
|
end
|
31
42
|
pager.next_page!(response.body)
|
32
43
|
end
|
@@ -34,29 +45,42 @@ module WrAPI
|
|
34
45
|
end
|
35
46
|
|
36
47
|
# Perform an HTTP POST request
|
37
|
-
|
38
|
-
|
48
|
+
# @return response is returned in json if format is :json
|
49
|
+
def post(path, options = {}, raw=true)
|
50
|
+
response = request(:post, path, options) do |request|
|
51
|
+
yield(request) if block_given?
|
52
|
+
end
|
53
|
+
entity_response(response, raw)
|
39
54
|
end
|
40
55
|
|
41
56
|
# Perform an HTTP PUT request
|
42
|
-
|
43
|
-
|
57
|
+
# @return response is returned in json if format is :json
|
58
|
+
def put(path, options = {}, raw=true)
|
59
|
+
response = request(:put, path, options) do |request|
|
60
|
+
yield(request) if block_given?
|
61
|
+
end
|
62
|
+
entity_response(response, raw)
|
44
63
|
end
|
45
64
|
|
46
65
|
# Perform an HTTP DELETE request
|
47
|
-
|
48
|
-
|
66
|
+
# @return response is returened
|
67
|
+
def delete(path, options = {}, raw=false)
|
68
|
+
response = request(:delete, path, options) do |request|
|
69
|
+
yield(request) if block_given?
|
70
|
+
end
|
71
|
+
entity_response(response, raw)
|
49
72
|
end
|
50
73
|
|
51
74
|
private
|
52
75
|
|
53
76
|
def create_pager
|
54
|
-
pagination_class.new(page_size)
|
77
|
+
pagination_class ? pagination_class.new(page_size) : WrAPI::RequestPagination::DefaultPager
|
55
78
|
end
|
56
79
|
|
57
80
|
# Perform an HTTP request
|
58
81
|
def request(method, path, options)
|
59
82
|
response = connection.send(method) do |request|
|
83
|
+
yield(request) if block_given?
|
60
84
|
uri = URI::Parser.new
|
61
85
|
case method
|
62
86
|
when :get, :delete
|
@@ -69,5 +93,13 @@ module WrAPI
|
|
69
93
|
end
|
70
94
|
response
|
71
95
|
end
|
96
|
+
|
97
|
+
def entity_response(response, raw=false)
|
98
|
+
if :json.eql?(format) && !raw
|
99
|
+
Entity.create(pagination_class.data(response.body))
|
100
|
+
else
|
101
|
+
response
|
102
|
+
end
|
103
|
+
end
|
72
104
|
end
|
73
105
|
end
|
data/lib/wrapi/respond_to.rb
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
module WrAPI
|
3
3
|
module RespondTo
|
4
4
|
|
5
|
-
# Delegate to
|
5
|
+
# Delegate to Client
|
6
6
|
def self.method_missing(method, *args, &block)
|
7
7
|
return super unless client.respond_to?(method)
|
8
8
|
client.send(method, *args, &block)
|
9
9
|
end
|
10
10
|
|
11
|
-
# Delegate to
|
11
|
+
# Delegate to Client
|
12
12
|
def self.respond_to?(method, include_all = false)
|
13
13
|
client.respond_to?(method, include_all) || super
|
14
14
|
end
|
data/lib/wrapi/version.rb
CHANGED
data/wrapi.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wrapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janco Tanis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-02-
|
11
|
+
date: 2024-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -52,6 +52,34 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
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: webmock
|
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'
|
55
83
|
description:
|
56
84
|
email: gems@jancology.com
|
57
85
|
executables: []
|
@@ -63,6 +91,7 @@ files:
|
|
63
91
|
- Gemfile
|
64
92
|
- README.md
|
65
93
|
- Rakefile
|
94
|
+
- bin/cc-test-reporter.exe
|
66
95
|
- lib/wrapi.rb
|
67
96
|
- lib/wrapi/api.rb
|
68
97
|
- lib/wrapi/authentication.rb
|