Class: Thunk::Server
- Inherits:
-
Object
- Object
- Thunk::Server
- Defined in:
- lib/thunk/server.rb
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #create(name = nil) ⇒ Object
- #get(uuid) ⇒ Object
-
#initialize(url = "http://thunk.us") ⇒ Server
constructor
A new instance of Server.
- #request(method, url, options = {}) ⇒ Object
Constructor Details
#initialize(url = "http://thunk.us") ⇒ Server
Returns a new instance of Server.
17 18 19 |
# File 'lib/thunk/server.rb', line 17 def initialize url = "http://thunk.us" @url = url end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
15 16 17 |
# File 'lib/thunk/server.rb', line 15 def url @url end |
Instance Method Details
#create(name = nil) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/thunk/server.rb', line 21 def create name = nil json = request :post, url, :name => name url = "#{self.url}/#{json['uuid']}" Thunk.new self, json end |
#get(uuid) ⇒ Object
28 29 30 31 |
# File 'lib/thunk/server.rb', line 28 def get uuid url = "#{self.url}/#{uuid}" Thunk.new self, request(:get, url) end |
#request(method, url, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/thunk/server.rb', line 33 def request method, url, = {} url = URI.parse url unless .empty? url.query = .collect { |k ,v| "#{CGI.escape k.to_s}=#{CGI.escape v.to_s}" }.join "&" end # FIX http timeout settings # FIX exception handing, etc http = Net::HTTP.new url.host, url.port rclass = { :get => Net::HTTP::Get, :post => Net::HTTP::Post }[method] req = rclass.new url.request_uri res = http.request req success = %w(200 201).include? res.code unless success warn "[Thunk] #{res.code}, #{method}, #{url}: #{res.}" warn res.body if res.body end return success if res.body.empty? data = JSON.parse(res.body) rescue {} unless Hash === data warn "[Thunk] Malformed JSON response: #{data.inspect}" data = {} end warn warning if warning = data.delete("warn") abort critical if critical = data.delete("abort") data end |