Class: SocketLabs::InjectionApi::Core::HttpRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/socketlabs/injectionapi/core/http_request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_request_method, arguments = nil) ⇒ HttpRequest

Returns a new instance of HttpRequest.

Parameters:

  • http_request_method (Hash)
  • arguments: (Hash)

    http_endpoint = The SocketLabs Injection API endpoint proxy = hash of proxy settings. ex: { host: ‘127.0.0.1’, port: 8080 }



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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
  @headers =
    [
      { :key => "User-Agent", :value => user_agent},
      { :key => "Content-Type", :value => "application/json; charset=utf-8" },
      { :key => "Accept", :value => "application/json"}
    ]

  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

    unless arguments[:authorization].nil? || arguments[:authorization].empty?
      @headers.push({ :key => "Authorization", :value => 'Bearer ' + arguments[:authorization]})
    end

  end

  @http = nil
  @request = build_request
end

Instance Attribute Details

#endpointObject (readonly)

The SocketLabs Injection API endpoint



26
27
28
# File 'lib/socketlabs/injectionapi/core/http_request.rb', line 26

def endpoint
  @endpoint
end

#httpObject (readonly)

The Net::HTTP used when making the HTTP request



30
31
32
# File 'lib/socketlabs/injectionapi/core/http_request.rb', line 30

def http
  @http
end

#proxyObject (readonly)

The Proxy to use when making the HTTP request



28
29
30
# File 'lib/socketlabs/injectionapi/core/http_request.rb', line 28

def proxy
  @proxy
end

#request_methodObject (readonly)

The HTTP Request Method to use



24
25
26
# File 'lib/socketlabs/injectionapi/core/http_request.rb', line 24

def request_method
  @request_method
end

#timeoutObject (readonly)

The Timeout to use when making the HTTP request



32
33
34
# File 'lib/socketlabs/injectionapi/core/http_request.rb', line 32

def timeout
  @timeout
end

Class Method Details

.http_request_methodObject

Hash enumeration of HTTP Request Methods



14
15
16
17
18
19
20
21
# 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

Instance Method Details

#send_request(request) ⇒ Object

Send the HTTP Request

Parameters:

  • (InjectionRequest)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/socketlabs/injectionapi/core/http_request.rb', line 79

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