Class: TimeTree::CalendarApp::Client

Inherits:
BaseClient show all
Defined in:
lib/timetree/calendar_app/client.rb

Overview

TimeTree API CalendarApp client.

Constant Summary

Constants inherited from BaseClient

BaseClient::API_HOST

Instance Attribute Summary collapse

Attributes inherited from BaseClient

#ratelimit_limit, #ratelimit_remaining, #ratelimit_reset_at

Instance Method Summary collapse

Methods inherited from BaseClient

#update_ratelimit

Constructor Details

#initialize(installation_id, application_id = nil, private_key = nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • installation_id (Integer)

    CalendarApp’s installation id

  • application_id (String) (defaults to: nil)

    CalendarApp id

  • private_key (String) (defaults to: nil)

    RSA private key for CalendarApp



22
23
24
25
26
27
28
29
30
# File 'lib/timetree/calendar_app/client.rb', line 22

def initialize(installation_id, application_id = nil, private_key = nil)
  @installation_id = installation_id
  @application_id = application_id || TimeTree.configuration.calendar_app_application_id
  @private_key = OpenSSL::PKey::RSA.new((private_key || TimeTree.configuration.calendar_app_private_key).to_s)
  check_client_requirement
  @http_cmd = HttpCommand.new(API_HOST, self)
rescue OpenSSL::PKey::RSAError
  raise Error.new 'private_key must be RSA private key.'
end

Instance Attribute Details

#application_idString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/timetree/calendar_app/client.rb', line 13

def application_id
  @application_id
end

#installation_idInteger (readonly)

Returns:

  • (Integer)


11
12
13
# File 'lib/timetree/calendar_app/client.rb', line 11

def installation_id
  @installation_id
end

#private_keyString (readonly)

Returns:

  • (String)


15
16
17
# File 'lib/timetree/calendar_app/client.rb', line 15

def private_key
  @private_key
end

#tokenString (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/timetree/calendar_app/client.rb', line 17

def token
  @token
end

Instance Method Details

#calendar(include_relationships: nil) ⇒ TimeTree::Calendar

Get a calendar information related to CalendarApp

includes association’s object in the response.

Parameters:

  • include_relationships (Array<symbol>) (defaults to: nil)

Returns:

Raises:

Since:

  • 1.0.0



40
41
42
43
44
45
46
47
# File 'lib/timetree/calendar_app/client.rb', line 40

def calendar(include_relationships: nil)
  check_access_token
  params = relationships_params(include_relationships, Calendar::RELATIONSHIPS)
  res = http_cmd.get('/calendar', params)
  raise ApiError.new(res) if res.status != 200

  to_model(res.body[:data], included: res.body[:included])
end

#calendar_membersArray<TimeTree::User>

Get a calendar’s member information.

Returns:

Raises:

Since:

  • 1.0.0



55
56
57
58
59
60
61
# File 'lib/timetree/calendar_app/client.rb', line 55

def calendar_members
  check_access_token
  res = http_cmd.get('/calendar/members')
  raise ApiError.new(res) if res.status != 200

  res.body[:data].map { |item| to_model(item) }
end

#create_activity(event_id, params) ⇒ TimeTree::Activity

Creates a comment.

comment’s information specified in TimeTree request body format.

Parameters:

  • event_id (String)

    event’s id.

  • params (Hash)

Returns:

Raises:

Since:

  • 1.0.0



166
167
168
169
170
171
172
173
174
175
# File 'lib/timetree/calendar_app/client.rb', line 166

def create_activity(event_id, params)
  check_event_id event_id
  check_access_token
  res = http_cmd.post("/calendar/events/#{event_id}/activities", params)
  raise ApiError.new(res) if res.status != 201

  activity = to_model(res.body[:data])
  activity.event_id = event_id
  activity
end

#create_event(params) ⇒ TimeTree::Event

Creates an event.

Parameters:

  • params (Hash)

    TimeTree request body format.

Returns:

Raises:

Since:

  • 1.0.0



112
113
114
115
116
117
118
# File 'lib/timetree/calendar_app/client.rb', line 112

def create_event(params)
  check_access_token
  res = http_cmd.post('/calendar/events', params)
  raise ApiError.new(res) if res.status != 201

  to_model(res.body[:data])
end

#delete_event(event_id) ⇒ true

Deletes an event.

Parameters:

  • event_id (String)

    event’s id.

Returns:

  • (true)

    if the operation succeeded.

Raises:

Since:

  • 1.0.0



147
148
149
150
151
152
153
154
# File 'lib/timetree/calendar_app/client.rb', line 147

def delete_event(event_id)
  check_event_id event_id
  check_access_token
  res = http_cmd.delete("/calendar/events/#{event_id}")
  raise ApiError.new(res) if res.status != 204

  true
end

#event(event_id, include_relationships: nil) ⇒ TimeTree::Event

Get an event’s information.

includes association’s object in the response.

Parameters:

  • event_id (String)

    event’s id.

  • include_relationships (Array<symbol>) (defaults to: nil)

Returns:

Raises:

Since:

  • 1.0.0



73
74
75
76
77
78
79
80
81
# File 'lib/timetree/calendar_app/client.rb', line 73

def event(event_id, include_relationships: nil)
  check_event_id event_id
  check_access_token
  params = relationships_params(include_relationships, Event::RELATIONSHIPS)
  res = http_cmd.get("/calendar/events/#{event_id}", params)
  raise ApiError.new(res) if res.status != 200

  to_model(res.body[:data], included: res.body[:included])
end

#inspectObject



177
178
179
180
181
182
183
184
185
186
# File 'lib/timetree/calendar_app/client.rb', line 177

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(days: 7, timezone: 'UTC', include_relationships: nil) ⇒ Array<TimeTree::Event>

Get events’ information after a request date.

includes association’s object in the response.

Parameters:

  • days (Integer) (defaults to: 7)

    The number of days to get.

  • timezone (String) (defaults to: 'UTC')

    Timezone.

  • include_relationships (Array<symbol>) (defaults to: nil)

Returns:

Raises:

Since:

  • 1.0.0



93
94
95
96
97
98
99
100
101
102
# File 'lib/timetree/calendar_app/client.rb', line 93

def upcoming_events(days: 7, timezone: 'UTC', include_relationships: nil)
  check_access_token
  params = relationships_params(include_relationships, Event::RELATIONSHIPS)
  params.merge!(days: days, timezone: timezone)
  res = http_cmd.get('/calendar/upcoming_events', params)
  raise ApiError.new(res) if res.status != 200

  included = res.body[:included]
  res.body[:data].map { |item| to_model(item, included: included) }
end

#update_event(event_id, params) ⇒ TimeTree::Event

Updates an event.

event’s information specified in TimeTree request body format.

Parameters:

  • event_id (String)

    event’s id.

  • params (Hash)

Returns:

Raises:

Since:

  • 1.0.0



130
131
132
133
134
135
136
137
# File 'lib/timetree/calendar_app/client.rb', line 130

def update_event(event_id, params)
  check_event_id event_id
  check_access_token
  res = http_cmd.put("/calendar/events/#{event_id}", params)
  raise ApiError.new(res) if res.status != 200

  to_model(res.body[:data])
end