Class: CTM::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ctm/base.rb

Defined Under Namespace

Classes: Location

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, token = nil) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ctm/base.rb', line 20

def initialize(data, token=nil)
  @token = token || CTM::Auth.token
  @account_id = data['account_id']
  @list_token_type = case self.class.to_s
                     when 'CTM::Sale' then 'calls'
                     else
                       self.class.to_s.sub(/CTM::/,'').underscore.pluralize
                     end
  if @account_id
    @list_type_path = "accounts/#{@account_id}/#{@list_token_type}"
  else
    @list_type_path = @list_token_type
  end
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



17
18
19
# File 'lib/ctm/base.rb', line 17

def 
  @account_id
end

#tokenObject (readonly)

Returns the value of attribute token.



17
18
19
# File 'lib/ctm/base.rb', line 17

def token
  @token
end

Class Method Details

.create(options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ctm/base.rb', line 48

def self.create(options)
  list_type_path = options.delete(:list_type_path)
  list_token_type = options.delete(:list_token_type)
   = options.delete(:account_id)
  token = options.delete(:token)
  path_str = "/api/v1/#{list_type_path}.json"
  #puts "create: #{self}(#{path_str}) -> #{options.inspect}"
  res = self.post(path_str, :body => options.merge(:auth_token => token))
  #puts "result: #{res.parsed_response.inspect}"
  #puts "properties: #{list_type_path.inspect} -> #{list_token_type.inspect} -> #{account_id}"
  if res.parsed_response['status'] == 'error'
    raise CTM::Error::Create.new(res.parsed_response['reason'])
  else
    self.new(res.parsed_response[list_token_type.singularize], token)
  end
end

Instance Method Details

#release!Object



42
43
44
45
46
# File 'lib/ctm/base.rb', line 42

def release!
  path_str = "/api/v1/#{@list_type_path}/#{@id}.json"
  res = self.class.delete(path_str, :body => {:auth_token => @token})
  res.parsed_response
end

#save(options = {}) ⇒ Object



35
36
37
38
39
40
# File 'lib/ctm/base.rb', line 35

def save(options={})
  #puts "save: #{options.inspect}"
  path_str = "/api/v1/#{@list_type_path}/#{@id}.json"
  #puts path_str
  self.class.put(path_str, :body => options.merge(:auth_token => @token))
end