wrapi 0.1.2 → 0.2.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: ff558eb0e62fb544d7beadc13a7c0c87379b782b08ddc2965fb8bf2a72bd5949
4
- data.tar.gz: c3de6d2a2ffafdfaf924ebb7ad56bc4773eaff7892ed2e974a44456b43dd4a9d
3
+ metadata.gz: 7f1cf824802fe7b24c8df963fcf6598e650d4bcf8393f4d685cab9f4825838ca
4
+ data.tar.gz: 4f7b2f485cdbbcf3e2f10aa64fa0d562d789bb23aa2e6c47611bb7ff5a86db29
5
5
  SHA512:
6
- metadata.gz: 6c2b08f4aecd91f74d2f01939d95c712899b9b03c38bd5b5e01763b43143ea9754e2353916c64e4fdf159a9069fbf3e48310685a195ef050a4913e8f2a58dbac
7
- data.tar.gz: 26e5aa20ded0ed3701515afb906859121a9d6f4ba2ee978828918785c39c8216153a35420cdd163617d7734f1c2f612c554a89559613ad81c6ec4f1d455c54c1
6
+ metadata.gz: d601f302816a8e521bfd00985f04391a939c4f416a06e7b5accb0701a6c02109f7653b4be0ea8d7b12b191f867284959e0068a1b84495c082dd600c094636e2e
7
+ data.tar.gz: 16c72f9028b2060dc39b28a9c7037e3d2fba2ebe2a1079a2dbfe3eef2f9ed198ecc60c7dd4ae0c61a1cf79ddefe4638789b4e5072e07da36236b0cf4d3fc92f6
data/CHANGELOG.md CHANGED
@@ -5,3 +5,13 @@
5
5
 
6
6
  ## [0.1.1] - 2024-02-5
7
7
  - default endpoint to nil and raise error in connection when not available
8
+
9
+ ## [0.1.2] - 2024-02-5
10
+ - default endpoint to nil and raise error in connection when not available
11
+
12
+ ## [0.1.3] - 2024-02-5
13
+ - fix entity should return empty array insted of nil
14
+
15
+ ## [0.2.0] - 2024-02-8
16
+ - implement json pagination
17
+
@@ -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
data/lib/wrapi/entity.rb CHANGED
@@ -59,7 +59,7 @@ module WrAPI
59
59
  def entify(a)
60
60
  a.map do |item|
61
61
  item.is_a?(Hash) ? self.class.new(item) : item
62
- end if !a.empty? && a[0].is_a?(Hash)
62
+ end
63
63
  end
64
64
  end
65
65
  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 sdata 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
@@ -10,39 +9,26 @@ module WrAPI
10
9
  # Perform an HTTP GET request and return entity incase format is :json
11
10
  def get(path, options = {})
12
11
  response = request(:get, path, options)
13
- :json.eql?(format) ? Entity.new(response.body) : response.body
12
+ :json.eql?(format) ? Entity.new(pagination_class.data(response.body)) : response.body
14
13
  end
15
14
 
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": [] }
15
+ # Perform an HTTP GET request for paged date sets response
23
16
  def get_paged(path, options = {}, &block)
24
- raise! ArgumentError,
25
- "Pages requests should be json formatted (given format '#{format}')" unless :json.eql? format
17
+ raise ArgumentError,
18
+ "Pages requests should be json formatted (given format '#{format}')" unless :json.eql? format
26
19
 
27
20
  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) }
21
+ pager = create_pager
22
+ 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) }
38
26
  if block_given?
39
27
  yield(d)
40
28
  else
41
29
  result += d
42
30
  end
43
- page += 1
44
- total = data['totalPages'].to_i
45
- next_page = data['nextPageToken']
31
+ pager.next_page!(response.body)
46
32
  end
47
33
  result unless block_given?
48
34
  end
@@ -64,6 +50,10 @@ module WrAPI
64
50
 
65
51
  private
66
52
 
53
+ def create_pager
54
+ pagination_class.new(page_size)
55
+ end
56
+
67
57
  # Perform an HTTP request
68
58
  def request(method, path, options)
69
59
  response = connection.send(method) do |request|
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.2'
4
+ VERSION = '0.2.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.2
4
+ version: 0.2.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-08 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