Sha256: 7f05a21f75f58cdb03c95ac70c9e157f3570c90254e29cc20fd1d4ceb288c6ea

Contents?: true

Size: 879 Bytes

Versions: 10

Compression:

Stored size: 879 Bytes

Contents

require 'openssl'
require 'net/http'
require 'json'

module Spout
  module Helpers
    class JsonRequest
      class << self
        def get(*args)
          new(*args).get
        end
      end

      attr_reader :url

      def initialize(url)
        begin
          @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
        end
      end

      def get
        begin
          full_path = @url.path
          full_path += "?#{@url.query}" if @url.query
          req = Net::HTTP::Get.new(full_path)
          response = @http.start do |http|
            http.request(req)
          end
          JSON.parse(response.body)
        rescue
          nil
        end
      end
    end
  end
end


Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
spout-0.10.2 lib/spout/helpers/json_request.rb
spout-0.10.1 lib/spout/helpers/json_request.rb
spout-0.10.0 lib/spout/helpers/json_request.rb
spout-0.10.0.rc3 lib/spout/helpers/json_request.rb
spout-0.10.0.rc2 lib/spout/helpers/json_request.rb
spout-0.10.0.rc lib/spout/helpers/json_request.rb
spout-0.10.0.beta10 lib/spout/helpers/json_request.rb
spout-0.10.0.beta9 lib/spout/helpers/json_request.rb
spout-0.10.0.beta8 lib/spout/helpers/json_request.rb
spout-0.10.0.beta7 lib/spout/helpers/json_request.rb