Module: CellForce::Api::Util

Defined in:
lib/cell_force/api.rb

Class Method Summary collapse

Class Method Details

.convert_sms_args(args) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cell_force/api.rb', line 31

def convert_sms_args(args)
  validation = args.delete(:sms_validation)
  unless validation.is_a?(SmsValidation::Sms)
    raise StandardError, "You cannot send an SMS by calling #{CellForce::Api}#post directly.  Use #{CellForce::Api}#send_sms(phone, message) instead."
  end
  args.merge(cellnumber: validation.phone[1..-1], message: validation.message)
end

.filtered(hash) ⇒ Object



14
15
16
# File 'lib/cell_force/api.rb', line 14

def filtered(hash)
  hash.is_a?(Hash) ? hash.reject{ |k,v| PARAMETER_FILTER.include?(k.to_sym) } : hash
end

.hash_to_log(hash) ⇒ Object



18
19
20
# File 'lib/cell_force/api.rb', line 18

def hash_to_log(hash)
  hash.sort_by{ |k,v| k }.collect{|k,v| "\t\t\t#{k}:#{" " * (15 - k.length)}\t#{v}" }.join("\n")
end

.parse_response(response) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
# File 'lib/cell_force/api.rb', line 22

def parse_response(response)
  body = JSON.parse(response.body)
  SmsValidation.log { "CellForce API: #{response.code}:#{response.message}" }
  SmsValidation.configuration.logger.debug { "\n\t\tHeaders:\n#{hash_to_log(response.headers)}" }
  SmsValidation.log { "\n\t\tBody:\n#{hash_to_log(body.inject({}) { |hash, (k,v)| hash[k] = filtered(v); hash })}" }
  raise Failure, body["error"] if "failure" == body["status"]
  Struct.new(*body.keys.collect(&:to_sym)).new(*body.values)
end