Sha256: 4f026f46fc28aa11f69bbbbdaaff62093da1eed4a290bb54f6e33b5919257a23

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 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(@session.auth_sub?  ? 'AuthSub' : 'GoogleLogin')
      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(auth='GoogleLogin')
      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' => "#{auth} #{auth == 'GoogleLogin' ? 'auth' : 'token'}=#{@session.auth_token}", 'GData-Version' => '2' })
    end

    def oauth_user_request
      @session.access_token.get("#{uri}#{query_string}", {'GData-Version' => '2'})
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
titanous-garb-0.8.7 lib/garb/data_request.rb
titanous-garb-0.8.5 lib/garb/data_request.rb