Sha256: 3b6452b4a0e9a9725f2b2c5a6e82b512607e3d24a98511c3b4ba4a46a8dc74a7

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module Dewey
  class ClientAuth
    attr_reader :authentications

    def initialize(email, password) 
      @email          = email
      @password       = password
      @authentications = {}
    end

    def authenticated?(service = nil)
      if service
        @authentications.has_key?(service)
      else
        @authentications.any? 
      end
    end
    
    def authenticate!(service = nil)
      service ||= :writely


      params = { 'accountType' => 'HOSTED_OR_GOOGLE',
                 'Email'       => @email,
                 'Passwd'      => @password,
                 'service'     => service.to_s
              }

      url      = URI.parse(Dewey::GOOGLE_LOGIN_URL)
      response = Net::HTTPS.post_form(url, params)

      case response
      when Net::HTTPSuccess
        @authentications[service] = response.body.split('=').last
        true
      when Net::HTTPForbidden
        false
      else
        raise DeweyError, "Unexpected response: #{response}"
      end
    end
    
    def token(service = nil)
      service = guard(service)
      authenticate!(service) unless authenticated?(service)
      @authentications[service]
    end

    private

    def guard(service)
      case service
      when nil           then :writely
      when 'document'    then :writely
      when 'spreadsheet' then :wise
      else
        service.to_sym
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dewey-0.2.8 lib/dewey/client_auth.rb
dewey-0.2.7 lib/dewey/client_auth.rb