Sha256: 91dd32e922e43ce2c79dfa4aaf7666764f0304248c62775914d96c3606bc9ed7

Contents?: true

Size: 971 Bytes

Versions: 2

Compression:

Stored size: 971 Bytes

Contents

require 'json'
require 'uri'
require 'net/http'
require 'net/https' # ruby 1.8.7 requires explicitly require net/https

module Monad
  module Drivers
    class JsonDriver
      def initialize(options)
        @url = options['url']

        if !@url
          raise FatalException.new "'url' must be specified for json data source: #{options['name']}."
        end

        if @url !~ URI::regexp || URI(@url).scheme !~ /^http|https$/
          raise FatalException.new "incorrect json data source url: #{@url}"
        end
      end

      def load
        uri = URI(@url)

        if uri.scheme == 'https'
          http = Net::HTTP.new(uri.host, uri.port)
          http.use_ssl = true
          http.verify_mode = OpenSSL::SSL::VERIFY_NONE

          request = Net::HTTP::Get.new(uri.request_uri)
          response = http.request(request).body
        else
          response = Net::HTTP.get(uri)
        end

        JSON.parse(response)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
monad-0.0.2 lib/monad/drivers/json_driver.rb
monad-0.0.1 lib/monad/drivers/json_driver.rb