Sha256: ad2030c31265f49765ee80e087e37127fdbd882dedd6bce4fa247383d0adcbec

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

require 'json'
require 'uri'
require 'net/http'
require 'date'
require 'time'
require 'zlib'

module Pollster

  class Base
    API_SERVER = 'elections.huffingtonpost.com'
    API_BASE = '/pollster/api'

    class << self

      private

        def encode_params(params)
          params.map { |k, v| "#{k}=#{v}" }.join('&')
        end

        def build_request_url(path, params={})
          URI("http://#{API_SERVER}#{API_BASE}/#{path}#{params.size > 0 ? "?#{encode_params(params)}" : ''}")
        end

        def invoke(path, params={})
          uri = build_request_url(path, params)
          request = Net::HTTP::Get.new(uri.request_uri)
          request['Accept-Encoding'] = 'gzip,deflate'
          response = Net::HTTP.start(uri.host, uri.port) { |http| http.request(request) }
          body = response.header['Content-Encoding'].eql?('gzip') ?
                 Zlib::GzipReader.new(StringIO.new(response.body)).read() :
                 response.body
          raise Exception, JSON.parse(body)["errors"].join(', ') if response.code.eql?('400')
          JSON.parse(body)
        end

        def hash_keys_to_sym(hash)
          Hash[*hash.map { |k, v| [k.to_sym, v] }.flatten(1)]
        end

    end
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pollster-0.2.3 lib/pollster/base.rb
pollster-0.2.2 lib/pollster/base.rb
pollster-0.2.1 lib/pollster/base.rb
pollster-0.2.0 lib/pollster/base.rb
pollster-0.1.1 lib/pollster/base.rb
pollster-0.1.0 lib/pollster/base.rb