Class: Seam::Resources::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/seam/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.



11
12
13
14
15
16
17
18
19
# File 'lib/seam/base_resource.rb', line 11

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

  @data.each do |key, value|
    value = Seam::DeepHashAccessor.new(value) if value.is_a?(Hash)
    instance_variable_set(:"@#{key}", value)
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/seam/base_resource.rb', line 9

def client
  @client
end

#dataObject

Returns the value of attribute data.



9
10
11
# File 'lib/seam/base_resource.rb', line 9

def data
  @data
end

Class Method Details

.date_accessor(*attrs) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/seam/base_resource.rb', line 46

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



29
30
31
32
33
34
35
# File 'lib/seam/base_resource.rb', line 29

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



37
38
39
40
41
42
43
44
# File 'lib/seam/base_resource.rb', line 37

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



21
22
23
24
25
26
27
# File 'lib/seam/base_resource.rb', line 21

def update_from_response(data)
  @data = data
  @data.each do |key, value|
    value = Seam::DeepHashAccessor.new(value) if value.is_a?(Hash)
    instance_variable_set(:"@#{key}", value)
  end
end