wrapi 0.3.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d7d0d197548b78702536dcb3ffd582f1a637ba7c4563b678b682702a55f1f92
4
- data.tar.gz: 944dd3cb645f422900a45ff8728a08f47472abb98c64e7c3c34a3a8e696ad0d0
3
+ metadata.gz: d7a1a27203e141d7bbc0f20e3e65f18dafc255c33a051d3e03786cdb88fefa6f
4
+ data.tar.gz: b9cb8227c911fcb9ef133c8816c41e5dee626f3c0ea9ee4f1d5538f7c48f61cd
5
5
  SHA512:
6
- metadata.gz: 51854938895ac5cfdcb616fd9a6a6de72ed0e4a7544bea9eca525b64b8c370b2dbc9b0ca2b596fcbd1838f7b4eaec0a4fa476666e1bca79436f448c647d358cb
7
- data.tar.gz: fe27c57580cf3d50d49830d0b1449d177f0de21cb077eb12a3cc953351c1f3d9dc898de8cf4391130dea6f5e549cf9b6530928094cf5866482fefb5e1d795cca
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
@@ -2,8 +2,7 @@
2
2
 
3
3
  source 'https://rubygems.org'
4
4
 
5
- # Specify your gem's dependencies in cloudally.gemspec
5
+ # Specify your gem's dependencies in wrapi.gemspec
6
6
  gemspec
7
7
 
8
8
  gem 'rake', '~> 13.0'
9
- gem 'rubocop', '~> 1.7'
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # Wrapper API
2
+ [![Version](https://img.shields.io/gem/v/wrapi.svg)](https://rubygems.org/gems/wrapi)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/d84930f1f1d8fae05c5c/maintainability)](https://codeclimate.com/github/jancotanis/wrapi/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/d84930f1f1d8fae05c5c/test_coverage)](https://codeclimate.com/github/jancotanis/wrapi/test_coverage)
2
5
 
3
- Some generic cody extracted from by a number of api wrapper gems. Internal used only.
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
@@ -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 {Integra365::API}
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,
@@ -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.authorization :Bearer, access_token if access_token
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 #{len}, expected 1)", caller(1) unless len == 1
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
- r = @attributes[method_sym.to_s]
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 entify(a)
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
- :json.eql?(format) ? Entity.new(pagination_class.data(response.body)) : response.body
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 :json.eql? format
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
- if d.is_a? Array
32
- d = pager.class.data(response.body).map { |e| Entity.new(e) }
33
- else
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
- result << d
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
- request.body = options.to_json unless options.empty?
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
@@ -2,13 +2,13 @@
2
2
  module WrAPI
3
3
  module RespondTo
4
4
 
5
- # Delegate to Integra365::Client
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 Integra365::Client
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WrAPI
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.1'
5
5
  end
data/wrapi.gemspec CHANGED
@@ -31,4 +31,6 @@ Gem::Specification.new do |s|
31
31
  s.add_runtime_dependency 'faraday'
32
32
  s.add_development_dependency 'minitest'
33
33
  s.add_development_dependency 'rubocop'
34
+ s.add_development_dependency 'simplecov'
35
+ s.add_development_dependency 'webmock'
34
36
  end
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.3.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-13 00:00:00.000000000 Z
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