class SocketLabs::InjectionApi::Core::HttpRequest

Attributes

endpoint[R]

The SocketLabs Injection API endpoint

http[R]

The Net::HTTP used when making the HTTP request

proxy[R]

The Proxy to use when making the HTTP request

request_method[R]

The HTTP Request Method to use

timeout[R]

The Timeout to use when making the HTTP request

Public Class Methods

http_request_method() click to toggle source

Hash enumeration of HTTP Request Methods

# File lib/socketlabs/injectionapi/core/http_request.rb, line 14
def self.http_request_method
  {
      :Get => { :method => "GET" },
      :Post => { :method => "POST" },
      :Put => { :method => "PUT" },
      :Delete => { :method => "DELETE" }
  }
end
new( http_request_method, arguments = nil ) click to toggle source

@param [Hash] http_request_method @param [Hash] arguments:

http_endpoint = The SocketLabs Injection API endpoint
proxy = hash of proxy settings. ex: { host: '127.0.0.1', port: 8080 }
# File lib/socketlabs/injectionapi/core/http_request.rb, line 38
def initialize(
    http_request_method,
    arguments = nil
)
  @request_method = http_request_method
  @endpoint = "https://inject.socketlabs.com/api/v1/email"
  @proxy = Array.new
  @timeout = 120

  unless arguments.nil? || arguments.empty?

    unless arguments[:http_endpoint].nil? || arguments[:http_endpoint].empty?
      @endpoint = arguments[:http_endpoint]
    end

    unless arguments[:proxy].nil? || arguments[:proxy].empty?
      @proxy = arguments[:proxy]
    end

    unless arguments[:timeout].nil?
      @timeout = arguments[:timeout]
    end

  end

  @http = nil
  @request = build_request
end

Public Instance Methods

send_request(request) click to toggle source

Send the HTTP Request @param [InjectionRequest]

# File lib/socketlabs/injectionapi/core/http_request.rb, line 69
def send_request(request)

  factory_hash = request.to_hash
  @request.body = factory_hash.to_json

  # send request
  response = @http.request(@request)
  http_response = HttpResponse.new(response)

  http_response

end

Private Instance Methods

add_request_headers(request) click to toggle source

add request headers @param [HTTP::NET] request: the request object @return [HTTP::NET] the resulting request

# File lib/socketlabs/injectionapi/core/http_request.rb, line 102
def add_request_headers(request)

  request.add_field('Content-Type', 'application/json')
  headers.each do |item|
    request[item[:key]] = item[:value]
  end
  request
end
build_request() click to toggle source

Build the API request for HTTP::NET

# File lib/socketlabs/injectionapi/core/http_request.rb, line 112
def build_request

  uri = URI.parse(@endpoint)
  # add uri
  params = [uri.host, uri.port]
  # add proxy
  params += @proxy.values_at(:host, :port, :user, :pass) unless @proxy.empty?

  @http = Net::HTTP.new(*params)
  # add timeout
  @http.read_timeout = @timeout
  # add ssl
  if @endpoint.start_with?('https')
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end

  net_http = Kernel.const_get('Net::HTTP::' + @request_method[:method].capitalize)
  @request = add_request_headers(net_http.new(uri.request_uri))

end
headers() click to toggle source

headers to add to the request

# File lib/socketlabs/injectionapi/core/http_request.rb, line 91
def headers
  [
      { :key => "User-Agent", :value => user_agent},
      { :key => "Content-Type", :value => "application/json; charset=utf-8" },
      { :key => "Accept", :value => "application/json"}
  ]
end
user_agent() click to toggle source

The User-Agent request header added to the Injection API Http request. Used to identify the source of the request. @return [String] the SocketLabs User-Agent

# File lib/socketlabs/injectionapi/core/http_request.rb, line 86
def user_agent
  "SocketLabs-ruby/#{VERSION};ruby(#{RUBY_VERSION})"
end