class SentryApi::Request

@private

Attributes

auth_token[RW]
default_org_slug[RW]
endpoint[RW]

Public Class Methods

decode(response) click to toggle source

Decodes a JSON response into Ruby object.

# 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) click to toggle source

Converts the response body to an ObjectifiedHash.

# 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

Public Instance Methods

delete(path, options={}) click to toggle source
# File lib/sentry-api/request.rb, line 64
def delete(path, options={})
  set_httparty_config(options)
  set_authorization_header(options)
  validate self.class.delete(@endpoint + path, options)
end
get(path, options={}) click to toggle source
# File lib/sentry-api/request.rb, line 44
def get(path, options={})
  set_httparty_config(options)
  set_authorization_header(options)
  validate self.class.get(@endpoint + path, options)
end
post(path, options={}) click to toggle source
# File lib/sentry-api/request.rb, line 50
def post(path, options={})
  set_httparty_config(options)
  set_json_body(options)
  set_authorization_header(options, path)
  validate self.class.post(@endpoint + path, options)
end
put(path, options={}) click to toggle source
# File lib/sentry-api/request.rb, line 57
def put(path, options={})
  set_httparty_config(options)
  set_json_body(options)
  set_authorization_header(options)
  validate self.class.put(@endpoint + path, options)
end
set_request_defaults() click to toggle source

Sets a base_uri and default_params for requests. @raise [Error::MissingCredentials] if endpoint not set.

# 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={}) click to toggle source
# File lib/sentry-api/request.rb, line 70
def upload(path, options={})
  set_httparty_config(options)
  set_authorization_header(options)
  validate self.class.post(@endpoint + path, options)
end
validate(response) click to toggle source

Checks the response code for common errors. Returns parsed response for successful requests.

# 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

Private Instance Methods

set_authorization_header(options, path=nil) click to toggle source

Sets a Authorization header for requests. @raise [Error::MissingCredentials] if auth_token and auth_token are not set.

# File lib/sentry-api/request.rb, line 121
def set_authorization_header(options, path=nil)
  unless path == '/session'
    raise Error::MissingCredentials.new("Please provide a auth_token for user") unless @auth_token
    options[:headers] = {'Authorization' => "Bearer #{@auth_token}"}
  end
end
set_httparty_config(options) click to toggle source

Set HTTParty configuration @see github.com/jnunemaker/httparty

# File lib/sentry-api/request.rb, line 138
def set_httparty_config(options)
  options.merge!(httparty) if httparty
end
set_json_body(options) click to toggle source

Set http post or put body as json string if content type is application/json

# File lib/sentry-api/request.rb, line 129
def set_json_body(options)
  headers = self.class.headers
  if headers and headers["Content-Type"] == "application/json"
    options[:body] = options[:body].to_json
  end
end