diff --git a/lib/calendlyr.rb b/lib/calendlyr.rb index f5d08be..74d1f90 100644 --- a/lib/calendlyr.rb +++ b/lib/calendlyr.rb @@ -9,27 +9,28 @@ module Calendlyr # Errors autoload :BadRequest, "calendlyr/error" autoload :Error, "calendlyr/error" - autoload :ExternalCalendarEror, "calendlyr/error" + autoload :ExternalCalendarError, "calendlyr/error" autoload :InternalServerError, "calendlyr/error" autoload :NotFound, "calendlyr/error" autoload :PaymentRequired, "calendlyr/error" autoload :PermissionDenied, "calendlyr/error" - autoload :ResponseErrorHandler, "calendlyr/error" autoload :Unauthenticated, "calendlyr/error" + autoload :TooManyRequests, "calendlyr/error" + autoload :ResponseErrorHandler, "calendlyr/error" # High-level categories of Calendly API calls autoload :AvailabilityResource, "calendlyr/resources/availability" autoload :DataComplianceResource, "calendlyr/resources/data_compliance" - autoload :EventResource, "calendlyr/resources/event" - autoload :EventTypeResource, "calendlyr/resources/event_type" - autoload :GroupResource, "calendlyr/resources/group" - autoload :OrganizationResource, "calendlyr/resources/organization" - autoload :OutgoingCommunicationResource, "calendlyr/resources/outgoing_communication" - autoload :RoutingFormResource, "calendlyr/resources/routing_form" - autoload :SchedulingLinkResource, "calendlyr/resources/scheduling_link" - autoload :ShareResource, "calendlyr/resources/share" - autoload :UserResource, "calendlyr/resources/user" - autoload :WebhookResource, "calendlyr/resources/webhook" + autoload :EventsResource, "calendlyr/resources/events" + autoload :EventTypesResource, "calendlyr/resources/event_types" + autoload :GroupsResource, "calendlyr/resources/groups" + autoload :OrganizationsResource, "calendlyr/resources/organizations" + autoload :OutgoingCommunicationsResource, "calendlyr/resources/outgoing_communications" + autoload :RoutingFormsResource, "calendlyr/resources/routing_forms" + autoload :SchedulingLinksResource, "calendlyr/resources/scheduling_links" + autoload :SharesResource, "calendlyr/resources/shares" + autoload :UsersResource, "calendlyr/resources/users" + autoload :WebhooksResource, "calendlyr/resources/webhooks" # Classes used to return a nicer object wrapping the response data autoload :ActivityLog, "calendlyr/objects/activity_log" diff --git a/lib/calendlyr/client.rb b/lib/calendlyr/client.rb index 1745c7f..f78a6ac 100644 --- a/lib/calendlyr/client.rb +++ b/lib/calendlyr/client.rb @@ -8,10 +8,6 @@ def initialize(token:) @token = token end - def availability - AvailabilityResource.new(self) - end - def me(force_reload: false) @me = nil if force_reload @me ||= users.me @@ -21,48 +17,23 @@ def organization me.organization end - def users - UserResource.new(self) - end - - def organizations - OrganizationResource.new(self) - end - - def event_types - EventTypeResource.new(self) - end - - def events - EventResource.new(self) - end - - def groups - GroupResource.new(self) + # Given a client.users, method_missing behaves like this: + # def users + # UsersResource.new(self) + # end + def method_missing(method_name, *args, &block) + resource_name = method_name.to_s.split("_").collect(&:capitalize).join + "Resource" + if Calendlyr.const_defined?(resource_name) + Calendlyr.const_get(resource_name).new(self) + else + super + end end - def outgoing_communications - OutgoingCommunicationResource.new(self) - end - - def routing_forms - RoutingFormResource.new(self) - end - - def scheduling_links - SchedulingLinkResource.new(self) - end - - def shares - ShareResource.new(self) - end - - def webhooks - WebhookResource.new(self) - end + def respond_to_missing?(method_name, include_private = false) + resource_name = method_name.to_s.split("_").collect(&:capitalize).join + "Resource" - def data_compliance - DataComplianceResource.new(self) + Calendlyr.const_defined?(resource_name) || super end # Avoid returning # diff --git a/lib/calendlyr/resources/event_type.rb b/lib/calendlyr/resources/event_types.rb similarity index 97% rename from lib/calendlyr/resources/event_type.rb rename to lib/calendlyr/resources/event_types.rb index 4b6c2b9..d73dac1 100644 --- a/lib/calendlyr/resources/event_type.rb +++ b/lib/calendlyr/resources/event_types.rb @@ -1,5 +1,5 @@ module Calendlyr - class EventTypeResource < Resource + class EventTypesResource < Resource def list(**params) response = get_request("event_types", params: params) Collection.from_response(response, type: EventType, client: client) diff --git a/lib/calendlyr/resources/event.rb b/lib/calendlyr/resources/events.rb similarity index 97% rename from lib/calendlyr/resources/event.rb rename to lib/calendlyr/resources/events.rb index 1f29ff0..9f2866f 100644 --- a/lib/calendlyr/resources/event.rb +++ b/lib/calendlyr/resources/events.rb @@ -1,5 +1,5 @@ module Calendlyr - class EventResource < Resource + class EventsResource < Resource def list(**params) response = get_request("scheduled_events", params: params) Collection.from_response(response, type: Event, client: client) diff --git a/lib/calendlyr/resources/group.rb b/lib/calendlyr/resources/groups.rb similarity index 95% rename from lib/calendlyr/resources/group.rb rename to lib/calendlyr/resources/groups.rb index 6d71dbd..f8cbfc9 100644 --- a/lib/calendlyr/resources/group.rb +++ b/lib/calendlyr/resources/groups.rb @@ -1,5 +1,5 @@ module Calendlyr - class GroupResource < Resource + class GroupsResource < Resource def list(organization:, **params) response = get_request("groups", params: params.merge(organization: organization)) Collection.from_response(response, type: Group, client: client) diff --git a/lib/calendlyr/resources/organization.rb b/lib/calendlyr/resources/organizations.rb similarity index 97% rename from lib/calendlyr/resources/organization.rb rename to lib/calendlyr/resources/organizations.rb index 9ba64be..817bbff 100644 --- a/lib/calendlyr/resources/organization.rb +++ b/lib/calendlyr/resources/organizations.rb @@ -1,5 +1,5 @@ module Calendlyr - class OrganizationResource < Resource + class OrganizationsResource < Resource def activity_log(organization: nil, **params) response = get_request("activity_log_entries", params: {organization: organization}.merge(params).compact) Collection.from_response(response, type: ActivityLog, client: client) diff --git a/lib/calendlyr/resources/outgoing_communication.rb b/lib/calendlyr/resources/outgoing_communications.rb similarity index 80% rename from lib/calendlyr/resources/outgoing_communication.rb rename to lib/calendlyr/resources/outgoing_communications.rb index 279f16f..07db536 100644 --- a/lib/calendlyr/resources/outgoing_communication.rb +++ b/lib/calendlyr/resources/outgoing_communications.rb @@ -1,5 +1,5 @@ module Calendlyr - class OutgoingCommunicationResource < Resource + class OutgoingCommunicationsResource < Resource def list(**params) response = get_request("outgoing_communications", params: params) Collection.from_response(response, type: Object, client: client) diff --git a/lib/calendlyr/resources/routing_form.rb b/lib/calendlyr/resources/routing_forms.rb similarity index 95% rename from lib/calendlyr/resources/routing_form.rb rename to lib/calendlyr/resources/routing_forms.rb index 57c3a42..e94769e 100644 --- a/lib/calendlyr/resources/routing_form.rb +++ b/lib/calendlyr/resources/routing_forms.rb @@ -1,5 +1,5 @@ module Calendlyr - class RoutingFormResource < Resource + class RoutingFormsResource < Resource def list(organization:, **params) response = get_request("routing_forms", params: {organization: organization}.merge(params)) Collection.from_response(response, type: RoutingForm, client: client) diff --git a/lib/calendlyr/resources/scheduling_link.rb b/lib/calendlyr/resources/scheduling_links.rb similarity index 87% rename from lib/calendlyr/resources/scheduling_link.rb rename to lib/calendlyr/resources/scheduling_links.rb index 716762e..b3bd3d6 100644 --- a/lib/calendlyr/resources/scheduling_link.rb +++ b/lib/calendlyr/resources/scheduling_links.rb @@ -1,5 +1,5 @@ module Calendlyr - class SchedulingLinkResource < Resource + class SchedulingLinksResource < Resource def create(owner:, max_event_count:, owner_type: "EventType") body = {owner: owner, max_event_count: max_event_count, owner_type: owner_type} SchedulingLink.new post_request("scheduling_links", body: body).dig("resource").merge(client: client) diff --git a/lib/calendlyr/resources/share.rb b/lib/calendlyr/resources/shares.rb similarity index 86% rename from lib/calendlyr/resources/share.rb rename to lib/calendlyr/resources/shares.rb index 0c45812..953d7c6 100644 --- a/lib/calendlyr/resources/share.rb +++ b/lib/calendlyr/resources/shares.rb @@ -1,5 +1,5 @@ module Calendlyr - class ShareResource < Resource + class SharesResource < Resource def create(event_type:, **params) body = {event_type: event_type}.merge(params) Share.new post_request("shares", body: body).dig("resource").merge(client: client) diff --git a/lib/calendlyr/resources/user.rb b/lib/calendlyr/resources/users.rb similarity index 85% rename from lib/calendlyr/resources/user.rb rename to lib/calendlyr/resources/users.rb index 75ebf60..9a5df7e 100644 --- a/lib/calendlyr/resources/user.rb +++ b/lib/calendlyr/resources/users.rb @@ -1,5 +1,5 @@ module Calendlyr - class UserResource < Resource + class UsersResource < Resource def me retrieve(uuid: "me") end diff --git a/lib/calendlyr/resources/webhook.rb b/lib/calendlyr/resources/webhooks.rb similarity index 96% rename from lib/calendlyr/resources/webhook.rb rename to lib/calendlyr/resources/webhooks.rb index 115802e..608f2d6 100644 --- a/lib/calendlyr/resources/webhook.rb +++ b/lib/calendlyr/resources/webhooks.rb @@ -1,5 +1,5 @@ module Calendlyr - class WebhookResource < Resource + class WebhooksResource < Resource def list(organization:, scope:, **params) response = get_request("webhook_subscriptions", params: params.merge(organization: organization, scope: scope).compact) Collection.from_response(response, type: Webhooks::Subscription, client: client) diff --git a/test/calendlyr/client_test.rb b/test/calendlyr/client_test.rb index d8e9f8b..b042e3a 100644 --- a/test/calendlyr/client_test.rb +++ b/test/calendlyr/client_test.rb @@ -8,4 +8,15 @@ def test_token def test_inspect assert_equal "#", client.inspect end + + def test_method_missing + assert_raises NoMethodError do + client.useers + end + end + + def test_respond_to_missing? + assert client.respond_to?(:users) + refute client.respond_to?(:useers) + end end diff --git a/test/calendlyr/resource_test.rb b/test/calendlyr/resource_test.rb index 68555d6..b6ea949 100644 --- a/test/calendlyr/resource_test.rb +++ b/test/calendlyr/resource_test.rb @@ -20,4 +20,12 @@ def test_handle_response_error_payment client.me end end + + def test_handle_response_too_many_requests + stub(path: "users/me", response: {body: "", status: 429}) + + assert_raises Calendlyr::TooManyRequests do + client.me + end + end end