class Spout::Helpers::JsonRequest
Generates JSON web requests for GET.
Attributes
url[R]
Public Class Methods
get(url, *args)
click to toggle source
# File lib/spout/helpers/json_request.rb, line 13 def get(url, *args) new(url, *args).get end
new(url, args = {})
click to toggle source
# File lib/spout/helpers/json_request.rb, line 20 def initialize(url, args = {}) @params = nested_hash_to_params(args) @url = URI.parse(url) @http = Net::HTTP.new(@url.host, @url.port) if @url.scheme == "https" @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_NONE end rescue @error = "Invalid URL: #{url.inspect}" puts @error.red end
Public Instance Methods
get()
click to toggle source
# File lib/spout/helpers/json_request.rb, line 34 def get return unless @error.nil? full_path = @url.path query = ([@url.query] + @params).flatten.compact.join("&") full_path += "?#{query}" if query.to_s != "" response = @http.start do |http| http.get(full_path) end [JSON.parse(response.body), response] rescue => e puts "GET Error: #{e}".red end
key_value_to_string(key, value, scope = nil)
click to toggle source
# File lib/spout/helpers/json_request.rb, line 54 def key_value_to_string(key, value, scope = nil) current_scope = (scope ? "#{scope}[#{key}]" : key) if value.is_a? Hash value.collect do |k,v| key_value_to_string(k, v, current_scope) end.join("&") elsif value.is_a? Array value.collect do |v| key_value_to_string("", v, current_scope) end else "#{current_scope}=#{CGI.escape(value.to_s)}" end end
nested_hash_to_params(args)
click to toggle source
# File lib/spout/helpers/json_request.rb, line 48 def nested_hash_to_params(args) args.collect do |key, value| key_value_to_string(key, value, nil) end end