wrapi 0.1.3 → 0.3.0

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: 7707119492c003388e3396aebeb7a50b64009075e35fa6fbe8f779ae8312785e
4
- data.tar.gz: a503388426775cd56de06672c62b7ba7086963d8191f2782e519061c13c21b28
3
+ metadata.gz: 1d7d0d197548b78702536dcb3ffd582f1a637ba7c4563b678b682702a55f1f92
4
+ data.tar.gz: 944dd3cb645f422900a45ff8728a08f47472abb98c64e7c3c34a3a8e696ad0d0
5
5
  SHA512:
6
- metadata.gz: 1f7d3ef57fcd6ab8e0fcd92d387d093d0f46430349139f2b51718718c63da1d2429768b42fa159ec22384a26a753b2bdeaff9bac3f0778cbfdc344be3439aa7e
7
- data.tar.gz: a669367530c66723672ab9523e055d752e235f45aee312366a4c1edbbd9240e2f137d5ea5f965f09b5f7371c873841bb0843d26364452ec93ba9985af883ff64
6
+ metadata.gz: 51854938895ac5cfdcb616fd9a6a6de72ed0e4a7544bea9eca525b64b8c370b2dbc9b0ca2b596fcbd1838f7b4eaec0a4fa476666e1bca79436f448c647d358cb
7
+ data.tar.gz: fe27c57580cf3d50d49830d0b1449d177f0de21cb077eb12a3cc953351c1f3d9dc898de8cf4391130dea6f5e549cf9b6530928094cf5866482fefb5e1d795cca
data/CHANGELOG.md CHANGED
@@ -11,3 +11,10 @@
11
11
 
12
12
  ## [0.1.3] - 2024-02-5
13
13
  - fix entity should return empty array insted of nil
14
+
15
+ ## [0.2.0] - 2024-02-8
16
+ - implement json pagination
17
+
18
+ ## [0.2.0] - 2024-02-13
19
+ - implement option to manipulate request
20
+
data/Gemfile CHANGED
@@ -6,4 +6,4 @@ source 'https://rubygems.org'
6
6
  gemspec
7
7
 
8
8
  gem 'rake', '~> 13.0'
9
- gem 'rubocop', "~> 1.7'
9
+ gem 'rubocop', '~> 1.7'
@@ -1,4 +1,6 @@
1
- require_relative './version'
1
+ #require_relative './pagination'
2
+ #require_relative './version'
3
+
2
4
  module WrAPI
3
5
  # Defines constants and methods related to configuration
4
6
  module Configuration
@@ -18,7 +20,8 @@ module WrAPI
18
20
  :logger,
19
21
  :format,
20
22
  :page_size,
21
- :user_agent
23
+ :user_agent,
24
+ :pagination_class
22
25
  ].freeze
23
26
 
24
27
  # By default, don't set any connection options
@@ -36,6 +39,8 @@ module WrAPI
36
39
 
37
40
  # The user agent that will be sent to the API endpoint if none is set
38
41
  DEFAULT_USER_AGENT = "Ruby API wrapper #{WrAPI::VERSION}".freeze
42
+
43
+ DEFAULT_PAGINATION = WrAPI::RequestPagination::DefaultPager
39
44
 
40
45
  # @private
41
46
  attr_accessor *VALID_OPTIONS_KEYS
@@ -74,6 +79,7 @@ module WrAPI
74
79
  self.format = DEFAULT_FORMAT
75
80
  self.page_size = DEFAULT_PAGE_SIZE
76
81
  self.user_agent = DEFAULT_USER_AGENT
82
+ self.pagination_class = DEFAULT_PAGINATION
77
83
  end
78
84
  end
79
85
  end
@@ -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
@@ -0,0 +1,39 @@
1
+ require 'uri'
2
+ require 'json'
3
+
4
+ module WrAPI
5
+ # Defines HTTP request methods
6
+ # required attributes format
7
+ module RequestPagination
8
+
9
+ # Defaut pages asumes all data retrieved in a single go.
10
+ class DefaultPager
11
+
12
+ # initialize with page size
13
+ def initialize(page_size=nil)
14
+ @page = 0
15
+ end
16
+
17
+ # go to next page
18
+ # @return true if nore pages
19
+ def next_page!(data=nil)
20
+ @page += 1
21
+ more_pages?
22
+ end
23
+
24
+ # assume single page
25
+ def more_pages?
26
+ @page < 1
27
+ end
28
+
29
+ def page_options
30
+ {}
31
+ end
32
+
33
+ def self.data(body)
34
+ body
35
+ end
36
+ end
37
+
38
+ end
39
+ end
data/lib/wrapi/request.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'uri'
2
2
  require 'json'
3
- require File.expand_path('entity', __dir__)
4
3
 
5
4
  module WrAPI
6
5
  # Defines HTTP request methods
@@ -8,65 +7,80 @@ module WrAPI
8
7
  module Request
9
8
 
10
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
11
  def get(path, options = {})
