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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f1cf824802fe7b24c8df963fcf6598e650d4bcf8393f4d685cab9f4825838ca
4
- data.tar.gz: 4f7b2f485cdbbcf3e2f10aa64fa0d562d789bb23aa2e6c47611bb7ff5a86db29
3
+ metadata.gz: d9f4476458af28d2c64f885a156dbab8e4797d30992b8b52a92fbe9876456442
4
+ data.tar.gz: d1cd4782db7f857ca68c72f956fa2b061b029e545a81ff85901ea39958345b32
5
5
  SHA512:
6
- metadata.gz: d601f302816a8e521bfd00985f04391a939c4f416a06e7b5accb0701a6c02109f7653b4be0ea8d7b12b191f867284959e0068a1b84495c082dd600c094636e2e
7
- data.tar.gz: 16c72f9028b2060dc39b28a9c7037e3d2fba2ebe2a1079a2dbfe3eef2f9ed198ecc60c7dd4ae0c61a1cf79ddefe4638789b4e5072e07da36236b0cf4d3fc92f6
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
@@ -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,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(/("accessToken":")(.+?)(".*)/, '\1[REMOVED]\3')
55
+ l.filter(/("[Aa]ccess_?[Tt]oken":")(.+?)(".*)/, '\1[REMOVED]\3')
56
56
  # filter header content
57
- l.filter(/(client-secret:.)([^&]+)/, '\1[REMOVED]')
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 #{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
@@ -6,7 +6,7 @@ module WrAPI
6
6
  # required attributes format
7
7
  module RequestPagination
8
8
 
9
- # Defaut pages asumes all sdata retrieved in a single go.
9
+ # Defaut pages asumes all data retrieved in a single go.
10
10
  class DefaultPager
11
11
 
12
12
  # initialize with page size
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
- def get(path, options = {})
11
- response = request(:get, path, options)
12
- :json.eql?(format) ? Entity.new(pagination_class.data(response.body)) : response.body
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
- def get_paged(path, options = {}, &block)
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
- #data = response.body
25
- d = pager.class.data(response.body).map { |e| Entity.new(e) }
26
- if block_given?
27
- yield(d)
28
- else
29
- result += d
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
- def post(path, options = {})
38
- request(:post, path, options)
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
- def put(path, options = {})
43
- request(:put, path, options)
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
- def delete(path, options = {})
48
- request(:delete, path, options)
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
@@ -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.2.0'
4
+ VERSION = '0.4.0'
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.2.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-08 00:00:00.000000000 Z
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