Class: Msg91::HttpClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHttpClient

Returns a new instance of HttpClient.



6
7
8
9
10
11
12
# File 'lib/msg91/http_client.rb', line 6

def initialize
  @base_url = URI('https://api.msg91.com/')
  @client = Net::HTTP.new(@base_url.host, @base_url.port)
  @client.use_ssl = true
  @client.verify_mode = OpenSSL::SSL::VERIFY_NONE
  @headers = { 'authkey' => Msg91.configuration.auth_key, 'Content-Type' => 'application/json' }
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



4
5
6
# File 'lib/msg91/http_client.rb', line 4

def base_url
  @base_url
end

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/msg91/http_client.rb', line 3

def client
  @client
end

#headersObject (readonly)

Returns the value of attribute headers.



4
5
6
# File 'lib/msg91/http_client.rb', line 4

def headers
  @headers
end

#requestObject

Returns the value of attribute request.



3
4
5
# File 'lib/msg91/http_client.rb', line 3

def request
  @request
end

Instance Method Details

#get(uri, params) ⇒ Object



14
15
16
17
18
# File 'lib/msg91/http_client.rb', line 14

def get(uri, params)
  @base_url.query = URI.encode_www_form(params)
  response = client.get(@base_url + uri)
  JSON.parse(response.read_body)
end

#post(_uri, params) ⇒ Object



20
21
22
23
# File 'lib/msg91/http_client.rb', line 20

def post(_uri, params)
  response = client.post(@base_url, params, @headers)
  JSON.parse(response.read_body)
end