timetree 0.3.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TimeTree
4
- VERSION = '0.3.2'
4
+ VERSION = '1.0.0'
5
5
  end
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
30
30
 
31
31
  spec.add_runtime_dependency 'faraday', '>= 1.0.1'
32
32
  spec.add_runtime_dependency 'faraday_middleware', '>= 1.0.0'
33
+ spec.add_runtime_dependency 'jwt', '>= 2.2.2'
33
34
 
34
35
  spec.add_development_dependency 'bundler'
35
36
  spec.add_development_dependency 'codecov'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timetree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenji Koshikawa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-18 00:00:00.000000000 Z
11
+ date: 2020-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: jwt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.2.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.2.2
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -143,15 +157,19 @@ files:
143
157
  - bin/setup
144
158
  - lib/timetree.rb
145
159
  - lib/timetree/api_error.rb
146
- - lib/timetree/client.rb
160
+ - lib/timetree/base_client.rb
161
+ - lib/timetree/calendar_app/access_token.rb
162
+ - lib/timetree/calendar_app/client.rb
147
163
  - lib/timetree/configuration.rb
148
164
  - lib/timetree/http_command.rb
149
165
  - lib/timetree/models/activity.rb
166
+ - lib/timetree/models/application.rb
150
167
  - lib/timetree/models/base_model.rb
151
168
  - lib/timetree/models/calendar.rb
152
169
  - lib/timetree/models/event.rb
153
170
  - lib/timetree/models/label.rb
154
171
  - lib/timetree/models/user.rb
172
+ - lib/timetree/oauth_app/client.rb
155
173
  - lib/timetree/version.rb
156
174
  - timetree.gemspec
157
175
  homepage: https://github.com/koshilife/timetree-api-ruby-client
@@ -161,7 +179,7 @@ metadata:
161
179
  homepage_uri: https://github.com/koshilife/timetree-api-ruby-client
162
180
  source_code_uri: https://github.com/koshilife/timetree-api-ruby-client
163
181
  changelog_uri: https://github.com/koshilife/timetree-api-ruby-client/blob/master/CHANGELOG.md
164
- documentation_uri: https://www.rubydoc.info/gems/timetree/0.3.2
182
+ documentation_uri: https://www.rubydoc.info/gems/timetree/1.0.0
165
183
  post_install_message:
166
184
  rdoc_options: []
167
185
  require_paths:
