lib/timetree/client.rb in timetree-0.1.2 vs lib/timetree/client.rb in timetree-0.1.3

- old
+ new

@@ -14,12 +14,11 @@ attr_reader :ratelimit_reset_at # @param token [String] a TimeTree's access token. def initialize(token = nil) @token = token || TimeTree.configuration.token - raise Error, 'token is required.' unless ready_token? - + check_token @http_cmd = HttpCommand.new(API_HOST, self) end # # Get current user information. @@ -42,10 +41,11 @@ # includes association's object in the response. # @return [TimeTree::Calendar] # @raise [TimeTree::ApiError] if the http response status will not success. # @since 0.0.1 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]) @@ -74,10 +74,11 @@ # @param cal_id [String] calendar's id. # @return [Array<TimeTree::Label>] # @raise [TimeTree::ApiError] if the http response status will not success. # @since 0.0.1 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 @@ -88,10 +89,11 @@ # @param cal_id [String] calendar's id. # @return [Array<TimeTree::User>] # @raise [TimeTree::ApiError] if the http response status will not success. # @since 0.0.1 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 @@ -105,10 +107,12 @@ # includes association's object in the response. # @return [TimeTree::Event] # @raise [TimeTree::ApiError] if the http response status will not success. # @since 0.0.1 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]) @@ -126,10 +130,11 @@ # includes association's object in the response. # @return [Array<TimeTree::Event>] # @raise [TimeTree::ApiError] if the http response status will not success. # @since 0.0.1 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 @@ -148,10 +153,11 @@ # @param params [Hash] TimeTree request body format. # @return [TimeTree::Event] # @raise [TimeTree::ApiError] if the http response status will not success. # @since 0.0.1 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 @@ -167,10 +173,12 @@ # event's information specified in TimeTree request body format. # @return [TimeTree::Event] # @raise [TimeTree::ApiError] if the http response status will not success. # @since 0.0.1 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 @@ -184,10 +192,12 @@ # @param event_id [String] event's id. # @return [true] if the operation succeeded. # @raise [TimeTree::ApiError] if the http response status will not success. # @since 0.0.1 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 @@ -201,10 +211,12 @@ # comment's information specified in TimeTree request body format. # @return [TimeTree::Activity] # @raise [TimeTree::ApiError] if the nhttp response status is not success. # @since 0.0.1 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 @@ -212,14 +224,14 @@ activity end def inspect limit_info = nil - if @ratelimit_limit + if defined?(@ratelimit_limit) && @ratelimit_limit limit_info = " ratelimit:#{@ratelimit_remaining}/#{@ratelimit_limit}" end - if @ratelimit_reset_at + 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 @@ -237,15 +249,31 @@ @ratelimit_reset_at = Time.at reset.to_i if reset end private - def to_model(data, included: nil) - TimeTree::BaseModel.to_model data, client: self, included: included + def check_token + check_required_property(@token, 'token') end - def ready_token? - @token.is_a?(String) && !@token.empty? + def check_calendar_id(value) + check_required_property(value, 'calendar_id') + end + + def check_event_id(value) + check_required_property(value, 'event_id') + end + + def check_required_property(value, name) + err = Error.new "#{name} is required." + raise err if value.nil? + raise err if value.to_s.empty? + + true + end + + def to_model(data, included: nil) + TimeTree::BaseModel.to_model data, client: self, included: included end def relationships_params(relationships, default) params = {} relationships ||= default