Module: SimpleUri
- Defined in:
- lib/simple_uri.rb,
lib/simple_uri/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"- @@debug_mode =
false
Class Method Summary collapse
- .body_to_str_params(body) ⇒ Object
-
.connect(url: nil, method: nil, params: nil, user: nil, password: nil, debug: @@debug_mode) ⇒ Object
parameters:url-String, method-Symbol||String, params-String, user-String, password:String, debug-Boolean.
-
.send_request(url: nil, method: nil, params: nil, req_body: nil, req_headers: nil, user: nil, password: nil, debug: @@debug_mode, cookies: false) ⇒ Object
parameters:url-String, method-Symbol||String, params-String, req_body-String, req_headers-Hash, user-String, password:String, debug-Boolean, cookies-Boolean.
Class Method Details
.body_to_str_params(body) ⇒ Object
46 47 48 |
# File 'lib/simple_uri.rb', line 46 def body_to_str_params(body) body.map { |k, v| "#{k.to_s}=#{v.to_s}" }.join('&') end |
.connect(url: nil, method: nil, params: nil, user: nil, password: nil, debug: @@debug_mode) ⇒ Object
parameters:url-String, method-Symbol||String, params-String, user-String, password:String, debug-Boolean
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/simple_uri.rb', line 14 def connect(url: nil, method: nil, params: nil, user: nil, password: nil, debug: @@debug_mode) enable_debug_mode(debug) uri = URI.parse(URI.encode(url)) http = Net::HTTP.new(uri.host, uri.port) http.read_timeout = 1000 debug_http(http) if url.match(/^https/) debug_msg 'Use SSL' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end req = "Net::HTTP::#{method.to_s.capitalize}".constantize.new(uri.path+params.to_s) req.basic_auth user, password if user && password [req, http] end |
.send_request(url: nil, method: nil, params: nil, req_body: nil, req_headers: nil, user: nil, password: nil, debug: @@debug_mode, cookies: false) ⇒ Object
parameters:url-String, method-Symbol||String, params-String, req_body-String, req_headers-Hash, user-String, password:String, debug-Boolean, cookies-Boolean
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/simple_uri.rb', line 31 def send_request(url: nil, method: nil, params: nil, req_body: nil, req_headers: nil, user: nil, password: nil, debug: @@debug_mode, cookies: false) req, http = connect(url: url, method: method, params: params, user: user, password: password, debug: debug) req.body = req_body req_headers.each { |k, v| req[k] = v } if req_headers res = http.request(req) res.body res_body = begin JSON.parse(res.body) rescue debug_msg 'Can\'t convert response to JSON' res.body end ? { body: res_body, cookies: res.response['set-cookie'] } : res_body end |