Class: RelateIq::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/relateiq/account.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Account

Returns a new instance of Account.



22
23
24
25
# File 'lib/relateiq/account.rb', line 22

def initialize(attrs = {})
  @id = attrs.fetch(:id, nil)
  @name = attrs.fetch(:name, nil)
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/relateiq/account.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/relateiq/account.rb', line 3

def name
  @name
end

Class Method Details

.create(attrs = {}) ⇒ Object



13
14
15
# File 'lib/relateiq/account.rb', line 13

def self.create(attrs = {})
  RelateIq::.new(attrs).save
end

.find(id) ⇒ Object



9
10
11
# File 'lib/relateiq/account.rb', line 9

def self.find(id)
  from_json(resource["#{id}"].get)
end

.from_json(json_string) ⇒ Object



17
18
19
20
# File 'lib/relateiq/account.rb', line 17

def self.from_json(json_string)
   = JSON.parse(json_string, symbolize_names: true)
  RelateIq::.new()
end

.resourceObject



5
6
7
# File 'lib/relateiq/account.rb', line 5

def self.resource
  @resource ||= RelateIq::ServiceFactory.get_endpoint('accounts')
end

Instance Method Details

#saveObject



35
36
37
38
39
40
41
# File 'lib/relateiq/account.rb', line 35

def save
  if id
    RelateIq::.from_json(RelateIq::.resource["#{id}"].put(to_json))
  else
    RelateIq::.from_json(RelateIq::.resource.post(to_json))
  end
end

#to_jsonObject



27
28
29
30
31
32
33
# File 'lib/relateiq/account.rb', line 27

def to_json
   = {
    name: @name
  }
  [:id] = id if id
  .to_json
end