Class: InvisibleCollector::Resources::CustomerResource

Inherits:
Object
  • Object
show all
Includes:
DefaultHandlers
Defined in:
lib/invisible_collector/resources/customer_resource.rb

Instance Method Summary collapse

Methods included from DefaultHandlers

#execute, #execute_get, #execute_post, #handle, #handles

Constructor Details

#initialize(options = {}) ⇒ CustomerResource

Returns a new instance of CustomerResource.



8
9
10
11
12
13
# File 'lib/invisible_collector/resources/customer_resource.rb', line 8

def initialize(options = {})
  super(options)
  handle(400) { |response| raise InvisibleCollector::InvalidRequest.from_json(response.body) }
  handle(409) { |response| raise InvisibleCollector::InvalidRequest.from_json(response.body) }
  handle(422) { |response| raise InvisibleCollector::InvalidRequest.from_json(response.body) }
end

Instance Method Details

#alarm(customer, params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/invisible_collector/resources/customer_resource.rb', line 15

def alarm(customer, params = {})
  id = customer.is_a?(Model::Customer) ? customer.gid : customer
  response = @connection.get("v1/customers/#{id}/alarm", params)
  if response.status == 404
    Response.new(response, nil)
  elsif handles.key? response.status
    handles[response.status].call response
  else
    alarm = Model::Alarm.new(JSON.parse(response.body).deep_transform_keys(&:underscore))
    Response.new(response, alarm)
  end
end

#debts(customer, params = {}) ⇒ Object

Returns a list of all debts registered for the specified customer. The customer attribute can be either a InvisibleCollector::Model::Customer instance or a customer id.

gid = 'customer id'
c = InvisibleCollector::Model::Customer.new(gid: gid)
client.customer.debts(c) #=> [InvisibleCollector::Model::Debt]
client.customer.debts(gid) #=> [InvisibleCollector::Model::Debt]


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/invisible_collector/resources/customer_resource.rb', line 37

def debts(customer, params = {})
  id = customer.is_a?(Model::Customer) ? customer.gid : customer
  response = @connection.get("customers/#{id}/debts", params)
  raise InvisibleCollector::NotFound.from_json(response.body) if response.status == 404

  if handles.key?(response.status)
    handles[response.status].call response
  else
    debts = JSON.parse(response.body).map { |j| Model::Debt.new(j.deep_transform_keys(&:underscore)) }
    Response.new(response, debts)
  end
end

#find(params = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/invisible_collector/resources/customer_resource.rb', line 50

def find(params = {})
  response = @connection.get('customers/find', params)
  if handles.key? response.status
    handles[response.status].call response
  else
    customer = JSON.parse(response.body).map { |j| Model::Customer.new(j.deep_transform_keys(&:underscore)) }
    Response.new(response, customer)
  end
end

#get(id) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/invisible_collector/resources/customer_resource.rb', line 60

def get(id)
  response = @connection.get("customers/#{id}")
  if response.status == 404
    nil
  else
    build_response(response)
  end
end

#get!(id) ⇒ Object



69
70
71
72
73
74
# File 'lib/invisible_collector/resources/customer_resource.rb', line 69

def get!(id)
  response = @connection.get("customers/#{id}")
  raise InvisibleCollector::NotFound.from_json(response.body) if response.status == 404

  build_response(response)
end

#save(customer = {}) ⇒ Object



76
77
78
79
# File 'lib/invisible_collector/resources/customer_resource.rb', line 76

def save(customer = {})
  response = execute_post('/customers', customer)
  build_response(response)
end

#update(customer = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/invisible_collector/resources/customer_resource.rb', line 81

def update(customer = {})
  body = customer.is_a?(Model::Customer) ? customer.to_h : customer
  response = execute do |connection|
    connection.put do |req|
      req.url "/customers/#{body[:gid]}"
      req.headers['Content-Type'] = 'application/json'
      req.body = body.to_json
    end
  end
  build_response(response)
end