Class: Terasms::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/terasms/http_client/base.rb

Direct Known Subclasses

Sms

Instance Method Summary collapse

Instance Method Details

#submit(url, params, method = 'GET', type = 'text', body = '') ⇒ Object



7
8
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
37
38
39
40
# File 'lib/terasms/http_client/base.rb', line 7

def submit url, params, method = 'GET', type = 'text', body =''
  begin
    uri = URI.parse(url)
    uri.query = URI.encode_www_form(params)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.read_timeout = 10
    headers = case type
    when 'json'
      {'Content-Type' => 'application/json'}
    when 'text'
      {'Content-Type' => 'text/html'}
    else 
      raise 'unknown type'
    end
    response = case method
    when 'GET'
      http.get(uri.request_uri, headers)
    when 'POST'
      http.post(uri.request_uri, body, headers)
    else
      raise 'unknown http method'
    end
    result = case type
    when 'json'
      JSON.parse(response.body)
    when 'text'
      response.body
    end
    {"status"=> "success", "result" => result}
  rescue => error
    {"status"=> "error", "info" => error.to_s}
  end
end