Class: Hubspot::HttpRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/hubspot/http_request.rb

Direct Known Subclasses

Base

Constant Summary collapse

BASE_URL =
"https://api.hubapi.com"

Instance Attribute Summary collapse

Attributes inherited from Object

#result

Instance Method Summary collapse

Methods inherited from Object

call, #call, #constructor, #errors, #failure?, #success?

Constructor Details

#initialize(debug = true) ⇒ HttpRequest

Returns a new instance of HttpRequest.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/hubspot/http_request.rb', line 10

def initialize(debug = true)
  @uri = URI(BASE_URL)
  @http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == "https"
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end

  @http.set_debug_output($stdout) if debug
end

Instance Attribute Details

#httpObject

Returns the value of attribute http.



8
9
10
# File 'lib/hubspot/http_request.rb', line 8

def http
  @http
end

#uriObject

Returns the value of attribute uri.



8
9
10
# File 'lib/hubspot/http_request.rb', line 8

def uri
  @uri
end

Instance Method Details

#get(path, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
# File 'lib/hubspot/http_request.rb', line 22

def get(path, options = {})
  raise ArgumentError, "Path Error" if path.blank?

  body = JSON.dump(options)
  request = Net::HTTP::Get.new(path, body)
  response = http.request(request)

  HttpResponse.call(response)
end

#post(path, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hubspot/http_request.rb', line 32

def post(path, options = {})
  raise ArgumentError, "Path Error" if path.blank?

  body = JSON.dump(options)
  request = Net::HTTP::Post.new(path) # fyi: uri.path or just uri
  request.add_field("Content-Type", "application/json")
  request.body = body
  response = http.request(request)

  HttpResponse.call(response)
end