@@ -1,298 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TimeTree
4
- # TimeTree apis client.
5
- class Client
6
- API_HOST = 'https://timetreeapis.com'
7
- # @return [String]
8
- attr_reader :token
9
- # @return [Integer]
10
- attr_reader :ratelimit_limit
11
- # @return [Integer]
12
- attr_reader :ratelimit_remaining
13
- # @return [Time]
14
- attr_reader :ratelimit_reset_at
15
-
16
- # @param token [String] a TimeTree's access token.
17
- def initialize(token = nil)
18
- @token = token || TimeTree.configuration.token
19
- check_token
20
- @http_cmd = HttpCommand.new(API_HOST, self)
21
- end
22
-
23
- #
24
- # Get current user information.
25
- #
26
- # @return [TimeTree::User]
27
- # @raise [TimeTree::ApiError] if the http response status will not success.
28
- # @since 0.0.1
29
- def current_user
30
- res = @http_cmd.get '/user'
31
- raise ApiError.new(res) if res.status != 200
32
-
33
- to_model res.body[:data]
34
- end
35
-
36
- #
37
- # Get a single calendar's information.
38
- #
39
- # @param cal_id [String] calendar's id.
40
- # @param include_relationships [Array<symbol>]
41
- # includes association's object in the response.
42
- # @return [TimeTree::Calendar]
43
- # @raise [TimeTree::Error] if the cal_id arg is empty.
44
- # @raise [TimeTree::ApiError] if the http response status will not success.
45
- # @since 0.0.1
46
- def calendar(cal_id, include_relationships: nil)
47
- check_calendar_id cal_id
48
- params = relationships_params(include_relationships, Calendar::RELATIONSHIPS)
49
- res = @http_cmd.get "/calendars/#{cal_id}", params
50
- raise ApiError.new(res) if res.status != 200
51
-
52
- to_model(res.body[:data], included: res.body[:included])
53
- end
54
-
55
- #
56
- # Get calendar list that current user can access.
57
- #
58
- # @param include_relationships [Array<symbol>]
59
- # includes association's object in the response.
60
- # @return [Array<TimeTree::Calendar>]
61
- # @raise [TimeTree::ApiError] if the http response status will not success.
62
- # @since 0.0.1
63
- def calendars(include_relationships: nil)
64
- params = relationships_params(include_relationships, Calendar::RELATIONSHIPS)
65
- res = @http_cmd.get '/calendars', params
66
- raise ApiError.new(res) if res.status != 200
67
-
68
- included = res.body[:included]
69
- res.body[:data].map { |item| to_model(item, included: included) }
70
- end
71
-
72
- #
73
- # Get a calendar's label information used in event.
74
- #
75
- # @param cal_id [String] calendar's id.
76
- # @return [Array<TimeTree::Label>]
77
- # @raise [TimeTree::Error] if the cal_id arg is empty.
78
- # @raise [TimeTree::ApiError] if the http response status will not success.
79
- # @since 0.0.1
80
- def calendar_labels(cal_id)
81
- check_calendar_id cal_id
82
- res = @http_cmd.get "/calendars/#{cal_id}/labels"
83
- raise ApiError.new(res) if res.status != 200
84
-
85
- res.body[:data].map { |item| to_model(item) }
86
- end
87
-
88
- #
89
- # Get a calendar's member information.
90
- #
91
- # @param cal_id [String] calendar's id.
92
- # @return [Array<TimeTree::User>]
93
- # @raise [TimeTree::Error] if the cal_id arg is empty.
94
- # @raise [TimeTree::ApiError] if the http response status will not success.
95
- # @since 0.0.1
96
- def calendar_members(cal_id)
97
- check_calendar_id cal_id
98
- res = @http_cmd.get "/calendars/#{cal_id}/members"
99
- raise ApiError.new(res) if res.status != 200
100
-
101
- res.body[:data].map { |item| to_model item }
102
- end
103
-
104
- #
105
- # Get the event's information.
106
- #
107
- # @param cal_id [String] calendar's id.
108
- # @param event_id [String] event's id.
109
- # @param include_relationships [Array<symbol>]
110
- # includes association's object in the response.
111
- # @return [TimeTree::Event]
112
- # @raise [TimeTree::Error] if the cal_id arg is empty.
113
- # @raise [TimeTree::Error] if the event_id arg is empty.
114
- # @raise [TimeTree::ApiError] if the http response status will not success.
115
- # @since 0.0.1
116
- def event(cal_id, event_id, include_relationships: nil)
117
- check_calendar_id cal_id
118
- check_event_id event_id
119
- params = relationships_params(include_relationships, Event::RELATIONSHIPS)
120
- res = @http_cmd.get "/calendars/#{cal_id}/events/#{event_id}", params
121
- raise ApiError.new(res) if res.status != 200
122
-
123
- ev = to_model(res.body[:data], included: res.body[:included])
124
- ev.calendar_id = cal_id
125
- ev
126
- end
127
-
128
- #
129
- # Get the events' information after a request date.
130
- #
131
- # @param cal_id[String] calendar's id.
132
- # @param days [Integer] The number of days to get.
133
- # @param timezone [String] Timezone.
134
- # @param include_relationships [Array<symbol>]
135
- # includes association's object in the response.
136
- # @return [Array<TimeTree::Event>]
137
- # @raise [TimeTree::Error] if the cal_id arg is empty.
138
- # @raise [TimeTree::ApiError] if the http response status will not success.
139
- # @since 0.0.1
140
- def upcoming_events(cal_id, days: 7, timezone: 'UTC', include_relationships: nil)
141
- check_calendar_id cal_id
142
- params = relationships_params(include_relationships, Event::RELATIONSHIPS)
143
- params.merge!(days: days, timezone: timezone)
144
- res = @http_cmd.get "/calendars/#{cal_id}/upcoming_events", params
145
- raise ApiError.new(res) if res.status != 200
146
-
147
- included = res.body[:included]
148
- res.body[:data].map do |item|
149
- ev = to_model(item, included: included)
150
- ev.calendar_id = cal_id
151
- ev
152
- end
153
- end
154
-
155
- #
156
- # Creates an event to the calendar.
157
- #
158
- # @param cal_id [String] calendar's id.
159
- # @param params [Hash] TimeTree request body format.
160
- # @return [TimeTree::Event]
161
- # @raise [TimeTree::Error] if the cal_id arg is empty.
162
- # @raise [TimeTree::ApiError] if the http response status will not success.
163
- # @since 0.0.1
164
- def create_event(cal_id, params)
165
- check_calendar_id cal_id
166
- res = @http_cmd.post "/calendars/#{cal_id}/events", params
167
- raise ApiError.new(res) if res.status != 201
168
-
169
- ev = to_model res.body[:data]
170
- ev.calendar_id = cal_id
171
- ev
172
- end
173
-
174
- #
175
- # Updates an event.
176
- #
177
- # @param cal_id [String] calendar's id.
178
- # @param event_id [String] event's id.
179
- # @param params [Hash]
180
- # event's information specified in TimeTree request body format.
181
- # @return [TimeTree::Event]
182
- # @raise [TimeTree::Error] if the cal_id arg is empty.
183
- # @raise [TimeTree::Error] if the event_id arg is empty.
184
- # @raise [TimeTree::ApiError] if the http response status will not success.
185
- # @since 0.0.1
186
- def update_event(cal_id, event_id, params)
187
- check_calendar_id cal_id
188
- check_event_id event_id
189
- res = @http_cmd.put "/calendars/#{cal_id}/events/#{event_id}", params
190
- raise ApiError.new(res) if res.status != 200
191
-
192
- ev = to_model res.body[:data]
193
- ev.calendar_id = cal_id
194
- ev
195
- end
196
-
197
- #
198
- # Deletes an event.
199
- #
200
- # @param cal_id [String] calendar's id.
201
- # @param event_id [String] event's id.
202
- # @return [true] if the operation succeeded.
203
- # @raise [TimeTree::Error] if the cal_id arg is empty.
204
- # @raise [TimeTree::Error] if the event_id arg is empty.
205
- # @raise [TimeTree::ApiError] if the http response status will not success.
206
- # @since 0.0.1
207
- def delete_event(cal_id, event_id)
208
- check_calendar_id cal_id
209
- check_event_id event_id
210
- res = @http_cmd.delete "/calendars/#{cal_id}/events/#{event_id}"
211
- raise ApiError.new(res) if res.status != 204
212
-
213
- true
214
- end
215
-
216
- #
217
- # Creates comment to an event.
218
- #
219
- # @param cal_id [String] calendar's id.
220
- # @param event_id [String] event's id.
221
- # @param params [Hash]
222
- # comment's information specified in TimeTree request body format.
223
- # @return [TimeTree::Activity]
224
- # @raise [TimeTree::Error] if the cal_id arg is empty.
225
- # @raise [TimeTree::Error] if the event_id arg is empty.
226
- # @raise [TimeTree::ApiError] if the http response status is not success.
227
- # @since 0.0.1
228
- def create_activity(cal_id, event_id, params)
229
- check_calendar_id cal_id
230
- check_event_id event_id
231
- res = @http_cmd.post "/calendars/#{cal_id}/events/#{event_id}/activities", params
232
- raise ApiError.new(res) if res.status != 201
233
-
234
- activity = to_model res.body[:data]
235
- activity.calendar_id = cal_id
236
- activity.event_id = event_id
237
- activity
238
- end
239
-
240
- def inspect
241
- limit_info = nil
242
- if defined?(@ratelimit_limit) && @ratelimit_limit
243
- limit_info = " ratelimit:#{ratelimit_remaining}/#{ratelimit_limit}"
244
- end
245
- if defined?(@ratelimit_reset_at) && @ratelimit_reset_at
246
- limit_info = "#{limit_info}, reset_at:#{ratelimit_reset_at.strftime('%m/%d %R')}"
247
- end
248
- "\#<#{self.class}:#{object_id}#{limit_info}>"
249
- end
250
-
251
- #
252
- # update ratelimit properties
253
- #
254
- # @param res [Faraday::Response]
255
- # apis http response.
256
- def update_ratelimit(res)
257
- limit = res.headers['x-ratelimit-limit']
258
- remaining = res.headers['x-ratelimit-remaining']
259
- reset = res.headers['x-ratelimit-reset']
260
- @ratelimit_limit = limit.to_i if limit
261
- @ratelimit_remaining = remaining.to_i if remaining
262
- @ratelimit_reset_at = Time.at reset.to_i if reset
263
- end
264
-
265
- private
266
-
267
- def check_token
268
- check_required_property(@token, 'token')
269
- end
270
-
271
- def check_calendar_id(value)
272
- check_required_property(value, 'calendar_id')
273
- end
274
-
275
- def check_event_id(value)
276
- check_required_property(value, 'event_id')
277
- end
278
-
279
- def check_required_property(value, name)
280
- err = Error.new "#{name} is required."
281
- raise err if value.nil?
282
- raise err if value.to_s.empty?
283
-
284
- true
285
- end
286
-
287
- def to_model(data, included: nil)
288
- TimeTree::BaseModel.to_model data, client: self, included: included
289
- end
290
-
291
- def relationships_params(relationships, default)
292
- params = {}
293
- relationships ||= default
294
- params[:include] = relationships.join ',' if relationships.is_a? Array
295
- params
296
- end
297
- end
298
- end