Sha256: d15b7bf5413913202d4d45aaf862e5973ff00d136c55b8a4f264d941ffa29e3f
Contents?: true
Size: 1.84 KB
Versions: 3
Compression:
Stored size: 1.84 KB
Contents
module Trumpet module Request @@http = Net::HTTP.new('api.trumpet.io', 3000) @@http_auth = {} def self.authenticate(username, password) @@http_auth[:username] = username @@http_auth[:password] = password end def self.get(path, options={}) do_request(Net::HTTP::Get, path, options) end def self.post(path, options={}) do_request(Net::HTTP::Post, path, options) end def self.put(path, options={}) do_request(Net::HTTP::Put, path, options) end def self.delete(path, options={}) do_request(Net::HTTP::Delete, path, options) end def self.set_server(uri, port=nil) @@http = Net::HTTP.new(uri.gsub('http://', ''), port) #no http:// in hosts end private def self.do_request(http_method, path, options) raw_request = http_method.new(path) raw_request.basic_auth(@@http_auth[:username], @@http_auth[:password]) unless @@http_auth.empty? raw_request.body = options[:parameters].to_params if options[:parameters] response = @@http.request(raw_request) unless response.code.to_i < 400 error_string = JSON.parse(response.body).to_s case response.code.to_i when 400 raise Trumpet::BadRequest, error_string when 401 raise Trumpet::Unauthorized, error_string when 403 raise Trumpet::Forbidden, error_string when 404 raise Trumpet::NotFound, error_string when 405 raise Trumpet::MethodNotAllowed, error_string when 500 raise Trumpet::InternalServerError, error_string when 501 raise Trumpet::NotImplemented, error_string end end (options[:parse_response] == false) ? response : JSON.parse(response.body) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
trumpet-trumpet-0.0.10 | lib/trumpet/request.rb |
trumpet-trumpet-0.0.11 | lib/trumpet/request.rb |
trumpet-trumpet-0.0.9 | lib/trumpet/request.rb |