aladtec 1.0.0.pre → 1.0.0.pre2

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: 8d1a55b6d0b8583820796f25b094c57789988fd579440f73f480a280efc0af25
4
- data.tar.gz: 429c1814e23f37a940ceeb68f2b725eee25324eeec012da717a82021b8382499
3
+ metadata.gz: 4bb6e0cb78ea7cc6446e1a62611ff179318d2ee0de1e94cbc1d29be51b3390d8
4
+ data.tar.gz: 719e483534d65051256f493bad7eb74632b735de7e2bb4985848ba1963fedc3e
5
5
  SHA512:
6
- metadata.gz: f28a90e45decd38cdd6b6fbc538deb738ffaeec8c2c32364eb69c25058e1165c1f85e355e4bcc486887a81568bec172870e9dc6b18cbe506054ee544df39320d
7
- data.tar.gz: 653004a2caa4814a61092dac892f6da1d36c67cb8135d83e2b0777cc90c7226f0d874139b8114bd9962abf2a5a1e9e5f8646e14d5f2eaefefb796af714780753
6
+ metadata.gz: ee5b6ecb37f26e1912447418e79489f5f33050705490fa3ca11061d19942ba7f593e9957fc7b18869a0a18604c20561ec757c7011b52e2ba7d64c0b3ce42c76c
7
+ data.tar.gz: a3dafeb15699a88737e5bd20ecc99d0d0af00b6d3baefe3d1fd6943718ea7549c5c74b9f28c7378a5487e1360a6ea8b1c205f9afbcab7f494fd5283e709cd9fb
@@ -13,9 +13,7 @@ module Aladtec
13
13
  class Client
14
14
  attr_reader :config
15
15
  def initialize(args = {})
16
- @config = Aladtec.config.dup.tap do |c|
17
- c.update(args)
18
- end
16
+ @config = Aladtec.config.dup.update(args)
19
17
  end
20
18
 
21
19
  def configure
@@ -52,11 +50,12 @@ module Aladtec
52
50
  events['data'].flat_map { |dates| dates['event_records'] }.map { |event| Event.new(event) }
53
51
  end
54
52
 
53
+ MAX = 1000
54
+
55
55
  # Public: Get a list of members
56
56
  #
57
57
  def members
58
- res = request('members')
59
- res['data'].map { |member| Member.new(member) }
58
+ paginated_get('members', {}, self).map { |member| Member.new(member) }
60
59
  end
61
60
 
62
61
  # Public: Get a list of schedules
@@ -89,8 +88,6 @@ module Aladtec
89
88
  scheduled_time['data'].flat_map { |date| date['time_records'] }.map { |range| Range.new(range) }
90
89
  end
91
90
 
92
- private
93
-
94
91
  def request(path, options = {})
95
92
  if auth_expired? && !authenticate
96
93
  raise Aladtec::Error, 'authentication failed'
@@ -103,6 +100,8 @@ module Aladtec
103
100
  res.parse
104
101
  end
105
102
 
103
+ private
104
+
106
105
  def format_time(time)
107
106
  (time.is_a?(Time) ? time : Time.parse(time)).strftime('%FT%T%:z')
108
107
  end
@@ -110,5 +109,52 @@ module Aladtec
110
109
  def auth_expired?
111
110
  !@auth_token || !@auth_expiration || Time.now >= @auth_expiration
112
111
  end
112
+
113
+ def paginated_get(path, params, client)
114
+ Cursor.new(path, params, client)
115
+ end
116
+
117
+ class Cursor
118
+ include Enumerable
119
+
120
+ def initialize(path, options = {}, client)
121
+ @path = path
122
+ @params = options.dup
123
+ @client = client
124
+
125
+ @collection = []
126
+ @offset = @params.fetch(:offset, nil)
127
+ @limit = @params.fetch(:limit, 50)
128
+ end
129
+
130
+ def each(start = 0, &block)
131
+ return to_enum(:each, start) unless block
132
+
133
+ Array(@collection[start..]).each(&block)
134
+
135
+ unless finished?
136
+ start = [@collection.size, start].max
137
+
138
+ fetch_next_page
139
+
140
+ each(start, &block)
141
+ end
142
+ end
143
+
144
+ private
145
+
146
+ def fetch_next_page
147
+ response = @client.request(@path, @params.merge(offset: @collection.length, limit: @limit))
148
+ data = response['data']
149
+ @last_response_empty = data.empty? || data.length < @limit
150
+ @collection += data
151
+ end
152
+
153
+ MAX = 1000
154
+
155
+ def finished?
156
+ @last_response_empty || @collection.length >= MAX
157
+ end
158
+ end
113
159
  end
114
160
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Aladtec
4
- VERSION = '1.0.0.pre'
4
+ VERSION = '1.0.0.pre2'
5
5
  end
@@ -23,6 +23,7 @@ describe Aladtec::Client do
23
23
  let(:members) { subject.members }
24
24
  before(:each) do
25
25
  stub_request(:get, URI.join(endpoint, 'members'))
26
+ .with(query: { offset: 0, limit: 50 })
26
27
  .to_return(body: fixture('members.json'),
27
28
  headers: { 'Content-Type': 'application/json' })
28
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aladtec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre
4
+ version: 1.0.0.pre2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Dahlke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-31 00:00:00.000000000 Z
11
+ date: 2024-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler