Module: Smspilot::Request

Included in:
Api
Defined in:
lib/smspilot/request.rb

Instance Method Summary collapse

Instance Method Details

#request_json(body) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/smspilot/request.rb', line 38

def request_json body
  if .nil?
   {"apikey" => api_key}.merge(body).to_json
  else
    {"apikey" => api_key, "login" => , "password" => password}.merge(body).to_json
  end
end

#send_request(body) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/smspilot/request.rb', line 9

def send_request(body)
  json_body = request_json body
  begin
    response = connection.post do |req|
      req.body = json_body
      logger.info("--------\n" + req.body)
    end

    response_error = (response.status == 200) ? Smspilot::Error::WrongStatusError : nil

  rescue Exception => e
    if e.kind_of? Faraday::Error::TimeoutError
      response_error = Smspilot::Error::TimeoutError
    elsif e.kind_of? Faraday::Error::ParsingError
      response_error = Smspilot::Error::ParsingError
    end
  end

  unless response.body["error"].nil?
    response_error = Error::ApiError.get_by_code(response.body["error"]["code"]).new()
  end

  response.instance_eval <<-RESP
    def error; #{response_error} end
  RESP

  response
end