Module: NextcallerClient::Utils
- Defined in:
- lib/nextcaller_client/utils.rb
Class Method Summary collapse
- .parse_error_response(resp) ⇒ Object
- .prepare_json_data(data) ⇒ Object
-
.prepare_url(path, url_params = {}) ⇒ Object
Prepare url from path and params.
-
.validate_phone(value, length = DEFAULT_PHONE_LENGTH) ⇒ Object
Validate phone format.
Class Method Details
.parse_error_response(resp) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/nextcaller_client/utils.rb', line 12 def self.parse_error_response(resp) if resp['Content-Type'].include? 'application/json' JSON.parse(resp.body) else resp.body end end |
.prepare_json_data(data) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/nextcaller_client/utils.rb', line 5 def self.prepare_json_data(data) unless data.is_a? Hash raise ArgumentError, 'Data should be a hash.' end data.to_json end |
.prepare_url(path, url_params = {}) ⇒ Object
Prepare url from path and params
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/nextcaller_client/utils.rb', line 29 def self.prepare_url(path, url_params={}) url = '%s%s' % [FULL_URL, path] unless url.end_with?('/') url += '/' end unless url_params.empty? url_params_str = url_params.collect { |k, v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}" }.join('&') url += "?#{url_params_str}" end url end |
.validate_phone(value, length = DEFAULT_PHONE_LENGTH) ⇒ Object
Validate phone format
21 22 23 24 25 26 |
# File 'lib/nextcaller_client/utils.rb', line 21 def self.validate_phone(value, length=DEFAULT_PHONE_LENGTH) value = value.to_s unless value =~ /^[0-9]{10}$/ raise ArgumentError, 'Invalid phone number: %s. Phone should consists of 10 digits.' % value end end |