Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactoring for storage options
  • Loading branch information
lancejpollard committed Aug 16, 2010
1 parent bf07dc8 commit 949e68c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/ubiquitously.rb
Expand Up @@ -70,6 +70,7 @@ def services
Dir["#{this}/ubiquitously/extensions/*"].each { |c| require c }
Dir["#{this}/ubiquitously/models/*"].each { |c| require c unless File.directory?(c) }
Dir["#{this}/ubiquitously/services/*"].each { |c| require c unless File.directory?(c) }
Dir["#{this}/ubiquitously/support/*"].each { |c| require c unless File.directory?(c) }

overrides = Dir["#{this}/ubiquitously/services/*"].map do |file|
name = File.basename(file).split(".").first.camelize
Expand Down
41 changes: 30 additions & 11 deletions lib/ubiquitously/models/user.rb
Expand Up @@ -26,9 +26,9 @@ def accounts
end

def accountables(*items)
items.flatten.inject(self.accounts) do |accounts, item|
next if accounts.detect { |account| account.service == item.to_s }
accounts << "Ubiquitously::#{name.to_s.camelize}::Account".constantize.new(
items.flatten.map(&:to_s).inject(self.accounts) do |accounts, item|
next if accounts.detect { |account| account.service == item }
accounts << "Ubiquitously::#{item.camelize}::Account".constantize.new(
:user => self
)
accounts
Expand All @@ -37,16 +37,13 @@ def accountables(*items)

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

def load
hash = storage.load
agent.cookie_jar.jar = hash[:cookies]
accountables(hash[:credentials]).each do |account|
account.credentials.merge!(hash[account.service.to_s])
end
puts storage.load.inspect
apply storage.load
end

def save
Expand All @@ -57,16 +54,38 @@ def cookies
self.agent.cookie_jar.jar
end

def cookies=(hash)
agent.cookie_jar.jar = hash
end

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

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

def cookies_for(service)
name = service_cookie_name(service.to_s)
!cookies.keys.detect { |domain| domain.downcase =~ /#{name}/ }.blank?
cookies.keys.detect { |domain| domain.downcase =~ /#{name}/ }
end

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

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

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

def service_cookie_name(service)
Expand Down
1 change: 1 addition & 0 deletions lib/ubiquitously/services/facebook.rb
Expand Up @@ -2,6 +2,7 @@ module Ubiquitously
module Facebook
class Account < Ubiquitously::Service::Account
def login
raise "LOGIN #{self.inspect}"
url = "http://localhost:4567/"
authorize_url = FacebookToken.authorize(url)
page = agent.get(authorize_url, [], url)
Expand Down
2 changes: 1 addition & 1 deletion lib/ubiquitously/support/active_record.rb
Expand Up @@ -6,7 +6,7 @@ def self.included(base)
end

module ClassMethods
cattr_accessor :ubiquitously_accounts
attr_accessor :ubiquitously_accounts

def ubiquitous(*args)
self.ubiquitously_accounts = args.flatten.map(&:to_s).uniq.map do |service|
Expand Down
16 changes: 9 additions & 7 deletions lib/ubiquitously/support/storage.rb
Expand Up @@ -5,11 +5,11 @@ def initialize(*)

end

def push(cookies, credentials)
def save(cookies, credentials)

end

def pull
def load

end
end
Expand All @@ -21,20 +21,22 @@ def initialize(path)
self.path = path
end

def push(cookies, credentials)
def save(cookies, credentials)
write("#{path}/cookies.yml", cookies)
write("#{path}/credentials.yml", credentials)
end

def pull
def load
{
:cookies => read("#{path}/cookies.yml"),
:credentials => read("#{path}/credentials.yml")
}
end

def read(path)
File.exists?(path) ? YAML.load_file(path) : {}
result = File.exists?(path) ? YAML.load_file(path) : {}
result = {} unless result.is_a?(Hash)
result
end

def write(path, content)
Expand All @@ -51,14 +53,14 @@ def initialize(record, cookies_attribute, credentials_attribute)
self.credentials_attribute = credentials_attribute
end

def push(cookies, credentials)
def save(cookies, credentials)
record.update_attributes(
cookies_attribute => cookies,
credentials_attribute => credentials
)
end

def pull
def load
{
:cookies => record.read_attribute(cookies_attribute),
:credentials => record.read_attribute(credentials_attribute)
Expand Down

0 comments on commit 949e68c

Please sign in to comment.