Method: Point::Request#make

Defined in:
lib/point/request.rb

#makeObject

 Make a request to the Point API using net/http. Data passed can be a hash or a string Hashes will be converted to JSON before being sent to the remote service.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/point/request.rb', line 22

def make
  uri = URI.parse([Point.site, @path].join('/'))
  http_request = http_class.new(uri.request_uri)
  http_request.basic_auth(Point.username, Point.apitoken)
  http_request.add_field("Accept", "application/json")
  http_request.add_field("Content-type", "application/json")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  data = self.data.to_json if self.data.is_a?(Hash) && self.data.respond_to?(:to_json)
  http_result = http.request(http_request, data)
  @output = http_result.body
  @success = case http_result
  when Net::HTTPSuccess
    true
  when Net::HTTPServiceUnavailable
    raise Point::Errors::ServiceUnavailable
  when Net::HTTPForbidden, Net::HTTPUnauthorized
    raise Point::Errors::AccessDenied, "Access Denied for '#{Point.username}'"
  when Net::HTTPNotFound
    raise Point::Errors::CommunicationError, "Not Found at #{uri.to_s}"
  when Net::HTTPPaymentRequired
    raise Point::Errors::AccessDenied, JSON.parse(output)['message']
  when Net::HTTPClientError
    false
  else
    raise Point::Errors::CommunicationError, http_result.body
  end
  self
end