Sha256: 6f0ee291ebe26b6c1882f24fbb083b28aeea0b4889a250b467610b4f381d1d7a

Contents?: true

Size: 1.05 KB

Versions: 7

Compression:

Stored size: 1.05 KB

Contents

module Garb
  class DataRequest
    class ClientError < StandardError; end

    def initialize(session, base_url, parameters={})
      @session = session
      @base_url = base_url
      @parameters = parameters
    end

    def query_string
      parameter_list = @parameters.map {|k,v| "#{k}=#{v}" }
      parameter_list.empty? ? '' : "?#{parameter_list.join('&')}"
    end

    def uri
      URI.parse(@base_url)
    end

    def send_request
      response = if @session.single_user?
        single_user_request
      elsif @session.oauth_user?
        oauth_user_request
      end

      raise ClientError, response.body.inspect unless response.kind_of?(Net::HTTPSuccess)
      response
    end

    def single_user_request
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      http.get("#{uri.path}#{query_string}", 'Authorization' => "GoogleLogin auth=#{@session.auth_token}")
    end

    def oauth_user_request
      @session.access_token.get("#{uri}#{query_string}")
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
garb-no-activesupport-0.7.4 lib/garb/data_request.rb
garb-0.7.4 lib/garb/data_request.rb
garb-no-activesupport-0.7.3 lib/garb/data_request.rb
garb-0.7.3 lib/garb/data_request.rb
garb-0.7.2 lib/garb/data_request.rb
garb-0.7.1 lib/garb/data_request.rb
garb-0.7.0 lib/garb/data_request.rb