Class: Sirportly::Request
- Inherits:
-
Object
- Object
- Sirportly::Request
- Defined in:
- lib/sirportly/request.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#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.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(client, path, method = :get) ⇒ Request
constructor
A new instance of Request.
- #make ⇒ Object
- #output ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(client, path, method = :get) ⇒ Request
Returns a new instance of Request.
13 14 15 16 17 |
# File 'lib/sirportly/request.rb', line 13 def initialize(client, path, method = :get) @path = path @method = method @client = client end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/sirportly/request.rb', line 10 def client @client end |
#data ⇒ Object
Returns the value of attribute data.
11 12 13 |
# File 'lib/sirportly/request.rb', line 11 def data @data end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
10 11 12 |
# File 'lib/sirportly/request.rb', line 10 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/sirportly/request.rb', line 10 def path @path end |
Class Method Details
.request(client, path, data = {}) ⇒ Object
4 5 6 7 8 |
# File 'lib/sirportly/request.rb', line 4 def self.request(client, path, data = {}) req = self.new(client, path, :post) req.data = data req.make && req.success? ? req.output : false end |
Instance Method Details
#make ⇒ Object
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sirportly/request.rb', line 27 def make uri = URI.parse([domain, "api/v1", @path].join('/')) http_request = http_req(uri, @data.stringify_keys) http_request.add_field("User-Agent", "SirportlyRubyClient/#{Sirportly::VERSION}") http_request.add_field("X-Auth-Token", @client.token) http_request.add_field("X-Auth-Secret", @client.secret) http_request.add_field("X-Sirportly-Rules", "disabled") if Sirportly.execute_rules == false if application http_request.add_field("X-Auth-Application", application) end http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http_result = http.request(http_request) if http_result.body == 'true' @output = true elsif http_result.body == 'false' @output = false elsif http_result.content_type == 'application/json' @output = JSON.parse(http_result.body) else @output = http_result.body end @success = case http_result when Net::HTTPSuccess true when Net::HTTPServiceUnavailable raise Sirportly::Errors::ServiceUnavailable when Net::HTTPForbidden, Net::HTTPUnauthorized puts http_result.body.inspect raise Sirportly::Errors::AccessDenied, "Access Denied for '#{@client.token}'" when Net::HTTPNotFound json = JSON.parse(http_result.body) raise Sirportly::Errors::NotFound, json['error'] when Net::HTTPClientError json = JSON.parse(http_result.body) raise Sirportly::Errors::ValidationError, json['errors'].to_s else raise Sirportly::Errors::CommunicationError, http_result.body end self end |
#output ⇒ Object
23 24 25 |
# File 'lib/sirportly/request.rb', line 23 def output @output || nil end |
#success? ⇒ Boolean
19 20 21 |
# File 'lib/sirportly/request.rb', line 19 def success? @success || false end |