Class: Point::Request
- Inherits:
-
Object
- Object
- Point::Request
- Defined in:
- lib/point/request.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path, method = :get) ⇒ Request
constructor
A new instance of Request.
-
#make ⇒ Object
Make a request to the Point API using net/http.
- #output ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(path, method = :get) ⇒ Request
Returns a new instance of Request.
7 8 9 10 |
# File 'lib/point/request.rb', line 7 def initialize(path, method = :get) @path = path @method = method end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/point/request.rb', line 5 def data @data end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
4 5 6 |
# File 'lib/point/request.rb', line 4 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/point/request.rb', line 4 def path @path end |
Instance Method Details
#make ⇒ Object
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 |
#output ⇒ Object
16 17 18 |
# File 'lib/point/request.rb', line 16 def output @output || nil end |
#success? ⇒ Boolean
12 13 14 |
# File 'lib/point/request.rb', line 12 def success? @success || false end |