Class: Badgify::HttpDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/badgify/http_decorator.rb

Overview

Constant Summary collapse

OPEN_TIMEOUT =

Timeouts

10
READ_TIMEOUT =

in seconds

120
CONTENT_TYPE_JSON =

Content-types

'application/json'
CONTENT_TYPE_FORM =
'application/x-www-form-urlencoded'

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ HttpDecorator

CONTENT_TYPE_MULTIPART = “multipart/form-data; boundary=#{ Rack::Multipart::MULTIPART_BOUNDARY }”



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/badgify/http_decorator.rb', line 21

def initialize(url)
  if Badgify.token.nil?
    raise AuthenticationError.new("No token provided")
  end

  # Build up our HTTP object
  uri = URI(url)
  @http = Net::HTTP.new(uri.hostname, uri.port)
  @http.use_ssl = true if uri.scheme == "https"
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if uri.scheme == "https"
  @http.open_timeout = OPEN_TIMEOUT
  @http.read_timeout = READ_TIMEOUT

  # In local development we can log requests and responses to $stdout.
  #   DO NOT EVER do this in production. EVER.
  if ENV['RACK_ENV'] == 'development'
    @http.set_debug_output($stdout)
  end
end

Instance Method Details

#delete(path) ⇒ Object

DELETE



91
92
93
94
95
# File 'lib/badgify/http_decorator.rb', line 91

def delete(path)
  request = Net::HTTP::Delete.new(path)

  parse fetch(request)
end

#finishObject

Clean up the connection if needed.



61
62
63
# File 'lib/badgify/http_decorator.rb', line 61

def finish
  @http.finish if @http.started?
end

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

GET



66
67
68
69
70
71
72
# File 'lib/badgify/http_decorator.rb', line 66

def get(path, params = {})
  uri       = URI.parse(path)
  uri.query = URI.encode_www_form(params) unless params.empty?
  request   = Net::HTTP::Get.new(uri.to_s)

  parse fetch(request)
end

#multipart(path, params) ⇒ Object

POST multipart



130
131
132
133
134
135
136
137
138
139
# File 'lib/badgify/http_decorator.rb', line 130

def multipart(path, params)
  request = Net::HTTP::Post.new(path)

  request.content_type = CONTENT_TYPE_MULTIPART
  request.body = Rack::Multipart::Generator.new(
    'file' => Rack::Multipart::UploadedFile.new(params['file'][:tempfile].path, params['file'][:type])
  ).dump

  parse fetch(request)
end

#patch(path, params = {}, as: :form) ⇒ Object

PATCH



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/badgify/http_decorator.rb', line 98

def patch(path, params = {}, as: :form)
  request = Net::HTTP::Patch.new(path)

  case as
  when :json
    request.content_type = CONTENT_TYPE_JSON
    request.body = JSON.generate(params) unless params.empty?
  else
    request.content_type = CONTENT_TYPE_FORM
    request.body = URI.encode_www_form(params) unless params.empty?
  end

  parse fetch(request)
end

#post(path, params = {}, as: :json) ⇒ Object

POST



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/badgify/http_decorator.rb', line 75

def post(path, params = {}, as: :json)
  request = Net::HTTP::Post.new(path)

  case as
  when :json
    request.content_type = CONTENT_TYPE_JSON
    request.body = JSON.generate(params) unless params.empty?
  else
    request.content_type = CONTENT_TYPE_FORM
    request.body = URI.encode_www_form(params) unless params.empty?
  end

  parse fetch(request)
end

#put(path, params = {}, as: :json) ⇒ Object

PUT



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/badgify/http_decorator.rb', line 114

def put(path, params = {}, as: :json)
  request = Net::HTTP::Put.new(path)

  case as
  when :json
    request.content_type = CONTENT_TYPE_JSON
    request.body = JSON.generate(params) unless params.empty?
  else
    request.content_type = CONTENT_TYPE_FORM
    request.body = URI.encode_www_form(params) unless params.empty?
  end

  parse fetch(request)
end

#startObject

Open a connection for multiple calls.

  • Accepts a block, otherwise just opens the connection.

  • You’ll need to close the connection if you just open it.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/badgify/http_decorator.rb', line 44

def start
  if block_given?
    # Open the connection.
    @http.start unless @http.started?

    # Yield to the calling block.
    yield(self)

    # Clean up the connection.
    @http.finish if @http.started?
  else
    # Open the connection.
    @http.start unless @http.started?
  end
end