Class: Essential::Resource

Inherits:
Object
  • Object
show all
Includes:
AttrMethods
Defined in:
lib/essential/resource.rb,
lib/essential/resource/list.rb,
lib/essential/resource/create.rb,
lib/essential/resource/delete.rb,
lib/essential/resource/update.rb,
lib/essential/resource/attr_methods.rb,
lib/essential/resource/attr_relations.rb,
lib/essential/resource/paginator_proxy.rb

Defined Under Namespace

Modules: AttrMethods, AttrRelations, Create, Delete, List, Update Classes: PaginatorProxy

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AttrMethods

#filter_attrs, included

Constructor Details

#initialize(sid: nil, attrs: nil, headers: nil) ⇒ Resource

Returns a new instance of Resource.



45
46
47
48
49
50
# File 'lib/essential/resource.rb', line 45

def initialize(sid: nil, attrs: nil, headers: nil)
  init_from(attrs)
  @attributes['sid'] = sid if sid
  @headers = headers
  self
end

Class Attribute Details

.schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

Class Method Details

.request(method, url: nil, params: nil, headers: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/essential/resource.rb', line 14

def request(method, url: nil, params: nil, headers: nil)
  if headers
    headers = headers.clone
    sid     = headers.delete(:sid)
    token   = headers.delete(:token)
  end

  Essential.request(method, url, sid: sid, token: token, params: params, headers: headers)
end

.urlObject



33
34
35
36
37
38
# File 'lib/essential/resource.rb', line 33

def self.url
  if self == Resource
    raise NotImplementedError, 'Resource is an abstract class'
  end
  "/v2/account/#{CGI.escape(class_name.downcase)}s"
end

Instance Method Details

#==(other) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/essential/resource.rb', line 84

def ==(other)
  case other
  when Essential::Resource
    self.sid == other.sid
  else
    false
  end
end

#as_json(opts = {}) ⇒ Object



76
77
78
# File 'lib/essential/resource.rb', line 76

def as_json(opts={})
  @attributes.dup
end

#fetch(headers: @headers) ⇒ Object



52
53
54
55
56
# File 'lib/essential/resource.rb', line 52

def fetch(headers: @headers)
  json = JSON.parse self.class.request(:get, url: self.url, headers: headers)
  init_from json
  self
end

#init_from(attributes) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/essential/resource.rb', line 58

def init_from(attributes)
  attributes = (attributes || {}).clone

  # reject reject keys that aren't actually our attrs
  @attributes = filter_attrs(attributes)

  if self.class.schema
    self.class.schema.each do |key,type|
      apply_schema(key, type)
    end
  end
  self
end

#inspectObject



93
94
95
96
97
98
99
100
# File 'lib/essential/resource.rb', line 93

def inspect
  format(
    '#<%s:0x%s @attributes=%s>',
    self.class.name,
    (self.object_id << 1).to_s(16),
    JSON.pretty_generate(@attributes)
  )
end

#loaded?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/essential/resource.rb', line 72

def loaded?
  @attributes.size > 1
end

#to_json(opts = {}) ⇒ Object



80
81
82
# File 'lib/essential/resource.rb', line 80

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

#urlObject

Raises:

  • (ArgumentError)


40
41
42
43
# File 'lib/essential/resource.rb', line 40

def url
  raise ArgumentError, 'sid required' if sid.nil? || sid.empty?
  format('%s/%s', self.class.url, CGI.escape(self.sid))
end