Class: LocaSMS::RestClient
- Inherits:
-
Object
- Object
- LocaSMS::RestClient
- Defined in:
- lib/locasms/rest_client.rb
Overview
Class that handle http calls to LocaSMS api
Instance Attribute Summary collapse
-
#base_params ⇒ Object
Returns the value of attribute base_params.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
Instance Method Summary collapse
-
#get(action, params = {}) ⇒ Hash
Performs a GET call for an action.
-
#initialize(base_url, base_params = {}) ⇒ RestClient
constructor
Creates a new instance of the RestClient class.
-
#params_for(action, params = {}) ⇒ Hash
Composes the parameters hash.
-
#parse_response(action, response) ⇒ Hash
Parses a result trying to get it in json.
Constructor Details
#initialize(base_url, base_params = {}) ⇒ RestClient
Creates a new instance of the RestClient class
14 15 16 17 |
# File 'lib/locasms/rest_client.rb', line 14 def initialize(base_url, base_params={}) @base_url = base_url @base_params = base_params end |
Instance Attribute Details
#base_params ⇒ Object
Returns the value of attribute base_params.
9 10 11 |
# File 'lib/locasms/rest_client.rb', line 9 def base_params @base_params end |
#base_url ⇒ Object
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/locasms/rest_client.rb', line 9 def base_url @base_url end |
Instance Method Details
#get(action, params = {}) ⇒ Hash
Performs a GET call for an action
41 42 43 44 45 46 47 48 49 |
# File 'lib/locasms/rest_client.rb', line 41 def get(action, params={}) params = params_for action, params uri = URI.parse(base_url) uri.query = URI.encode_www_form(params) response = Net::HTTP.get_response(uri).body parse_response(action, response) end |
#params_for(action, params = {}) ⇒ Hash
Composes the parameters hash
63 64 65 |
# File 'lib/locasms/rest_client.rb', line 63 def params_for(action, params={}) { action: action }.merge(base_params).merge(params).select {|k, v| v } end |
#parse_response(action, response) ⇒ Hash
Parses a result trying to get it in json
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/locasms/rest_client.rb', line 73 def parse_response(action, response) raise InvalidOperation.new(action: action) if response =~ /^0:OPERACAO INVALIDA$/i j = MultiJson.load(response) rescue { 'status' => 1, 'data' => response, 'msg' => nil } return j if j['status'] == 1 or action == :getstatus raise InvalidLogin.new(action: action) if j['msg'] =~ /^falha ao realizar login$/i raise Exception.new(message: j['msg'], raw: response, action: action) end |