Class: Amiando::Resource

Inherits:
Object
  • Object
show all
Includes:
Autorun
Defined in:
lib/amiando/resource.rb

Direct Known Subclasses

ApiKey, Event, Partner, PaymentType, TicketCategory, TicketShop, User

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Autorun

included

Constructor Details

- (Resource) initialize(attributes = nil)

A new instance of Resource



114
115
116
# File 'lib/amiando/resource.rb', line 114

def initialize(attributes = nil)
  set_attributes(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_name, *args, &block)



122
123
124
125
126
127
128
# File 'lib/amiando/resource.rb', line 122

def method_missing(method_name, *args, &block)
  if attributes.key?(method_name) && args.empty?
    attributes[method_name]
  else
    super
  end
end

Instance Attribute Details

- (Object) attributes (readonly)

Returns the value of attribute attributes



6
7
8
# File 'lib/amiando/resource.rb', line 6

def attributes
  @attributes
end

- (Object) request

Returns the value of attribute request



5
6
7
# File 'lib/amiando/resource.rb', line 5

def request
  @request
end

- (Object) response

Returns the value of attribute response



5
6
7
# File 'lib/amiando/resource.rb', line 5

def response
  @response
end

- (Object) success (readonly)

Returns the value of attribute success



6
7
8
# File 'lib/amiando/resource.rb', line 6

def success
  @success
end

Class Method Details

+ (Object) map(local, remote, options = {})



11
12
13
14
# File 'lib/amiando/resource.rb', line 11

def map(local, remote, options = {})
  mapping[local] = remote
  typecasting[local] = options[:type] if options[:type]
end

+ (Object) map_params(attributes)

From { :first_name => '1', :last_name => '2' } to { :firstName => '1', :lastName => '2' }



27
28
29
30
31
32
33
34
# File 'lib/amiando/resource.rb', line 27

def map_params(attributes)
  mapped_attributes = attributes.map do |key,value|
    mapped_key = mapping[key] || key
    value = typecast(key, value)
    [mapped_key, value]
  end
  Hash[mapped_attributes]
end

+ (Object) mapping



20
21
22
# File 'lib/amiando/resource.rb', line 20

def mapping
  @@mapping ||= {}
end

+ (Object) method_missing(method_name, *args, &block)



47
48
49
50
51
52
53
54
55
# File 'lib/amiando/resource.rb', line 47

def method_missing(method_name, *args, &block)
  if match = /sync_(.*)/.match(method_name.to_s)
    res = self.send(match[1], *args, &block)
    Amiando.run
    res
  else
    super
  end
end

+ (Object) reverse_map_params(attributes)



36
37
38
39
40
41
42
43
44
45
# File 'lib/amiando/resource.rb', line 36

def reverse_map_params(attributes)
  inverted_mapping = mapping.invert
  mapped_attributes = attributes.map do |key,value|
    key        = key.to_sym
    mapped_key = inverted_mapping[key] || key
    value      = inverse_typecast(key, value)
    [mapped_key, value]
  end
  Hash[mapped_attributes]
end

+ (Object) typecasting



16
17
18
# File 'lib/amiando/resource.rb', line 16

def typecasting
  @@typecasting ||= {}
end

Instance Method Details

- (Object) ==(resource)



151
152
153
# File 'lib/amiando/resource.rb', line 151

def ==(resource)
  id == resource.id
end

- (Object) [](key)



118
119
120
# File 'lib/amiando/resource.rb', line 118

def [](key)
  @attributes[key.to_sym]
end

- (Object) extract_attributes_from(response_body, key)



143
144
145
146
147
148
149
# File 'lib/amiando/resource.rb', line 143

def extract_attributes_from(response_body, key)
  @attributes = {}

  set_attributes(response_body[key])

  @success = response_body['success']
end

- (Object) id



130
131
132
# File 'lib/amiando/resource.rb', line 130

def id
  attributes[:id]
end

- (Object) populate(reponse_body)



134
135
136
# File 'lib/amiando/resource.rb', line 134

def populate(reponse_body)
  raise Error::NotImplemented.new("populate method not implemented for #{self.class}")
end

- (Object) populate_create(response_body)



138
139
140
141
# File 'lib/amiando/resource.rb', line 138

def populate_create(response_body)
  @attributes = {:id => response_body['id'], :errors => response_body['errors']}
  @success    = response_body['success']
end