Class: Hover::Client::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/hover/client/http.rb

Direct Known Subclasses

BasicAuth, HMAC

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site = 'http://localhost:3000', prefix = 'api/v1') ⇒ HTTP

Returns a new instance of HTTP.



11
12
13
14
# File 'lib/hover/client/http.rb', line 11

def initialize(site = 'http://localhost:3000', prefix = 'api/v1')
  @prefix = prefix
  @site = site
end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



9
10
11
# File 'lib/hover/client/http.rb', line 9

def prefix
  @prefix
end

#siteObject

Returns the value of attribute site.



9
10
11
# File 'lib/hover/client/http.rb', line 9

def site
  @site
end

Instance Method Details

#authenticate(request) ⇒ Object



16
17
# File 'lib/hover/client/http.rb', line 16

def authenticate(request)
end

#delete(path, parameters = {}) ⇒ Object



84
85
86
87
88
# File 'lib/hover/client/http.rb', line 84

def delete(path, parameters = {})
  uri = make_uri(path)
  request = Net::HTTP::Delete.new(uri)
  make_form_request(request, uri, parameters)
end

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



40
41
42
43
44
45
46
# File 'lib/hover/client/http.rb', line 40

def get(path, parameters = {})
  uri = make_uri(path, parameters)
  request = Net::HTTP::Get.new(uri)
  response = make_request(request, uri)

  response
end

#get_redirect_location(path, parameters = {}, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hover/client/http.rb', line 48

def get_redirect_location(path, parameters = {}, &block)
  uri = make_uri(path, parameters)
  request = Net::HTTP::Get.new(uri)
  redirect_response = make_request(request, uri)

  if redirect_response.is_a?(Net::HTTPRedirection) && redirect_response['location']
    redirect_response['location']
  else
    nil
  end
end

#make_form_request(request, uri, parameters) ⇒ Object



66
67
68
69
70
# File 'lib/hover/client/http.rb', line 66

def make_form_request(request, uri, parameters)
  request.set_form_data(parameters)
  response = make_request(request, uri)
  response
end

#make_request(request, uri) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hover/client/http.rb', line 27

def make_request(request, uri)
  authenticate(request)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if 'https' == uri.scheme

  response = http.request(request)

  raise_errors_for_cloudfront_response(response)

  response
end

#make_uri(path, parameters = {}) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/hover/client/http.rb', line 19

def make_uri(path, parameters = {})
  parts = [site, prefix, path]
  uri = URI.parse(parts.map(&:presence).compact.join('/'))
  uri = URI.parse("#{uri.to_s}?#{URI.encode_www_form(parameters)}") unless parameters.empty?

  uri
end

#parse_response(response) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/hover/client/http.rb', line 90

def parse_response(response)
  raise_errors(response)

  case response.code.to_i
    when 204 # no content
      {}
    else
      ::JSON.parse(response.body)
  end
end

#patch(path, parameters = {}) ⇒ Object



78
79
80
81
82
# File 'lib/hover/client/http.rb', line 78

def patch(path, parameters = {})
  uri = make_uri(path)
  request = Net::HTTP::Patch.new(uri)
  make_form_request(request, uri, parameters)
end

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



60
61
62
63
64
# File 'lib/hover/client/http.rb', line 60

def post(path, parameters = {})
  uri = make_uri(path)
  request = Net::HTTP::Post.new(uri)
  make_form_request(request, uri, parameters)
end

#put(path, parameters = {}) ⇒ Object



72
73
74
75
76
# File 'lib/hover/client/http.rb', line 72

def put(path, parameters = {})
  uri = make_uri(path)
  request = Net::HTTP::Put.new(uri)
  make_form_request(request, uri, parameters)
end

#raise_errors(response) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hover/client/http.rb', line 113

def raise_errors(response)
  message = "(#{response.code}): - #{response.body}"

  case response.code.to_i
    when 400
      raise BadRequest, message
    when 401
      raise Unauthorized, message
    when 403
      raise General, message
    when 404
      raise NotFound, message
    when 500
      raise InternalError, "Internal error: #{message}"
    when 502..503
      raise Unavailable, message
  end
end

#raise_errors_for_cloudfront_response(response) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hover/client/http.rb', line 101

def raise_errors_for_cloudfront_response(response)
  #
  # If the server can't be reached cloudfront will respond with an
  # error message of it's own. We need to catch those so that we can retry
  # the requests.
  #

  return unless (response.header['Server'] =~ /Cloudfront/i)

  raise ::Hover::Client::Unavailable
end