Module: WebPurify::Request
- Defined in:
- lib/web_purify/request.rb
Overview
WebPurify::Request
WebPurify::Request handles the HTTP/HTTPS queries to the API endpoints, converting returned JSON into a usable object.
Defined Under Namespace
Classes: RequestError
Constant Summary collapse
- WRAPPER =
:rsp
Class Method Summary collapse
-
.get(uri) ⇒ String
Handles making the query according to http or https scheme.
-
.query(request_base, query_base, params) ⇒ Hash
TODO: Request handling can be dramatically simplified using ‘open-uri’.
-
.to_query(hash) ⇒ String
Converts a hash of key/values into a url-ready query string.
Class Method Details
.get(uri) ⇒ String
Handles making the query according to http or https scheme
68 69 70 |
# File 'lib/web_purify/request.rb', line 68 def self.get(uri) URI.parse(uri.to_s).read end |
.query(request_base, query_base, params) ⇒ Hash
TODO: Request handling can be dramatically simplified using ‘open-uri’
Executes a query to the API endpoint
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/web_purify/request.rb', line 46 def self.query(request_base, query_base, params) q = query_base.merge(params) uri_builder = (request_base[:scheme]=='https') ? URI::HTTPS : URI::HTTP uri = uri_builder.build( :host => request_base[:host], :path => request_base[:path], :query => WebPurify::Request.to_query(q) ) res = JSON.parse(WebPurify::Request.get(uri), :symbolize_names => true)[WRAPPER] if res[:err] err_attrs = res[:err][:@attributes] raise RequestError.new(err_attrs[:code], err_attrs[:msg]) else res end end |
.to_query(hash) ⇒ String
Converts a hash of key/values into a url-ready query string
33 34 35 |
# File 'lib/web_purify/request.rb', line 33 def self.to_query(hash) return URI.encode_www_form(hash) end |