Class: Bolster::Server
- Inherits:
-
Object
- Object
- Bolster::Server
- Defined in:
- lib/bolster.rb
Instance Method Summary collapse
- #delete(uri) ⇒ Object
- #full_uri(uri) ⇒ Object
- #get(uri) ⇒ Object
- #handle_error(req, res) ⇒ Object
-
#initialize(uri, options = {}) ⇒ Server
constructor
A new instance of Server.
- #post(uri, json) ⇒ Object
- #put(uri, json) ⇒ Object
- #request(req) ⇒ Object
Constructor Details
#initialize(uri, options = {}) ⇒ Server
Returns a new instance of Server.
7 8 9 10 |
# File 'lib/bolster.rb', line 7 def initialize(uri, = {}) @uri = URI(uri) @options = end |
Instance Method Details
#delete(uri) ⇒ Object
12 13 14 15 |
# File 'lib/bolster.rb', line 12 def delete(uri) res = request(Net::HTTP::Delete.new(full_uri(uri))) res.body end |
#full_uri(uri) ⇒ Object
36 37 38 39 |
# File 'lib/bolster.rb', line 36 def full_uri(uri) full_uri = @uri.request_uri + uri full_uri.gsub!('//','/') end |
#get(uri) ⇒ Object
17 18 19 20 |
# File 'lib/bolster.rb', line 17 def get(uri) res = request(Net::HTTP::Get.new(full_uri(uri))) res.body end |
#handle_error(req, res) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/bolster.rb', line 59 def handle_error(req, res) e = RuntimeError.new(%Q{#{res.code}: #{res.} METHOD: #{req.method} URI: #{req.path} #{req.body}}) raise e end |
#post(uri, json) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/bolster.rb', line 29 def post(uri, json) req = Net::HTTP::Post.new(full_uri(uri)) req['content-type'] = 'application/json' req.body = json request(req) end |
#put(uri, json) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/bolster.rb', line 22 def put(uri, json) req = Net::HTTP::Put.new(full_uri(uri)) req['content-type'] = 'application/json' req.body = json request(req) end |
#request(req) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bolster.rb', line 41 def request(req) http = Net::HTTP.new(@uri.hostname, @uri.port) if @uri.class == URI::HTTPS require 'net/https' http.use_ssl = true end if @options.has_key?(:user) && @options.has_key?(:password) req.basic_auth(@options[:user], @options[:password]) end res = http.start { |h| h.request(req) } unless res.kind_of?(Net::HTTPSuccess) handle_error(req, res) end res end |