Module: EventbriteSDK::Resource::Operations::Endpoint::ClassMethods
- Defined in:
- lib/eventbrite_sdk/resource/operations/endpoint.rb
Instance Method Summary collapse
- #define_path_methods ⇒ Object
- #path(is_create = false) ⇒ Object
-
#resource_path(path) ⇒ Object
Define the url path for the resource.
-
#retrieve(params, request = EventbriteSDK) ⇒ Object
Retrieve a resource.
Instance Method Details
#define_path_methods ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/eventbrite_sdk/resource/operations/endpoint.rb', line 65 def define_path_methods @paths.values.each do |path_config| path_config.scan(/:\w+/).each do |path_attr| attr = path_attr.delete(':').to_sym define_method(attr) { @attrs[attr] if @attrs.respond_to?(attr) } end end end |
#path(is_create = false) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/eventbrite_sdk/resource/operations/endpoint.rb', line 75 def path(is_create = false) if is_create @paths[:create] else @paths[:update] end end |
#resource_path(path) ⇒ Object
Define the url path for the resource. It also implicitly defines the primary key and any additional foreign keys required for this resource.
Example:
TicketClass is a resource that requires a primary key of id and a foreign key of event_id to be retrieved, modified or deleted.
resource_path(‘events/:event_id/ticket_classes/:id’)
The resource now has #id and #event_id accessor methods, and requires those parameters to build the correct resource url path. See the retrieve method (above) for additional details.
55 56 57 58 59 60 61 62 63 |
# File 'lib/eventbrite_sdk/resource/operations/endpoint.rb', line 55 def resource_path(path) if path.is_a?(Hash) @paths = path else @paths = { create: path, update: path } end define_path_methods end |
#retrieve(params, request = EventbriteSDK) ⇒ Object
Retrieve a resource.
params: Hash of supported parameters. The keys and values are
used to build the request URL by substituting supported
keys in the resource_path with the value defined in params.
The :expand key allows the support of expansions of child
objects. It supports strings, symbols and arrays.
expand: :something
expand: %i(something another)
expand: %w(something another)
expand: 'something,another'
Example:
class Thing < Resource
resource_path('things/:id')
end
Thing.retrieve(id: 1234, expand: :others)
This tells the resource to replace the :id placeholder with the value 1234. It also will pass the :expand option with the
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/eventbrite_sdk/resource/operations/endpoint.rb', line 30 def retrieve(params, request = EventbriteSDK) url_path = params.reduce(path) do |path_config, (key, value)| path_config.gsub(":#{key}", value.to_s) end api_token = params.fetch(:api_token, nil) query = params[:expand] && { expand: [*params[:expand]].join(',') } new request.get(url: url_path, query: query, api_token: api_token) end |