Module: UCEngine::NetHttpRequest
- Included in:
- Client, Client::Session
- Defined in:
- lib/em-ucengine/client_nethttp.rb
Instance Method Summary collapse
- #delete(path, params = nil) ⇒ Object
- #get(path, params = nil) ⇒ Object
-
#json_post(path, body) ⇒ Object
Perform a post request on the API with a content type application/json.
- #post(path, params = nil, body = nil) ⇒ Object
Instance Method Details
#delete(path, params = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/em-ucengine/client_nethttp.rb', line 57 def delete(path, params=nil) uri = URI.parse(path) params ||= {} params.merge!({:uid => self.uid, :sid => self.sid}) query = params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&') if params Net::HTTP.start(uri.host, uri.port) do |http| http.delete("#{uri.path}?#{query}") end end |
#get(path, params = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/em-ucengine/client_nethttp.rb', line 37 def get(path, params=nil) uri = URI.parse(path) params ||= {} params.merge!({:uid => self.uid, :sid => self.sid}) query = params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&') if params Net::HTTP.start(uri.host, uri.port) do |http| http.get("#{uri.path}?#{query}") end end |
#json_post(path, body) ⇒ Object
Perform a post request on the API with a content type application/json
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/em-ucengine/client_nethttp.rb', line 72 def json_post(path, body) #http_request(:post, path, {}, body, nil, {'Content-Type' => 'application/json'}) uri = URI.parse(path) req = Net::HTTP::Post.new(uri.path) req.body = body.to_json req.add_field("Content-Type", "application/json") Net::HTTP.new(uri.host, uri.port).start do |http| http.request(req) end end |
#post(path, params = nil, body = nil) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/em-ucengine/client_nethttp.rb', line 48 def post(path, params=nil, body=nil) uri = URI.parse(path) body ||={} body.merge!(params) if params body.merge!({:uid => self.uid, :sid => self.sid}) if self.class == UCEngine::Client::Session Net::HTTP.post_form(uri, body) end |