Sha256: c6bed14136f57e7c4182cd08d82e97dafb070b3e0057fed5b2781908b0f36998
Contents?: true
Size: 1.61 KB
Versions: 1
Compression:
Stored size: 1.61 KB
Contents
class HttpClient require 'net/http' def initialize(host, port, namespace) @http = Net::HTTP.new(host, port) @host = host @port = port @namespace = namespace end def send_request(method, resource, headers=nil, data=nil) build_response(self.send(method.to_s.downcase, headers, resource, data)) end protected # returns struct containing response.code, headers, body and message # this is only for easily interfaceing another http client def build_response(raw_response) debugger response_struct = Struct.new(:code, :message, :headers, :body) response = response_struct.new response.code = raw_response.code response.message = raw_response.message response.body = raw_response.body # response.headers = raw_response.header # TODO improve me! response.headers = JSON.parse(raw_response.header.to_json) rescue nil response end def get(headers, resource, params) request = Net::HTTP::Get.new(resource_path(resource), initheader = headers) @http.request(request) end def put(headers, resource, data) request = Net::HTTP::Put.new(resource_path(resource), initheader = headers) request.body = data.to_json @http.request(request) end def post(headers, resource, data) request = Net::HTTP::Post.new(resource_path(resource), initheader = headers) request.body = data.to_json @http.request(request) end def delete(headers, resource, params) request = Net::HTTP::Delete.new(resource_path(resource), initheader = headers) @http.request(request) end def resource_path(resource) "/" + @namespace + resource end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
apirunner-0.2.0 | lib/http_client.rb |