Sha256: 3faa242243ce0568b5fcd93b948981993c809ebcf23cd15d2a836a4a41a1077d
Contents?: true
Size: 1.86 KB
Versions: 3
Compression:
Stored size: 1.86 KB
Contents
require 'net/http' require 'net/https' module Gattica # Authenticates a user against the Google Client Login system class Auth include Convertible SCRIPT_NAME = '/accounts/ClientLogin' HEADERS = { 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => 'Ruby Net::HTTP' } # Google asks that you be nice and provide a user-agent string OPTIONS = { :source => "gattica-#{Gattica::VERSION}", :service => 'analytics' } # Google asks that you provide the name of your app as a 'source' parameter in your POST attr_reader :tokens # Try to authenticate the user def initialize(http, user) require 'cgi' unless defined?(CGI) && defined?(CGI::escape) query_params = OPTIONS.merge(user.to_h).collect do |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" end.sort * '&' response, data = http.post(SCRIPT_NAME, query_params, HEADERS) if response.code != '200' case response.code when '403' raise GatticaError::CouldNotAuthenticate, 'Your email and/or password is not recognized by the Google ClientLogin system (status code: 403)' else raise GatticaError::UnknownAnalyticsError, response.body + " (status code: #{response.code})" end end @tokens = parse_tokens(data) end private # Parse the authentication tokens out of the response and makes them available as a hash # # tokens[:auth] => Google requires this for every request (added to HTTP headers on GET requests) # tokens[:sid] => Not used # tokens[:lsid] => Not used def parse_tokens(data) tokens = {} data.split("\n").each do |t| tokens.merge!({ t.split('=').first.downcase.to_sym => t.split('=').last }) end return tokens end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
jeremyf-gattica-0.3.6 | lib/gattica/auth.rb |
jeremyf-gattica-0.3.7 | lib/gattica/auth.rb |
jeremyf-gattica-0.3.8 | lib/gattica/auth.rb |