Module: Shushu::HttpHelpers

Extended by:
HttpHelpers
Included in:
Shushu, HttpHelpers
Defined in:
lib/helpers/http_helpers.rb

Instance Method Summary collapse

Instance Method Details

#build_q_params(hash) ⇒ Object



5
6
7
8
9
# File 'lib/helpers/http_helpers.rb', line 5

def build_q_params(hash)
  "?" + hash.map do |k, v|
    CGI.escape("#{k}=#{v}")
  end.join("&")
end

#dec_json(json) ⇒ Object



22
23
24
# File 'lib/helpers/http_helpers.rb', line 22

def dec_json(json)
  Yajl::Parser.parse(json)
end

#enc_json(hash) ⇒ Object



18
19
20
# File 'lib/helpers/http_helpers.rb', line 18

def enc_json(hash)
  Yajl::Encoder.encode(hash)
end

#handle_req(&blk) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/helpers/http_helpers.rb', line 26

def handle_req(&blk)
  begin
    resp = yield
    dec_json(resp.body)
  rescue RestClient::Exception => e
    body = dec_json(e.http_body)
    code = e.http_code
    raise(ShushuException, "Error in HTTP request. code=#{code} body=#{body}")
  end
end

#headersObject



11
12
13
14
15
16
# File 'lib/helpers/http_helpers.rb', line 11

def headers
  {
    :content_type => :json,
    :accept       => :json
  }
end