Class: CodebaseApi::Request
- Inherits:
-
Object
- Object
- CodebaseApi::Request
- Defined in:
- lib/codebase_api/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.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(path, method = :get) ⇒ Request
constructor
A new instance of Request.
- #make ⇒ Object
- #output ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(path, method = :get) ⇒ Request
Returns a new instance of Request.
13 14 15 16 |
# File 'lib/codebase_api/request.rb', line 13 def initialize(path, method = :get) @path = path @method = method end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
11 12 13 |
# File 'lib/codebase_api/request.rb', line 11 def data @data end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
10 11 12 |
# File 'lib/codebase_api/request.rb', line 10 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/codebase_api/request.rb', line 10 def path @path end |
Class Method Details
.request(path, method = :get, data = {}) ⇒ Object
4 5 6 7 8 |
# File 'lib/codebase_api/request.rb', line 4 def self.request(path, method=:get, data = {}) req = self.new(path, method) req.data = data req.make && req.success? ? req.output : false end |
Instance Method Details
#make ⇒ Object
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/codebase_api/request.rb', line 26 def make uri = URI.parse(["https://api3.codebasehq.com", @path].join('/')) http_request = http_class.new(uri.request_uri) http_request.initialize_http_header({"User-Agent" => "CodebaseApiRubyClient/#{CodebaseApi::VERSION}"}) http_request.initialize_http_header({"Accept" => "application/json"}) http_request.content_type = "application/json" http_request.basic_auth CodebaseApi.account_user, CodebaseApi.api_key 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, @data.to_json) if http_result.body == 'true' @output = true elsif http_result.body == 'false' @output = false else http_result.body.present? ? @output = JSON.parse(http_result.body) : @output end @success = case http_result when Net::HTTPSuccess true when Net::HTTPServiceUnavailable raise CodebaseApi::Errors::ServiceUnavailable when Net::HTTPForbidden, Net::HTTPUnauthorized raise CodebaseApi::Errors::AccessDenied, "Access Denied" when Net::HTTPNotFound json = JSON.parse(http_result.body) raise CodebaseApi::Errors::NotFound, json['error'] when Net::HTTPBadRequest, Net::HTTPUnauthorized, Net::HTTPMethodNotAllowed json = JSON.parse(http_result.body) raise CodebaseApi::Errors::AccessDenied, "Access Denied for '#{CodebaseApi.application}' #{json['error']}" else raise CodebaseApi::Errors::CommunicationError, http_result.body end # show detailed info about the request puts "[Codebase API] Sent: #{data}".yellow puts "[Codebase API] Requesting: #{[path].join('/')} on https://api3.codebasehq.com".yellow puts "[Codebase API] Response: #{http_result.body}".yellow self end |
#output ⇒ Object
22 23 24 |
# File 'lib/codebase_api/request.rb', line 22 def output @output || nil end |
#success? ⇒ Boolean
18 19 20 |
# File 'lib/codebase_api/request.rb', line 18 def success? @success || false end |