Class: Dewey::ClientAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/dewey/client_auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ ClientAuth



5
6
7
8
9
# File 'lib/dewey/client_auth.rb', line 5

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

Instance Attribute Details

#authenticationsObject (readonly)

Returns the value of attribute authentications.



3
4
5
# File 'lib/dewey/client_auth.rb', line 3

def authentications
  @authentications
end

Instance Method Details

#authenticate!(service = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dewey/client_auth.rb', line 19

def authenticate!(service = nil)
  service ||= :writely


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

  url      = URI.parse(Dewey::)
  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

#authenticated?(service = nil) ⇒ Boolean



11
12
13
14
15
16
17
# File 'lib/dewey/client_auth.rb', line 11

def authenticated?(service = nil)
  if service
    @authentications.has_key?(service)
  else
    @authentications.any?
  end
end

#token(service = nil) ⇒ Object



43
44
45
46
47
# File 'lib/dewey/client_auth.rb', line 43

def token(service = nil)
  service = guard(service)
  authenticate!(service) unless authenticated?(service)
  @authentications[service]
end