Class: Ubiquitously::User
Instance Attribute Summary collapse
-
#accounts ⇒ Object
Returns the value of attribute accounts.
-
#agent ⇒ Object
Returns the value of attribute agent.
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#email ⇒ Object
Returns the value of attribute email.
-
#name ⇒ Object
Returns the value of attribute name.
-
#storage ⇒ Object
Returns the value of attribute storage.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #account_classes(*items) ⇒ Object
- #account_for(service) ⇒ Object
- #accountables(*items, &block) ⇒ Object
- #cookies_for(service) ⇒ Object
- #cookies_for?(service) ⇒ Boolean
- #credentials ⇒ Object
- #credentials=(hash) ⇒ Object
- #credentials_for(service) ⇒ Object
- #credentials_for?(service) ⇒ Boolean
-
#initialize(attributes = {}) ⇒ User
constructor
A new instance of User.
- #load ⇒ Object
- #login(*services) ⇒ Object
- #password_for(service) ⇒ Object
- #save ⇒ Object
- #service_cookie_name(service) ⇒ Object
- #username_for(service) ⇒ Object
Methods inherited from Base
Methods included from SubclassableCallbacks
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
#accounts ⇒ Object
Returns the value of attribute accounts.
4 5 6 |
# File 'lib/ubiquitously/models/user.rb', line 4 def accounts @accounts end |
#agent ⇒ Object
Returns the value of attribute agent.
4 5 6 |
# File 'lib/ubiquitously/models/user.rb', line 4 def agent @agent end |
#cookies ⇒ Object
Returns the value of attribute cookies.
4 5 6 |
# File 'lib/ubiquitously/models/user.rb', line 4 def @cookies end |
#email ⇒ Object
Returns the value of attribute email.
3 4 5 |
# File 'lib/ubiquitously/models/user.rb', line 3 def email @email end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/ubiquitously/models/user.rb', line 3 def name @name end |
#storage ⇒ Object
Returns the value of attribute storage.
4 5 6 |
# File 'lib/ubiquitously/models/user.rb', line 4 def storage @storage end |
#username ⇒ Object
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 account_classes(*items) services = items.flatten.map(&:to_s) services.map do |service| "Ubiquitously::#{service.camelize}::Account".constantize end.select { |account| services.include?(account.service) } end |
#account_for(service) ⇒ Object
123 124 125 126 |
# File 'lib/ubiquitously/models/user.rb', line 123 def account_for(service) service = service.service unless service.is_a?(String) accountables(*service).detect { |account| account.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 { |account| account.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 { |account| services.include?(account.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 (service) name = (service.to_s) result = .keys.detect { |domain| domain.downcase =~ /#{name}/ } return nil if result.blank? uri = URI.parse("http://#{result}") unless agent..empty?(uri) = agent..(uri) = .length > 0 ? .join("; ") : nil else = nil end end |
#cookies_for?(service) ⇒ Boolean
98 99 100 |
# File 'lib/ubiquitously/models/user.rb', line 98 def (service) !(service).blank? end |
#credentials ⇒ Object
72 73 74 75 76 77 |
# File 'lib/ubiquitously/models/user.rb', line 72 def credentials self.accounts.inject({}) do |hash, account| hash[account.service] = account.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 |account| account.credentials.merge!(hash[account.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
106 107 108 |
# File 'lib/ubiquitously/models/user.rb', line 106 def credentials_for?(service) !credentials_for(service).blank? end |
#load ⇒ Object
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 login(*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) account_for(service).password end |
#save ⇒ Object
60 61 62 |
# File 'lib/ubiquitously/models/user.rb', line 60 def save storage.save(, credentials) end |
#service_cookie_name(service) ⇒ Object
110 111 112 113 |
# File 'lib/ubiquitously/models/user.rb', line 110 def (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) account_for(service).username end |