Sha256: dfd74aa2f62853e00db03cd8d2259c20ac48e6a78622311ce9a0fdc07c48d05c

Contents?: true

Size: 959 Bytes

Versions: 3

Compression:

Stored size: 959 Bytes

Contents

# frozen_string_literal: true

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

# TODO: Deprecated, use JsonRequestGeneric instead
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

3 entries across 3 versions & 1 rubygems

Version Path
spout-0.12.0.beta2 lib/spout/helpers/json_request.rb
spout-0.12.0.beta1 lib/spout/helpers/json_request.rb
spout-0.11.1 lib/spout/helpers/json_request.rb