class Neko::HTTP
Constants
- METHOD_HTTP_CLASS
Attributes
headers[RW]
http[R]
init_uri[R]
logger[RW]
Public Class Methods
get(url, params, hdrs = nil)
click to toggle source
# File lib/neko-http.rb, line 28 def self.get(url, params, hdrs = nil) h = HTTP.new(url, hdrs) data = h.get(params: params) h.close return data end
new(url, hdrs = nil)
click to toggle source
# File lib/neko-http.rb, line 66 def initialize(url, hdrs = nil) @logger = Neko.logger @init_uri = URI(url) raise ArgumentError, 'Invalid URL' unless @init_uri.class <= URI::HTTP @http = Net::HTTP.new(init_uri.host, init_uri.port) http.use_ssl = init_uri.scheme == 'https' http.verify_mode = OpenSSL::SSL::VERIFY_PEER @headers = hdrs end
post_form(url, params, hdrs = nil)
click to toggle source
# File lib/neko-http.rb, line 35 def self.post_form(url, params, hdrs = nil) h = HTTP.new(url, hdrs) data = h.post(params: params) h.close return data end
post_json(url, obj, hdrs = {})
click to toggle source
Send POST request with JSON body It will set the Content-Type to application/json. @param url [String] full URL string @param obj [Array, Hash, String] Array/Hash will be converted to JSON @param hdrs [Array, Hash, String] Array/Hash will be converted to JSON
# File lib/neko-http.rb, line 47 def self.post_json(url, obj, hdrs = {}) hdrs['Content-Type'] = 'application/json' h = HTTP.new(url, hdrs) case obj when Array, Hash body = JSON.fast_generate(obj) when String body = obj else raise ArgumentError, 'Argument is neither Array, Hash, String' end data = h.post(body: body) h.close return data end
Public Instance Methods
close()
click to toggle source
# File lib/neko-http.rb, line 96 def close http.finish if http.started? end
delete(path: nil, params: nil, query: nil)
click to toggle source
# File lib/neko-http.rb, line 92 def delete(path: nil, params: nil, query: nil) return operate(__method__, path: path, params: params, query: query) end
get(path: nil, params: nil, query: nil)
click to toggle source
# File lib/neko-http.rb, line 76 def get(path: nil, params: nil, query: nil) return operate(__method__, path: path, params: params, query: query) end
patch(path: nil, params: nil, body: nil, query: nil)
click to toggle source
# File lib/neko-http.rb, line 88 def patch(path: nil, params: nil, body: nil, query: nil) return operate(__method__, path: path, params: params, body: body, query: query) end
post(path: nil, params: nil, body: nil, query: nil)
click to toggle source
# File lib/neko-http.rb, line 80 def post(path: nil, params: nil, body: nil, query: nil) return operate(__method__, path: path, params: params, body: body, query: query) end
put(path: nil, params: nil, body: nil, query: nil)
click to toggle source
# File lib/neko-http.rb, line 84 def put(path: nil, params: nil, body: nil, query: nil) return operate(__method__, path: path, params: params, body: body, query: query) end
Private Instance Methods
handle_response(res)
click to toggle source
# File lib/neko-http.rb, line 158 def handle_response(res) if res.connection_close? logger.info('HTTP response header says connection close; closing session now') close end case res when Net::HTTPRedirection logger.info('HTTP response was a redirect') data = URI(res['Location']) if data.class == URI::Generic data = uri_with_path(res['Location']) logger.debug("Full URI object built for local redirect with path: #{data.path}") end # when Net::HTTPSuccess # when Net::HTTPClientError # when Net::HTTPServerError else data = { code: res.code.to_i, headers: res.to_hash, body: res.body, message: res.msg } end return data end
inject_headers_to(req)
click to toggle source
# File lib/neko-http.rb, line 152 def inject_headers_to(req) return if headers.nil? headers.each { |k, v| req[k] = v } logger.info('Header injected into HTTP request header') end
operate(method, path: nil, params: nil, body: nil, query: nil)
click to toggle source
# File lib/neko-http.rb, line 102 def operate(method, path: nil, params: nil, body: nil, query: nil) uri = uri_with_path(path) case method when :get, :delete if params query = URI.encode_www_form(params) logger.info('Created urlencoded query from params') end uri.query = query if query req = METHOD_HTTP_CLASS[method].new(uri) when :put, :patch, :post uri.query = query if query req = METHOD_HTTP_CLASS[method].new(uri) if params req.form_data = params logger.info('Created form data from params') elsif body req.body = body end else return nil end if uri.userinfo req.basic_auth(uri.user, uri.password) logger.info('Created basic auth header from URL') end data = send(req) data = redirect(method, uri: data, body: req.body) if data.class <= URI::HTTP return data end
redirect(method, uri:, body: nil)
click to toggle source
# File lib/neko-http.rb, line 185 def redirect(method, uri:, body: nil) if uri.host == init_uri.host && uri.port == init_uri.port logger.info("Local #{method.upcase} redirect, reusing HTTP session") new_http = self else logger.info("External #{method.upcase} redirect, spawning new HTTP object") new_http = HTTP.new("#{uri.scheme}://#{uri.host}#{uri.path}", headers) end new_http.__send__(:operate, method, path: uri.path, body: body, query: uri.query) end
send(req)
click to toggle source
# File lib/neko-http.rb, line 139 def send(req) inject_headers_to(req) unless http.started? logger.info('HTTP session not started; starting now') http.start logger.debug("Opened connection to #{http.address}:#{http.port}") end logger.debug("Sending HTTP #{req.method} request to #{req.path}") logger.debug("Body size: #{req.body.length}") if req.request_body_permitted? res = http.request(req) return handle_response(res) end
uri_with_path(path)
click to toggle source
# File lib/neko-http.rb, line 133 def uri_with_path(path) uri = init_uri.clone uri.path = path unless path.nil? return uri end