Class: Neko::HTTP
- Inherits:
-
Object
- Object
- Neko::HTTP
- Defined in:
- lib/neko-http.rb
Constant Summary collapse
- VERSION =
'20220224'.freeze
- METHOD_HTTP_CLASS =
{ get: Net::HTTP::Get, put: Net::HTTP::Put, patch: Net::HTTP::Patch, post: Net::HTTP::Post, delete: Net::HTTP::Delete }
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#init_uri ⇒ Object
readonly
Returns the value of attribute init_uri.
-
#logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
-
.get(url, params, hdrs = nil) ⇒ Hash
Simple GET request.
-
.post_form(url, params, hdrs = nil) ⇒ Object
Send POST request with form data URL encoded body.
-
.post_json(url, obj, hdrs = {}) ⇒ Object
Send POST request with JSON body It will set the Content-Type to application/json.
Instance Method Summary collapse
- #close ⇒ Object
- #delete(path: nil, params: nil, query: nil) ⇒ Object
- #get(path: nil, params: nil, query: nil) ⇒ Object
-
#initialize(url, hdrs = nil) ⇒ HTTP
constructor
Instance constructor for tailored use.
- #patch(path: nil, params: nil, body: nil, query: nil) ⇒ Object
- #post(path: nil, params: nil, body: nil, query: nil) ⇒ Object
- #put(path: nil, params: nil, body: nil, query: nil) ⇒ Object
Constructor Details
#initialize(url, hdrs = nil) ⇒ HTTP
Instance constructor for tailored use
81 82 83 84 85 86 87 88 89 |
# File 'lib/neko-http.rb', line 81 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 |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
76 77 78 |
# File 'lib/neko-http.rb', line 76 def headers @headers end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
75 76 77 |
# File 'lib/neko-http.rb', line 75 def http @http end |
#init_uri ⇒ Object (readonly)
Returns the value of attribute init_uri.
75 76 77 |
# File 'lib/neko-http.rb', line 75 def init_uri @init_uri end |
#logger ⇒ Object
Returns the value of attribute logger.
76 77 78 |
# File 'lib/neko-http.rb', line 76 def logger @logger end |
Class Method Details
.get(url, params, hdrs = nil) ⇒ Hash
Simple GET request
34 35 36 37 38 39 |
# File 'lib/neko-http.rb', line 34 def self.get(url, params, hdrs = nil) h = HTTP.new(url, hdrs) data = h.get(params: params) h.close return data end |
.post_form(url, params, hdrs = nil) ⇒ Object
Send POST request with form data URL encoded body
46 47 48 49 50 51 |
# File 'lib/neko-http.rb', line 46 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 = {}) ⇒ Object
Send POST request with JSON body It will set the Content-Type to application/json.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/neko-http.rb', line 59 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 |
Instance Method Details
#close ⇒ Object
111 112 113 |
# File 'lib/neko-http.rb', line 111 def close http.finish if http.started? end |
#delete(path: nil, params: nil, query: nil) ⇒ Object
107 108 109 |
# File 'lib/neko-http.rb', line 107 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) ⇒ Object
91 92 93 |
# File 'lib/neko-http.rb', line 91 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) ⇒ Object
103 104 105 |
# File 'lib/neko-http.rb', line 103 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) ⇒ Object
95 96 97 |
# File 'lib/neko-http.rb', line 95 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) ⇒ Object
99 100 101 |
# File 'lib/neko-http.rb', line 99 def put(path: nil, params: nil, body: nil, query: nil) return operate(__method__, path: path, params: params, body: body, query: query) end |