Class: EventbriteSDK::Resource

Inherits:
Object
  • Object
show all
Includes:
Operations::AttributeSchema, Operations::Endpoint, Operations::Relationships
Defined in:
lib/eventbrite_sdk/resource.rb,
lib/eventbrite_sdk/resource/field.rb,
lib/eventbrite_sdk/resource/attributes.rb,
lib/eventbrite_sdk/resource/operations/list.rb,
lib/eventbrite_sdk/resource/field_comparable.rb,
lib/eventbrite_sdk/resource/schema_definition.rb,
lib/eventbrite_sdk/resource/operations/endpoint.rb,
lib/eventbrite_sdk/resource/null_schema_definition.rb,
lib/eventbrite_sdk/resource/operations/relationships.rb,
lib/eventbrite_sdk/resource/operations/attribute_schema.rb

Defined Under Namespace

Modules: Operations Classes: Attributes, Field, FieldComparable, NullSchemaDefinition, SchemaDefinition

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operations::Relationships

included

Methods included from Operations::Endpoint

included

Methods included from Operations::AttributeSchema

included

Constructor Details

#initialize(hydrated_attrs = {}) ⇒ Resource

Returns a new instance of Resource.



45
46
47
# File 'lib/eventbrite_sdk/resource.rb', line 45

def initialize(hydrated_attrs = {})
  reload hydrated_attrs
end

Class Method Details

.build(attrs) ⇒ Object



7
8
9
10
11
# 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) ⇒ Object

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Method Details

#delete(request: EventbriteSDK, api_token: nil) ⇒ Object



81
82
83
# 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

#inspectObject



61
62
63
# File 'lib/eventbrite_sdk/resource.rb', line 61

def inspect
  "#<#{self.class}: #{JSON.pretty_generate(@attrs.to_h)}>"
end

#new?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/eventbrite_sdk/resource.rb', line 49

def new?
  !id
end

#read_attribute_for_serialization(attribute) ⇒ Object



85
86
87
# File 'lib/eventbrite_sdk/resource.rb', line 85

def read_attribute_for_serialization(attribute)
  attrs[attribute]
end

#refresh!(request: EventbriteSDK, api_token: nil) ⇒ Object



53
54
55
56
57
58
59
# 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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# 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 = {}) ⇒ Object



77
78
79
# File 'lib/eventbrite_sdk/resource.rb', line 77

def to_json(opts = {})
  attrs.to_json(opts)
end