Class: Lightspeed::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/lightspeed/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil, context: nil, attributes: {}) ⇒ Resource

Returns a new instance of Resource.



13
14
15
16
17
# File 'lib/lightspeed/resource.rb', line 13

def initialize(client: nil, context: nil, attributes: {})
  self.client = client
  self.context = context
  self.attributes = attributes
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



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

def 
  @account
end

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.collection_nameObject



87
88
89
# File 'lib/lightspeed/resource.rb', line 87

def self.collection_name
  resource_name.pluralize
end

.fields(fields = {}) ⇒ Object



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

def self.fields(fields = {})
  @fields ||= []
  attr_writer(*fields.keys)
  fields.each do |name, klass|
    @fields << define_method(name) do
      get_transformed_value(name, klass)
    end
  end
  @fields
end

.id_fieldObject



91
92
93
# File 'lib/lightspeed/resource.rb', line 91

def self.id_field
  "#{resource_name.camelize(:lower)}ID"
end

.relationships(*args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/lightspeed/resource.rb', line 99

def self.relationships(*args)
  @relationships ||= []
  paired_args = args.flat_map { |r| r.is_a?(Hash) ? r.to_a : [[r, r]] }
  paired_args.each do |(relation_name, class_name)|
    method_name = relation_name.to_s.underscore.to_sym
    @relationships << define_method(method_name) do
      instance_variable_get("@#{method_name}") || get_relation(method_name, relation_name, class_name)
    end
  end
  @relationships
end

.resource_nameObject



83
84
85
# File 'lib/lightspeed/resource.rb', line 83

def self.resource_name
  name.demodulize
end

Instance Method Details

#as_jsonObject Also known as: to_h



120
121
122
# File 'lib/lightspeed/resource.rb', line 120

def as_json
  fields_to_h.merge(relationships_to_h).reject { |_, v| v.nil? || v == {} }
end

#base_pathObject



125
126
127
128
129
130
131
# File 'lib/lightspeed/resource.rb', line 125

def base_path
  if context.is_a?(Lightspeed::Collection)
    "#{context.base_path}/#{id}"
  else
    "#{.base_path}/#{resource_name}/#{id}"
  end
end

#destroyObject



78
79
80
81
# File 'lib/lightspeed/resource.rb', line 78

def destroy
  self.attributes = delete[resource_name]
  self
end

#get_transformed_value(name, kind) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lightspeed/resource.rb', line 43

def get_transformed_value(name, kind)
  value = instance_variable_get("@#{name}")
  if value.is_a?(String)
    case kind
    when :string then value
    when :integer then value.to_i
    when :id then value.to_i
    when :datetime then DateTime.parse(value)
    when :boolean then value == 'true'
    when :decimal then BigDecimal.new(value)
    when :hash then Hash.new(value)
    else
      raise ArgumentError, "Could not transform value #{value} to a #{kind}"
    end
  else
    value
  end
end

#id_paramsObject



95
96
97
# File 'lib/lightspeed/resource.rb', line 95

def id_params
  { self.class.id_field => id }
end

#inspectObject



111
112
113
# File 'lib/lightspeed/resource.rb', line 111

def inspect
  "#<#{self.class.name} API#{base_path}>"
end

#loadObject



66
67
68
# File 'lib/lightspeed/resource.rb', line 66

def load
  self.attributes = get[resource_name] if (attributes.keys - [self.class.id_field]).empty?
end

#read_attribute_for_serialization(method_name) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/lightspeed/resource.rb', line 141

def read_attribute_for_serialization(method_name)
  method_name = method_name.to_sym

  if self.class.fields.include?(method_name) || self.class.relationships.include?(method_name)
    send(method_name)
  end
end

#reloadObject



70
71
72
# File 'lib/lightspeed/resource.rb', line 70

def reload
  self.attributes = get[resource_name]
end

#resource_nameObject



137
138
139
# File 'lib/lightspeed/resource.rb', line 137

def resource_name
  self.class.resource_name
end

#singular_path_parentObject



133
134
135
# File 'lib/lightspeed/resource.rb', line 133

def singular_path_parent
  context
end

#to_jsonObject



116
117
118
# File 'lib/lightspeed/resource.rb', line 116

def to_json
  Yajl::Encoder.encode(as_json)
end

#update(attributes = {}) ⇒ Object



74
75
76
# File 'lib/lightspeed/resource.rb', line 74

def update(attributes = {})
  self.attributes = put(body: Yajl::Encoder.encode(attributes))[resource_name]
end