Module: EventbriteSDK::Resource::Operations::Endpoint::ClassMethods

Defined in:
lib/eventbrite_sdk/resource/operations/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/eventbrite_sdk/resource/operations/endpoint.rb', line 6

def path
  @path
end

#path_optsObject (readonly)

Returns the value of attribute path_opts.



6
7
8
# File 'lib/eventbrite_sdk/resource/operations/endpoint.rb', line 6

def path_opts
  @path_opts
end

Instance Method Details

#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.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/eventbrite_sdk/resource/operations/endpoint.rb', line 58

def resource_path(path)
  @path = path

  path.scan(/:\w+/).each do |path_attr|
    attr = path_attr.delete(':').to_sym

    define_method(attr) do
      @attrs[attr] if @attrs.respond_to?(attr)
    end
  end
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



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/eventbrite_sdk/resource/operations/endpoint.rb', line 32

def retrieve(params, request = EventbriteSDK)
  url_path = params.reduce(@path) do |path, (key, value)|
    path.gsub(":#{key}", value.to_s)
  end

  if params[:expand]
    query = { expand: [*params[:expand]].join(',') }
  end

  new request.get(url: url_path, query: query)
end