module Minimart::Utils::Http
A collection of methods to help issue HTTP requests
Public Class Methods
build_url(base_url, path=nil)
click to toggle source
Build a URL from a base URL, and a path @param [String] base_url The base URL to hit @param [String] path
# File lib/minimart/utils/http.rb, line 43 def self.build_url(base_url, path=nil) result = (base_url =~ /\A[a-z].*:\/\//i) ? base_url : "http://#{base_url}" result = URI.parse(result).to_s result = concat_url_fragment(result, path) return result end
concat_url_fragment(frag_one, frag_two)
click to toggle source
# File lib/minimart/utils/http.rb, line 50 def self.concat_url_fragment(frag_one, frag_two) return frag_one unless frag_two result = frag_one result << '/' unless result[-1] == '/' result << ((frag_two[0] == '/') ? frag_two[1..-1] : frag_two) result end
get(base_url, path=nil, headers={})
click to toggle source
Issue a GET request to a URL @param [String] base_url The base URL to hit @param [String] path The path to the RESTful resource to fetch
# File lib/minimart/utils/http.rb, line 20 def self.get(base_url, path=nil, headers={}) headers = headers.merge(verify_ssl: Minimart::Configuration.verify_ssl) resource = RestClient::Resource.new(base_url.to_s) path ? resource[path].get(headers) : resource.get(headers) end
get_binary(base_name, url, path=nil)
click to toggle source
GET a binary file @param [String] base_name A base name to give the returned file @param [String] url The base URL to hit @param [String] path The path to the RESTful resource to fetch @return [Tempfile]
# File lib/minimart/utils/http.rb, line 32 def self.get_binary(base_name, url, path=nil) result = Tempfile.new(base_name) result.binmode result.write(get(url, path)) result.close(false) result end
get_json(url, path=nil)
click to toggle source
Issue a GET request to a URL, and return parsed JSON. @param [String] url The base URL to hit @param [String] path The path to the RESTful resource to fetch @return [Hash] The parsed JSON response
# File lib/minimart/utils/http.rb, line 13 def self.get_json(url, path=nil) JSON.parse(get(url, path, accept: 'application/json')) end