module Helpers::Request

Constants

ENDPOINT
VERSION

Protected Instance Methods

delete(path, params = {}) click to toggle source
# File lib/onesky/helpers/request.rb, line 56
def delete(path, params = {})
  uri = uri_prefix + path
  params = {
    content_type: :json,
    params: auth_hash.merge(params)
  }.merge(custom_header)

  RestClient.delete(uri, params) do |resp, req, result|
    handle_error(resp)
  end
end
get(path, params = {}) click to toggle source
# File lib/onesky/helpers/request.rb, line 11
def get(path, params = {})
  uri = uri_prefix + path
  params = {
    content_type: :json,
    params: auth_hash.merge(params),
  }.merge(custom_header)

  RestClient.get(uri, params) do |resp, req, result|
    handle_error(resp)
  end
end
post(path, body_hash) click to toggle source
# File lib/onesky/helpers/request.rb, line 23
def post(path, body_hash)
  uri = uri_prefix + path
  params = {
    content_type: :json,
    params: auth_hash,
  }.merge(custom_header)

  RestClient.post(uri, body_hash.to_json, params) do |resp, req, result|
    handle_error(resp)
  end
end
post_multipart(path, body_hash) click to toggle source
# File lib/onesky/helpers/request.rb, line 35
def post_multipart(path, body_hash)
  uri = uri_prefix + path
  params = {params: auth_hash}.merge(custom_header)

  RestClient.post(uri, body_hash.merge({multipart: true}), params) do |resp, req, result|
    handle_error(resp)
  end
end
put(path, body_hash) click to toggle source
# File lib/onesky/helpers/request.rb, line 44
def put(path, body_hash)
  uri = uri_prefix + path
  params = {
    content_type: :json,
    params: auth_hash,
  }.merge(custom_header)

  RestClient.put(uri, body_hash.to_json, params) do |resp, req, result|
    handle_error(resp)
  end
end

Private Instance Methods

custom_header() click to toggle source
# File lib/onesky/helpers/request.rb, line 103
def custom_header
  {'Onesky-Plugin' => @plugin_code}
end
extract_message(response) click to toggle source
# File lib/onesky/helpers/request.rb, line 95
def extract_message(response)
  response_body = JSON.parse(response)
  if response_body.has_key?('meta') && response_body['meta'].has_key?('message')
    return response_body['meta']['message']
  end
  ''
end
handle_error(response) click to toggle source
# File lib/onesky/helpers/request.rb, line 74
def handle_error(response)
  return response if response.code.to_i < 400 # return if not error

  case response.code.to_i
  when 400
    raise Onesky::Errors::BadRequestError.new(response), "400 Bad Request - #{extract_message(response)}"
  when 401
    raise Onesky::Errors::UnauthorizedError.new(response), "401 Unauthorized - #{extract_message(response)}"
  when 403
    raise Onesky::Errors::ForbiddenError.new(response), "403 Forbidden - #{extract_message(response)}"
  when 404
    raise Onesky::Errors::NotFoundError.new(response), "404 Not Found - #{extract_message(response)}"
  when 405
    raise Onesky::Errors::MethodNotAllowedError.new(response), "405 Method Not Allowed - #{extract_message(response)}"
  when 500
    raise Onesky::Errors::InternalServerError.new(response), "500 Internal Server Error - Please contact OneSky at support@oneskyapp.com"
  else
    raise Onesky::Errors::OneskyError.new(response), "#{response.code} Error"
  end
end
uri_prefix() click to toggle source
# File lib/onesky/helpers/request.rb, line 70
def uri_prefix
  "#{ENDPOINT}/#{VERSION}"
end