timetree 0.2.0 → 1.0.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.
@@ -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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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