Class: SentryApi::Request
- Inherits:
-
Object
- Object
- SentryApi::Request
- Includes:
- HTTMultiParty
- Defined in:
- lib/sentry-api/request.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#auth_token ⇒ Object
Returns the value of attribute auth_token.
-
#default_org_slug ⇒ Object
Returns the value of attribute default_org_slug.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
Class Method Summary collapse
-
.decode(response) ⇒ Object
Decodes a JSON response into Ruby object.
-
.parse(body) ⇒ Object
Converts the response body to an ObjectifiedHash.
Instance Method Summary collapse
- #delete(path, options = {}) ⇒ Object
- #get(path, options = {}) ⇒ Object
- #post(path, options = {}) ⇒ Object
- #put(path, options = {}) ⇒ Object
-
#set_request_defaults ⇒ Object
Sets a base_uri and default_params for requests.
- #upload(path, options = {}) ⇒ Object
-
#validate(response) ⇒ Object
Checks the response code for common errors.
Instance Attribute Details
#auth_token ⇒ Object
Returns the value of attribute auth_token.
12 13 14 |
# File 'lib/sentry-api/request.rb', line 12 def auth_token @auth_token end |
#default_org_slug ⇒ Object
Returns the value of attribute default_org_slug.
12 13 14 |
# File 'lib/sentry-api/request.rb', line 12 def default_org_slug @default_org_slug end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
12 13 14 |
# File 'lib/sentry-api/request.rb', line 12 def endpoint @endpoint end |
Class Method Details
.decode(response) ⇒ Object
Decodes a JSON response into Ruby object.
38 39 40 41 42 |
# File 'lib/sentry-api/request.rb', line 38 def self.decode(response) JSON.load response rescue JSON::ParserError raise Error::Parsing.new "The response is not a valid JSON" end |
.parse(body) ⇒ Object
Converts the response body to an ObjectifiedHash.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sentry-api/request.rb', line 15 def self.parse(body) body = decode(body) if body.is_a? Hash ObjectifiedHash.new body elsif body.is_a? Array if body[0].is_a? Array body else PaginatedResponse.new(body.collect! { |e| ObjectifiedHash.new(e) }) end elsif body true elsif !body false elsif body.nil? false else raise Error::Parsing.new "Couldn't parse a response body" end end |
Instance Method Details
#delete(path, options = {}) ⇒ Object
64 65 66 67 68 |
# File 'lib/sentry-api/request.rb', line 64 def delete(path, ={}) set_httparty_config() () validate self.class.delete(@endpoint + path, ) end |
#get(path, options = {}) ⇒ Object
44 45 46 47 48 |
# File 'lib/sentry-api/request.rb', line 44 def get(path, ={}) set_httparty_config() () validate self.class.get(@endpoint + path, ) end |
#post(path, options = {}) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/sentry-api/request.rb', line 50 def post(path, ={}) set_httparty_config() set_json_body() (, path) validate self.class.post(@endpoint + path, ) end |
#put(path, options = {}) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/sentry-api/request.rb', line 57 def put(path, ={}) set_httparty_config() set_json_body() () validate self.class.put(@endpoint + path, ) end |
#set_request_defaults ⇒ Object
Sets a base_uri and default_params for requests.
112 113 114 115 |
# File 'lib/sentry-api/request.rb', line 112 def set_request_defaults self.class.default_params raise Error::MissingCredentials.new("Please set an endpoint to API") unless @endpoint end |
#upload(path, options = {}) ⇒ Object
70 71 72 73 74 |
# File 'lib/sentry-api/request.rb', line 70 def upload(path, ={}) set_httparty_config() () validate self.class.post(@endpoint + path, ) end |
#validate(response) ⇒ Object
Checks the response code for common errors. Returns parsed response for successful requests.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/sentry-api/request.rb', line 78 def validate(response) error_klass = case response.code when 400 then Error::BadRequest when 401 then Error::Unauthorized when 403 then Error::Forbidden when 404 then Error::NotFound when 405 then Error::MethodNotAllowed when 409 then Error::Conflict when 422 then Error::Unprocessable when 500 then Error::InternalServerError when 502 then Error::BadGateway when 503 then Error::ServiceUnavailable end fail error_klass.new(response) if error_klass parsed = response.parsed_response parsed.client = self if parsed.respond_to?(:client=) parsed.parse_headers!(response.headers) if parsed.respond_to?(:parse_headers!) parsed end |