Class: TimeTree::Client
- Inherits:
-
Object
- Object
- TimeTree::Client
- Defined in:
- lib/timetree/client.rb
Overview
TimeTree apis client.
Constant Summary collapse
- API_HOST =
'https://timetreeapis.com'
Instance Attribute Summary collapse
- #ratelimit_limit ⇒ Integer readonly
- #ratelimit_remaining ⇒ Integer readonly
- #ratelimit_reset_at ⇒ Time readonly
- #token ⇒ String readonly
Instance Method Summary collapse
-
#calendar(cal_id, include_relationships: nil) ⇒ TimeTree::Calendar
Get a single calendar’s information.
-
#calendar_labels(cal_id) ⇒ Array<TimeTree::Label>
Get a calendar’s label information used in event.
-
#calendar_members(cal_id) ⇒ Array<TimeTree::User>
Get a calendar’s member information.
-
#calendars(include_relationships: nil) ⇒ Array<TimeTree::Calendar>
Get calendar list that current user can access.
-
#create_activity(cal_id, event_id, params) ⇒ TimeTree::Activity
Creates comment to an event.
-
#create_event(cal_id, params) ⇒ TimeTree::Event
Creates an event to the calendar.
-
#current_user ⇒ TimeTree::User
Get current user information.
-
#delete_event(cal_id, event_id) ⇒ true
Deletes an event.
-
#event(cal_id, event_id, include_relationships: nil) ⇒ TimeTree::Event
Get the event’s information.
-
#initialize(token = nil) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
-
#upcoming_events(cal_id, days: 7, timezone: 'UTC', include_relationships: nil) ⇒ Array<TimeTree::Event>
Get the events’ information after a request date.
-
#update_event(cal_id, event_id, params) ⇒ TimeTree::Event
Updates an event.
-
#update_ratelimit(res) ⇒ Object
update ratelimit properties.
Constructor Details
#initialize(token = nil) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 |
# File 'lib/timetree/client.rb', line 17 def initialize(token = nil) @token = token || TimeTree.configuration.token check_token @http_cmd = HttpCommand.new(API_HOST, self) end |
Instance Attribute Details
#ratelimit_limit ⇒ Integer (readonly)
10 11 12 |
# File 'lib/timetree/client.rb', line 10 def ratelimit_limit @ratelimit_limit end |
#ratelimit_remaining ⇒ Integer (readonly)
12 13 14 |
# File 'lib/timetree/client.rb', line 12 def ratelimit_remaining @ratelimit_remaining end |
#ratelimit_reset_at ⇒ Time (readonly)
14 15 16 |
# File 'lib/timetree/client.rb', line 14 def ratelimit_reset_at @ratelimit_reset_at end |
#token ⇒ String (readonly)
8 9 10 |
# File 'lib/timetree/client.rb', line 8 def token @token end |
Instance Method Details
#calendar(cal_id, include_relationships: nil) ⇒ TimeTree::Calendar
Get a single calendar’s information.
includes association’s object in the response.
46 47 48 49 50 51 52 53 |
# File 'lib/timetree/client.rb', line 46 def calendar(cal_id, include_relationships: nil) check_calendar_id cal_id params = relationships_params(include_relationships, Calendar::RELATIONSHIPS) res = @http_cmd.get "/calendars/#{cal_id}", params raise ApiError, res if res.status != 200 to_model(res.body[:data], included: res.body[:included]) end |
#calendar_labels(cal_id) ⇒ Array<TimeTree::Label>
Get a calendar’s label information used in event.
80 81 82 83 84 85 86 |
# File 'lib/timetree/client.rb', line 80 def calendar_labels(cal_id) check_calendar_id cal_id res = @http_cmd.get "/calendars/#{cal_id}/labels" raise ApiError, res if res.status != 200 res.body[:data].map { |item| to_model(item) } end |
#calendar_members(cal_id) ⇒ Array<TimeTree::User>
Get a calendar’s member information.
96 97 98 99 100 101 102 |
# File 'lib/timetree/client.rb', line 96 def calendar_members(cal_id) check_calendar_id cal_id res = @http_cmd.get "/calendars/#{cal_id}/members" raise ApiError, res if res.status != 200 res.body[:data].map { |item| to_model item } end |
#calendars(include_relationships: nil) ⇒ Array<TimeTree::Calendar>
Get calendar list that current user can access.
includes association’s object in the response.
63 64 65 66 67 68 69 70 |
# File 'lib/timetree/client.rb', line 63 def calendars(include_relationships: nil) params = relationships_params(include_relationships, Calendar::RELATIONSHIPS) res = @http_cmd.get '/calendars', params raise ApiError, res if res.status != 200 included = res.body[:included] res.body[:data].map { |item| to_model(item, included: included) } end |
#create_activity(cal_id, event_id, params) ⇒ TimeTree::Activity
Creates comment to an event.
comment’s information specified in TimeTree request body format.
228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/timetree/client.rb', line 228 def create_activity(cal_id, event_id, params) check_calendar_id cal_id check_event_id event_id res = @http_cmd.post "/calendars/#{cal_id}/events/#{event_id}/activities", params raise ApiError, res if res.status != 201 activity = to_model res.body[:data] activity.calendar_id = cal_id activity.event_id = event_id activity end |
#create_event(cal_id, params) ⇒ TimeTree::Event
Creates an event to the calendar.
164 165 166 167 168 169 170 171 172 |
# File 'lib/timetree/client.rb', line 164 def create_event(cal_id, params) check_calendar_id cal_id res = @http_cmd.post "/calendars/#{cal_id}/events", params raise ApiError, res if res.status != 201 ev = to_model res.body[:data] ev.calendar_id = cal_id ev end |
#current_user ⇒ TimeTree::User
Get current user information.
29 30 31 32 33 34 |
# File 'lib/timetree/client.rb', line 29 def current_user res = @http_cmd.get '/user' raise ApiError, res if res.status != 200 to_model res.body[:data] end |
#delete_event(cal_id, event_id) ⇒ true
Deletes an event.
207 208 209 210 211 212 213 214 |
# File 'lib/timetree/client.rb', line 207 def delete_event(cal_id, event_id) check_calendar_id cal_id check_event_id event_id res = @http_cmd.delete "/calendars/#{cal_id}/events/#{event_id}" raise ApiError, res if res.status != 204 true end |
#event(cal_id, event_id, include_relationships: nil) ⇒ TimeTree::Event
Get the event’s information.
includes association’s object in the response.
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/timetree/client.rb', line 116 def event(cal_id, event_id, include_relationships: nil) check_calendar_id cal_id check_event_id event_id params = relationships_params(include_relationships, Event::RELATIONSHIPS) res = @http_cmd.get "/calendars/#{cal_id}/events/#{event_id}", params raise ApiError, res if res.status != 200 ev = to_model(res.body[:data], included: res.body[:included]) ev.calendar_id = cal_id ev end |
#inspect ⇒ Object
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/timetree/client.rb', line 240 def inspect limit_info = nil if defined?(@ratelimit_limit) && @ratelimit_limit limit_info = " ratelimit:#{ratelimit_remaining}/#{ratelimit_limit}" end if defined?(@ratelimit_reset_at) && @ratelimit_reset_at limit_info = "#{limit_info}, reset_at:#{ratelimit_reset_at.strftime('%m/%d %R')}" end "\#<#{self.class}:#{object_id}#{limit_info}>" end |
#upcoming_events(cal_id, days: 7, timezone: 'UTC', include_relationships: nil) ⇒ Array<TimeTree::Event>
Get the events’ information after a request date.
includes association’s object in the response.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/timetree/client.rb', line 140 def upcoming_events(cal_id, days: 7, timezone: 'UTC', include_relationships: nil) check_calendar_id cal_id params = relationships_params(include_relationships, Event::RELATIONSHIPS) params.merge!(days: days, timezone: timezone) res = @http_cmd.get "/calendars/#{cal_id}/upcoming_events", params raise ApiError, res if res.status != 200 included = res.body[:included] res.body[:data].map do |item| ev = to_model(item, included: included) ev.calendar_id = cal_id ev end end |
#update_event(cal_id, event_id, params) ⇒ TimeTree::Event
Updates an event.
event’s information specified in TimeTree request body format.
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/timetree/client.rb', line 186 def update_event(cal_id, event_id, params) check_calendar_id cal_id check_event_id event_id res = @http_cmd.put "/calendars/#{cal_id}/events/#{event_id}", params raise ApiError, res if res.status != 200 ev = to_model res.body[:data] ev.calendar_id = cal_id ev end |
#update_ratelimit(res) ⇒ Object
update ratelimit properties
apis http response.
256 257 258 259 260 261 262 263 |
# File 'lib/timetree/client.rb', line 256 def update_ratelimit(res) limit = res.headers['x-ratelimit-limit'] remaining = res.headers['x-ratelimit-remaining'] reset = res.headers['x-ratelimit-reset'] @ratelimit_limit = limit.to_i if limit @ratelimit_remaining = remaining.to_i if remaining @ratelimit_reset_at = Time.at reset.to_i if reset end |