Module: VC::HttpHelpers
Instance Attribute Summary collapse
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #build_q_params(hash) ⇒ Object
- #dec_json(json) ⇒ Object
- #enc_json(hash) ⇒ Object
- #handle_req(&blk) ⇒ Object
- #headers ⇒ Object
Instance Attribute Details
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/helpers/http_helpers.rb', line 5 def url @url end |
Instance Method Details
#build_q_params(hash) ⇒ Object
10 11 12 13 14 |
# File 'lib/helpers/http_helpers.rb', line 10 def build_q_params(hash) "?" + hash.map do |k, v| CGI.escape("#{k}=#{v}") end.join("&") end |
#dec_json(json) ⇒ Object
24 25 26 |
# File 'lib/helpers/http_helpers.rb', line 24 def dec_json(json) Yajl::Parser.parse(json) end |
#enc_json(hash) ⇒ Object
20 21 22 |
# File 'lib/helpers/http_helpers.rb', line 20 def enc_json(hash) Yajl::Encoder.encode(hash) end |
#handle_req(&blk) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/helpers/http_helpers.rb', line 28 def handle_req(&blk) begin resp = yield dec_json(resp.body) rescue RestClient::Exception => e body = dec_json(e.http_body) case e.http_code when 404 when 401 raise(AuthorizationError, "Response: #{body.inspect}") when 403 when 409 when 500 raise(UnexpectedError, "Response: #{body.inspect}") when 503 raise(ServiceDownError, "#{VC.url} is down for maintenance.") else raise(UnexpectedError, "Response: #{body.inspect}") end end end |
#headers ⇒ Object
16 17 18 |
# File 'lib/helpers/http_helpers.rb', line 16 def headers {:content_type => :json, :accept => :json} end |