Class: Transports::HttpClient

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

Overview

Small abstraction on top of Net::HTTP to handle things like public key pinning et all.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, ssl: self.class.ssl_options, proxy_address: nil, proxy_port: nil, user_agent: self.class.default_user_agent) ⇒ HttpClient

Returns a new instance of HttpClient.



36
37
38
39
40
41
42
43
# File 'lib/mote_sms/transports/http_client.rb', line 36

def initialize(endpoint, ssl: self.class.ssl_options,
               proxy_address: nil, proxy_port: nil, user_agent: self.class.default_user_agent)
  @endpoint = URI.parse(endpoint)
  @proxy_address = proxy_address
  @proxy_port = proxy_port
  @user_agent = user_agent
  @ssl = ssl || self.class.ssl_options
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



34
35
36
# File 'lib/mote_sms/transports/http_client.rb', line 34

def endpoint
  @endpoint
end

#proxy_addressObject (readonly)

Returns the value of attribute proxy_address.



34
35
36
# File 'lib/mote_sms/transports/http_client.rb', line 34

def proxy_address
  @proxy_address
end

#proxy_portObject (readonly)

Returns the value of attribute proxy_port.



34
35
36
# File 'lib/mote_sms/transports/http_client.rb', line 34

def proxy_port
  @proxy_port
end

#sslObject (readonly)

Returns the value of attribute ssl.



34
35
36
# File 'lib/mote_sms/transports/http_client.rb', line 34

def ssl
  @ssl
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



34
35
36
# File 'lib/mote_sms/transports/http_client.rb', line 34

def user_agent
  @user_agent
end

Class Method Details

.default_user_agentObject



30
31
32
# File 'lib/mote_sms/transports/http_client.rb', line 30

def self.default_user_agent
  "Ruby/mote_sms #{MoteSMS::VERSION}"
end

.ssl_optionsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mote_sms/transports/http_client.rb', line 14

def self.ssl_options
  @ssl_options ||= lambda do |http|
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    http.verify_depth = 9
    http.cert_store = OpenSSL::X509::Store.new
    http.cert_store.set_default_paths
    Dir["#{CERTS_PATH}/*.crt"].each do |c|
      begin
        http.cert_store.add_cert OpenSSL::X509::Certificate.new(File.read(c))
      rescue OpenSSL::X509::StoreError => error
        nil
      end
    end
  end
end

Instance Method Details

#https?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/mote_sms/transports/http_client.rb', line 45

def https?
  endpoint.scheme == 'https'
end

#request(request) ⇒ Object



49
50
51
52
# File 'lib/mote_sms/transports/http_client.rb', line 49

def request(request)
  request['User-Agent'] = user_agent
  build_http_client.request(request)
end