require 'net/http' module Amee class Service include HTTParty base_uri Amee::Config[:server] format :json attr_accessor :auth_token def initialize(auth_token) @auth_token = auth_token end def post(method, path, options = {}) perform_request(:post, method, path, options) end def get(method, path, options = {}) perform_request(:get, method, path, options) end def put(method, path, options = {}) perform_request(:put, method, path, options) end def delete(method, path, options = {}) perform_request(:delete, method, path, options) end def self.auth_token(username, password, path) response = Net::HTTP.post_form( URI.parse(self.base_uri + path), {'username'=> username, 'password'=>password} ) response['authToken'] end private # we want to attach the authToken to the headers of the request # and then check the status code def perform_request(type, method, path, options) attach_headers(options) response = self.class.send(type, path, options) check_response(response.code) Parser.parse(method, response) end def attach_headers(options) (options[:headers] ||= {}).merge!( { "authToken" => @auth_token, 'Accept' => options[:accept] || Amee::Config[:accept] } ) end # checking the status code of the response; if we are not authenticated # then authenticate the session # # @raise [Amee::PermissionDenied] if the status code is 403 # @raise [Amee::UnAuthorized] if a auth_token could not be generated # @raise [Amee::SessionExpired] if a we get a 401 # @raise [Amee::UnknownError] if we get something strange def check_response(code) case code when 200, 201 true when 403 raise Amee::Session::PermissionDenied.new("You do not have permission to perform the requested operation") when 401 raise Amee::Session::Expired.new("Session has expired") when 404 raise Amee::Session::NotFound.new("resource was not found") else raise Amee::Session::UnknownError.new("super strange type unknown error") end end end end