Class: Ubiquitously::User

Inherits:
Base show all
Defined in:
lib/ubiquitously/models/user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#apply, #debug?

Methods included from SubclassableCallbacks

included, override

Constructor Details

#initialize(attributes = {}) ⇒ User

Returns a new instance of User.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ubiquitously/models/user.rb', line 6

def initialize(attributes = {})
  attributes = attributes.symbolize_keys
  
  raise "Please define 'storage'" unless attributes[:storage]
  
  unless attributes[:agent]
    attributes[:agent] = Mechanize.new
    #attributes[:agent].log = Logger.new(STDOUT)
    attributes[:agent].user_agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; ru-ru) AppleWebKit/533.2+ (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10"
  end
  
  if attributes[:storage].is_a?(String)
    attributes[:storage] = Ubiquitously::Storage::FileSystem.new(attributes[:storage])
  end
  
  apply attributes
end

Instance Attribute Details

#accountsObject

Returns the value of attribute accounts.



4
5
6
# File 'lib/ubiquitously/models/user.rb', line 4

def accounts
  @accounts
end

#agentObject

Returns the value of attribute agent.



4
5
6
# File 'lib/ubiquitously/models/user.rb', line 4

def agent
  @agent
end

#cookiesObject

Returns the value of attribute cookies.



4
5
6
# File 'lib/ubiquitously/models/user.rb', line 4

def cookies
  @cookies
end

#emailObject

Returns the value of attribute email.



3
4
5
# File 'lib/ubiquitously/models/user.rb', line 3

def email
  @email
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/ubiquitously/models/user.rb', line 3

def name
  @name
end

#storageObject

Returns the value of attribute storage.



4
5
6
# File 'lib/ubiquitously/models/user.rb', line 4

def storage
  @storage
end

#usernameObject

Returns the value of attribute username.



3
4
5
# File 'lib/ubiquitously/models/user.rb', line 3

def username
  @username
end

Instance Method Details

#account_classes(*items) ⇒ Object



42
43
44
45
46
47
# File 'lib/ubiquitously/models/user.rb', line 42

def (*items)
  services = items.flatten.map(&:to_s)
  services.map do |service|
    "Ubiquitously::#{service.camelize}::Account".constantize
  end.select { || services.include?(.service) }
end

#account_for(service) ⇒ Object



123
124
125
126
# File 'lib/ubiquitously/models/user.rb', line 123

def (service)
  service = service.service unless service.is_a?(String)
  accountables(*service).detect { || .service == service }
end

#accountables(*items, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ubiquitously/models/user.rb', line 28

def accountables(*items, &block)
  services = items.flatten.map(&:to_s)
  services.each do |service|
    next if self.accounts.detect { || .service == service }
    clazz = "Ubiquitously::#{service.camelize}::Account".constantize
    if block_given?
      self.accounts << yield(clazz)
    else
      self.accounts << clazz.new(:user => self)
    end
  end
  self.accounts.select { || services.include?(.service) }
end

#cookies_for(service) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ubiquitously/models/user.rb', line 85

def cookies_for(service)
  name = service_cookie_name(service.to_s)
  result = cookies.keys.detect { |domain| domain.downcase =~ /#{name}/ }
  return nil if result.blank?
  uri = URI.parse("http://#{result}")
  unless agent.cookie_jar.empty?(uri)
    cookies = agent.cookie_jar.cookies(uri)
    cookie = cookies.length > 0 ? cookies.join("; ") : nil
  else
    cookie = nil
  end
end

#cookies_for?(service) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/ubiquitously/models/user.rb', line 98

def cookies_for?(service)
  !cookies_for(service).blank?
end

#credentialsObject



72
73
74
75
76
77
# File 'lib/ubiquitously/models/user.rb', line 72

def credentials
  self.accounts.inject({}) do |hash, |
    hash[.service] = .credentials.stringify_keys
    hash
  end
end

#credentials=(hash) ⇒ Object



79
80
81
82
83
# File 'lib/ubiquitously/models/user.rb', line 79

def credentials=(hash)
  accountables(hash.keys).each do ||
    .credentials.merge!(hash[.service])
  end
end

#credentials_for(service) ⇒ Object



102
103
104
# File 'lib/ubiquitously/models/user.rb', line 102

def credentials_for(service)
  credentials[service.to_s]
end

#credentials_for?(service) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/ubiquitously/models/user.rb', line 106

def credentials_for?(service)
  !credentials_for(service).blank?
end

#loadObject



56
57
58
# File 'lib/ubiquitously/models/user.rb', line 56

def load
  apply storage.load
end

#login(*services) ⇒ Object



49
50
51
52
53
54
# File 'lib/ubiquitously/models/user.rb', line 49

def (*services)
  load
  accountables(*services).map(&:login)
  save
  true
end

#password_for(service) ⇒ Object



119
120
121
# File 'lib/ubiquitously/models/user.rb', line 119

def password_for(service)
  (service).password
end

#saveObject



60
61
62
# File 'lib/ubiquitously/models/user.rb', line 60

def save
  storage.save(cookies, credentials)
end


110
111
112
113
# File 'lib/ubiquitously/models/user.rb', line 110

def service_cookie_name(service)
  return service.gsub("_", "_?") unless service == "dzone_snippets"
  return "dzone"
end

#username_for(service) ⇒ Object



115
116
117
# File 'lib/ubiquitously/models/user.rb', line 115

def username_for(service)
  (service).username
end