12
- response = request(:get, path, options)
13
- :json.eql?(format) ? Entity.new(response.body) : response.body
12
+ response = request(:get, path, options) do |request|
13
+ yield(request) if block_given?
14
+ end
15
+ :json.eql?(format) ? Entity.new(pagination_class.data(response.body)) : response.body
14
16
  end
15
17
 
16
- # Perform an HTTP GET request for paged date sets response ind to
17
- # Name Description
18
- # pageSize The number of records to display per page
19
- # page The page number
20
- # nextPageToken Next page token
21
- #
22
- # response format { "page": 0, "totalPages": 0, "total": 0, "nextPageToken": "string", "data": [] }
23
- def get_paged(path, options = {}, &block)
24
- raise! ArgumentError,
25
- "Pages requests should be json formatted (given format '#{format}')" unless :json.eql? format
18
+ # Perform an HTTP GET request for paged date sets response
19
+ # @return nil if block given, otherwise complete concatenated json result set
20
+ def get_paged(path, options = {}, request_labda = nil)
21
+ raise ArgumentError,
22
+ "Pages requests should be json formatted (given format '#{format}')" unless :json.eql? format
26
23
 
27
24
  result = []
28
- page = 1
29
- total = page + 1
30
- next_page = ''
31
- while page <= total
32
- following_page = { pageSize: page_size }
33
- following_page.merge!({ page: page, nextPageToken: next_page }) unless next_page.empty?
34
-
35
- response = request(:get, path, options.merge(following_page))
36
- data = response.body
37
- d = data['data'].map { |e| Entity.new(e) }
25
+ pager = create_pager
26
+ while pager.more_pages?
27
+ response = request(:get, path, options.merge(pager.page_options)) do |req|
28
+ request_labda.call(req) if request_labda
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
38
36
  if block_given?
39
37
  yield(d)
40
38
  else
41
- result += d
39
+ if d.is_a? Array
40
+ result += d
41
+ else
42
+ result << d
43
+ end
42
44
  end
43
- page += 1
44
- total = data['totalPages'].to_i
45
- next_page = data['nextPageToken']
45
+ pager.next_page!(response.body)
46
46
  end
47
47
  result unless block_given?
48
48
  end
49
49
 
50
50
  # Perform an HTTP POST request
51
+ # @return response is returned in json if format is :json
51
52
  def post(path, options = {})
52
- request(:post, path, options)
53
+ response = request(:post, path, options) do |request|
54
+ yield(request) if block_given?
55
+ end
53
56
  end
54
57
 
55
58
  # Perform an HTTP PUT request
59
+ # @return response is returned in json if format is :json
56
60
  def put(path, options = {})
57
- request(:put, path, options)
61
+ response = request(:put, path, options) do |request|
62
+ yield(request) if block_given?
63
+ end
58
64
  end
59
65
 
60
66
  # Perform an HTTP DELETE request
67
+ # @return response is returened
61
68
  def delete(path, options = {})
62
- request(:delete, path, options)
69
+ response = request(:delete, path, options) do |request|
70
+ yield(request) if block_given?
71
+ end
63
72
  end
64
73
 
65
74
  private
66
75
 
76
+ def create_pager
77
+ pagination_class ? pagination_class.new(page_size) : WrAPI::RequestPagination::DefaultPager
78
+ end
79
+
67
80
  # Perform an HTTP request
68
81
  def request(method, path, options)
69
82
  response = connection.send(method) do |request|
83
+ yield(request) if block_given?
70
84
  uri = URI::Parser.new
71
85
  case method
72
86
  when :get, :delete
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.1.3'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/wrapi.rb CHANGED
@@ -1,11 +1,12 @@
1
- require File.expand_path('wrapi/authentication', __dir__)
2
- require File.expand_path('wrapi/connection', __dir__)
1
+ require File.expand_path('wrapi/version', __dir__)
2
+ require File.expand_path('wrapi/pagination', __dir__)
3
3
  require File.expand_path('wrapi/configuration', __dir__)
4
+ require File.expand_path('wrapi/connection', __dir__)
4
5
  require File.expand_path('wrapi/api', __dir__)
5
- require File.expand_path('wrapi/request', __dir__)
6
6
  require File.expand_path('wrapi/entity', __dir__)
7
+ require File.expand_path('wrapi/request', __dir__)
7
8
  require File.expand_path('wrapi/respond_to', __dir__)
8
- require File.expand_path('wrapi/version', __dir__)
9
+ require File.expand_path('wrapi/authentication', __dir__)
9
10
 
10
11
  module WrAPI
11
12
  extend RespondTo
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.1.3
4
+ version: 0.3.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-05 00:00:00.000000000 Z
11
+ date: 2024-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -69,6 +69,7 @@ files:
69
69
  - lib/wrapi/configuration.rb
70
70
  - lib/wrapi/connection.rb
71
71
  - lib/wrapi/entity.rb
72
+ - lib/wrapi/pagination.rb
72
73
  - lib/wrapi/request.rb
73
74
  - lib/wrapi/respond_to.rb
74
75
  - lib/wrapi/version.rb