Class: Seam::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/seam/resources/base_resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, client = nil) ⇒ BaseResource

Returns a new instance of BaseResource.



7
8
9
10
11
12
13
14
# File 'lib/seam/resources/base_resource.rb', line 7

def initialize(data, client = nil)
  @data = data
  @client = client

  @data.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/seam/resources/base_resource.rb', line 5

def client
  @client
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/seam/resources/base_resource.rb', line 5

def data
  @data
end

Class Method Details

.date_accessor(*attrs) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/seam/resources/base_resource.rb', line 40

def self.date_accessor(*attrs)
  attrs.each do |attr|
    define_method(attr) do
      value = instance_variable_get("@#{attr}")

      raise "No value for #{attr} set" if value.nil?

      parse_datetime(value)
    end
  end
end

.load_from_response(data, client = nil) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/seam/resources/base_resource.rb', line 23

def self.load_from_response(data, client = nil)
  if data.is_a?(Array)
    data.map { |d| new(d, client) }
  else
    new(data, client)
  end
end

Instance Method Details

#inspectObject



31
32
33
34
35
36
37
38
# File 'lib/seam/resources/base_resource.rb', line 31

def inspect
  "<#{self.class.name}:#{"0x00%x" % (object_id << 1)}\n" + # rubocop:disable Style/StringConcatenation, Style/FormatString
    instance_variables
      .map { |k| k.to_s.sub("@", "") }
      .filter { |k| k != "data" and k != "client" and respond_to? k }
      .map { |k| "  #{k}=#{send(k).inspect}" }
      .join("\n") + ">"
end

#update_from_response(data) ⇒ Object



16
17
18
19
20
21
# File 'lib/seam/resources/base_resource.rb', line 16

def update_from_response(data)
  @data = data
  @data.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end