wrapi 0.3.0 → 0.4.1
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 +2 -2
- data/lib/wrapi/entity.rb +29 -19
- data/lib/wrapi/request.rb +36 -20
- 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: d7a1a27203e141d7bbc0f20e3e65f18dafc255c33a051d3e03786cdb88fefa6f
|
4
|
+
data.tar.gz: b9cb8227c911fcb9ef133c8816c41e5dee626f3c0ea9ee4f1d5538f7c48f61cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65296ac28b6371f30ef67d7c6c4736d7a1a75820f5c33488acd9eaa370eecb63e5407dad016484e13e05b184faf99b853cac0c4c2f0e6ccd1e527cadb5a9212e
|
7
|
+
data.tar.gz: 18ab1a6603f10c89191c3fdf5345b7dbf22a2862bf251e1477b8d0da092679bdc8a1698f87b65e8e9cae68e69f8ff111e758822cb1ddb2560da594d4472cf56b
|
data/CHANGELOG.md
CHANGED
@@ -18,3 +18,13 @@
|
|
18
18
|
## [0.2.0] - 2024-02-13
|
19
19
|
- implement option to manipulate request
|
20
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
|
28
|
+
|
29
|
+
## [0.4.1] - 2024-02-28
|
30
|
+
- fix issue with post body only supported as json
|
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,12 +33,12 @@ 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
|
40
40
|
def setup_authorization(connection)
|
41
|
-
connection.
|
41
|
+
connection.headers['Authorization'] = "Bearer #{access_token}" if access_token
|
42
42
|
end
|
43
43
|
|
44
44
|
# callback method to setup api headers
|
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/request.rb
CHANGED
@@ -7,19 +7,19 @@ module WrAPI
|
|
7
7
|
module Request
|
8
8
|
|
9
9
|
# Perform an HTTP GET request and return entity incase format is :json
|
10
|
-
# @return if format is :json an [Entity] is returned, otherwhise the response body
|
11
|
-
def get(path, options = {})
|
10
|
+
# @return if format is :json and !raw an [Entity] is returned, otherwhise the response body
|
11
|
+
def get(path, options = {}, raw=false)
|
12
12
|
response = request(:get, path, options) do |request|
|
13
13
|
yield(request) if block_given?
|
14
14
|
end
|
15
|
-
|
15
|
+
entity_response(response, raw)
|
16
16
|
end
|
17
17
|
|
18
18
|
# Perform an HTTP GET request for paged date sets response
|
19
19
|
# @return nil if block given, otherwise complete concatenated json result set
|
20
20
|
def get_paged(path, options = {}, request_labda = nil)
|
21
21
|
raise ArgumentError,
|
22
|
-
"Pages requests should be json formatted (given format '#{format}')" unless
|
22
|
+
"Pages requests should be json formatted (given format '#{format}')" unless is_json?
|
23
23
|
|
24
24
|
result = []
|
25
25
|
pager = create_pager
|
@@ -27,19 +27,16 @@ module WrAPI
|
|
27
27
|
response = request(:get, path, options.merge(pager.page_options)) do |req|
|
28
28
|
request_labda.call(req) if request_labda
|
29
29
|
end
|
30
|
-
d = pager.class.data(response.body)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
d = Entity.new(d)
|
35
|
-
end
|
36
|
-
if block_given?
|
37
|
-
yield(d)
|
38
|
-
else
|
39
|
-
if d.is_a? Array
|
40
|
-
result += d
|
30
|
+
if d = pager.class.data(response.body)
|
31
|
+
d = Entity.create(d)
|
32
|
+
if block_given?
|
33
|
+
yield(d)
|
41
34
|
else
|
42
|
-
|
35
|
+
if d.is_a? Array
|
36
|
+
result += d
|
37
|
+
else
|
38
|
+
result << d
|
39
|
+
end
|
43
40
|
end
|
44
41
|
end
|
45
42
|
pager.next_page!(response.body)
|
@@ -49,26 +46,33 @@ module WrAPI
|
|
49
46
|
|
50
47
|
# Perform an HTTP POST request
|
51
48
|
# @return response is returned in json if format is :json
|
52
|
-
def post(path, options = {})
|
49
|
+
def post(path, options = {}, raw=true)
|
53
50
|
response = request(:post, path, options) do |request|
|
54
51
|
yield(request) if block_given?
|
55
52
|
end
|
53
|
+
entity_response(response, raw)
|
56
54
|
end
|
57
55
|
|
58
56
|
# Perform an HTTP PUT request
|
59
57
|
# @return response is returned in json if format is :json
|
60
|
-
def put(path, options = {})
|
58
|
+
def put(path, options = {}, raw=true)
|
61
59
|
response = request(:put, path, options) do |request|
|
62
60
|
yield(request) if block_given?
|
63
61
|
end
|
62
|
+
entity_response(response, raw)
|
64
63
|
end
|
65
64
|
|
66
65
|
# Perform an HTTP DELETE request
|
67
66
|
# @return response is returened
|
68
|
-
def delete(path, options = {})
|
67
|
+
def delete(path, options = {}, raw=false)
|
69
68
|
response = request(:delete, path, options) do |request|
|
70
69
|
yield(request) if block_given?
|
71
70
|
end
|
71
|
+
entity_response(response, raw)
|
72
|
+
end
|
73
|
+
|
74
|
+
def is_json?
|
75
|
+
format && 'json'.eql?(format.to_s)
|
72
76
|
end
|
73
77
|
|
74
78
|
private
|
@@ -88,10 +92,22 @@ module WrAPI
|
|
88
92
|
when :post, :put
|
89
93
|
request.headers['Content-Type'] = "application/#{format}"
|
90
94
|
request.path = uri.escape(path)
|
91
|
-
|
95
|
+
if is_json? && !options.empty?
|
96
|
+
request.body = options.to_json
|
97
|
+
else
|
98
|
+
request.body = URI.encode_www_form(options) unless options.empty?
|
99
|
+
end
|
92
100
|
end
|
93
101
|
end
|
94
102
|
response
|
95
103
|
end
|
104
|
+
|
105
|
+
def entity_response(response, raw=false)
|
106
|
+
if is_json? && !raw
|
107
|
+
Entity.create(pagination_class.data(response.body))
|
108
|
+
else
|
109
|
+
response
|
110
|
+
end
|
111
|
+
end
|
96
112
|
end
|
97
113
|
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.1
|
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-28 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
|