Class: Bvr::Customer

Inherits:
Object
  • Object
show all
Defined in:
lib/bvr/customer.rb

Constant Summary collapse

API_COMMANDS =
{
  find: "getuserinfo",
  create: "createcustomer",
  block: "changeuserinfo",
  authenticate: "validateuser",
  changepassword: 'changepassword'
}
BLOCKED_VALUES =
{
  true => 'True',
  false => 'False'
}
CREATE_OPTIONS =
{
  mendatory: [
    :username,
    :password,
    :command,
    :customer,
    :customerpassword
  ],
  optional: [
    :geocalicli,
    :tariffrate
  ]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Customer

Returns a new instance of Customer.



32
33
34
35
# File 'lib/bvr/customer.rb', line 32

def initialize(id)
  @id = id
  @phones = Bvr::PhoneCollection.new(id)
end

Instance Attribute Details

#creditObject

Returns the value of attribute credit.



30
31
32
# File 'lib/bvr/customer.rb', line 30

def credit
  @credit
end

#emailObject

Returns the value of attribute email.



30
31
32
# File 'lib/bvr/customer.rb', line 30

def email
  @email
end

#idObject

Returns the value of attribute id.



30
31
32
# File 'lib/bvr/customer.rb', line 30

def id
  @id
end

#passwordObject

Returns the value of attribute password.



30
31
32
# File 'lib/bvr/customer.rb', line 30

def password
  @password
end

#phonesObject

Returns the value of attribute phones.



30
31
32
# File 'lib/bvr/customer.rb', line 30

def phones
  @phones
end

#raw_blockedObject

Returns the value of attribute raw_blocked.



30
31
32
# File 'lib/bvr/customer.rb', line 30

def raw_blocked
  @raw_blocked
end

Class Method Details

.authenticate(id, password) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/bvr/customer.rb', line 37

def self.authenticate(id, password)
  params = {
    command: API_COMMANDS[:authenticate],
    customer: id,
    customerpassword: password
  }

  response = Bvr.connection.get(params)
  response['Result'] == 'Success'
end

.block(id, block = true) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bvr/customer.rb', line 48

def self.block(id, block=true)
  params = {
    command: API_COMMANDS[:changeuserinfo],
    customer: id,
    customerblocked: block
  }

  raise ArgumentError.new('Please provide a boolean') unless !!block == block

  Bvr.connection.get(params)
end

.change_password(id, old_password, new_password) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/bvr/customer.rb', line 60

def self.change_password(id, old_password, new_password)
  params = {
    command: API_COMMANDS[:changepassword],
    customer: id,
    oldcustomerpassword: old_password,
    newcustomerpassword: new_password
  }

  Bvr.connection.get(params)
end

.create(options) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
# File 'lib/bvr/customer.rb', line 71

def self.create(options)
  params = { command: API_COMMANDS[:create] }

  options.merge!(params)
  raise ArgumentError.new('Invalid or unknown Argument') unless self.valid_create_options?(options)

  Bvr.connection.get(options)
end

.find(id) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/bvr/customer.rb', line 80

def self.find(id)
  params = {
    command: API_COMMANDS[:find],
    customer: id
  }

  response = Bvr.connection.get(params)

  response['Failed'].nil? ? self.new_from_response(response) : nil
end

.new_from_response(h) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/bvr/customer.rb', line 91

def self.new_from_response(h)
  self.new(h["Customer"]).tap do |customer|
    customer.email   = h["EmailAddress"]
    customer.raw_blocked = h["Blocked"]
    customer.credit  = Bvr::Credit.new(h["SpecificBalance"], h["Balance"], customer)
    h['GeocallCLI'].each{ |number| customer.phones << Bvr::Phone.new(number)}
  end
end

Instance Method Details

#balanceObject



100
101
102
# File 'lib/bvr/customer.rb', line 100

def balance
  self.credit.balance
end

#block!Object



108
109
110
111
112
113
114
115
116
# File 'lib/bvr/customer.rb', line 108

def block!
  response = Bvr::Customer.block(self.id, true)

  if response['Result'][0] == 'Success'
    self.raw_blocked = BLOCKED_VALUES[true]
  end

  return response['Result'][0] == 'Success'
end

#blocked?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/bvr/customer.rb', line 104

def blocked?
  self.raw_blocked == BLOCKED_VALUES[true]
end

#calls(options = {}) ⇒ Object



135
136
137
138
# File 'lib/bvr/customer.rb', line 135

def calls(options={})
  return @_calls if @_calls && @_calls.query_params == options
  @_calls = Bvr::CallCollection.find_by_customer_id(self.id, options)
end

#change_password(new_password) ⇒ Object



118
119
120
121
122
123
# File 'lib/bvr/customer.rb', line 118

def change_password(new_password)
  response = Bvr::Customer.change_password(self.id, self.password, new_password)
  return false if response['Result'] != 'Success'

  self.password = new_password
end

#unblock!Object



125
126
127
128
129
130
131
132
133
# File 'lib/bvr/customer.rb', line 125

def unblock!
  response = Bvr::Customer.block(self.id, false)

  if response['Result'][0] == 'Success'
    self.raw_blocked = BLOCKED_VALUES[false]
  end

  return response['Result'][0] == 'Success'
end