class EventbriteSDK::Resource
Public Class Methods
build(attrs)
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 7 def self.build(attrs) new.tap do |instance| instance.assign_attributes(attrs) end end
define_api_actions(*actions)
click to toggle source
Allows compile time definition of POST methods
Example:
class Event < Resource define_api_actions :publish, :unpublish end
would defined instance methods like so:
def publish
!new? && EventbriteSDK.post(url: path('publish'))
end
def publish
!new? && EventbriteSDK.post(url: path('unpublish'))
end
# File lib/eventbrite_sdk/resource.rb, line 29 def self.define_api_actions(*actions) req = lambda do |inst, postfix, opts| inst.instance_eval { !new? && EventbriteSDK.post(opts.merge(url: path(postfix))) } end actions.each do |action| if action.is_a?(Hash) method_name, postfix_path = action.flatten else method_name = postfix_path = action end define_method(method_name) { |opts = {}| req.call(self, postfix_path, opts) } end end
new(hydrated_attrs = {})
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 45 def initialize(hydrated_attrs = {}) reload hydrated_attrs end
Public Instance Methods
delete(request: EventbriteSDK, api_token: nil)
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 81 def delete(request: EventbriteSDK, api_token: nil) request.delete(url: path, api_token: api_token)['deleted'] end
inspect()
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 61 def inspect "#<#{self.class}: #{JSON.pretty_generate(@attrs.to_h)}>" end
new?()
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 49 def new? !id end
read_attribute_for_serialization(attribute)
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 85 def read_attribute_for_serialization(attribute) attrs[attribute] end
refresh!(request: EventbriteSDK, api_token: nil)
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 53 def refresh!(request: EventbriteSDK, api_token: nil) if new? false else reload request.get(url: path, api_token: api_token) end end
save(postfixed_path = '', api_token: nil, request: EventbriteSDK)
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 65 def save(postfixed_path = '', api_token: nil, request: EventbriteSDK) return unless changed? || !postfixed_path.empty? response = request.post(url: path(postfixed_path), payload: attrs.payload(self.class.prefix), api_token: api_token) reload(response) true end
to_json(opts = {})
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 77 def to_json(opts = {}) attrs.to_json(opts) end
Private Instance Methods
reload(hydrated_attrs = {})
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 95 def reload(hydrated_attrs = {}) build_attrs(hydrated_attrs) reset_memoized_relationships end
resource_class_from_string(klass)
click to toggle source
# File lib/eventbrite_sdk/resource.rb, line 91 def resource_class_from_string(klass) EventbriteSDK.const_get(klass) end