Skip to content

Commit

Permalink
[Refactor] Making client DRY (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
araluce authored Sep 17, 2024
1 parent 6bd0ed6 commit 5a0e6ea
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 65 deletions.
25 changes: 13 additions & 12 deletions lib/calendlyr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
57 changes: 14 additions & 43 deletions lib/calendlyr/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 #<Calendlyr::Client @token="token" ...>
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Calendlyr
class UserResource < Resource
class UsersResource < Resource
def me
retrieve(uuid: "me")
end
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
11 changes: 11 additions & 0 deletions test/calendlyr/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ def test_token
def test_inspect
assert_equal "#<Calendlyr::Client>", 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
8 changes: 8 additions & 0 deletions test/calendlyr/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5a0e6ea

Please sign in to comment.