lib/mls/models/account.rb in mls-0.11.3 vs lib/mls/models/account.rb in mls-0.12.1

- old
+ new

@@ -1,55 +1,57 @@ class MLS::Account < MLS::Resource - - property :id, Fixnum, :serialize => :if_present - property :type, String, :default => 'Account' - property :name, String, :serialize => :if_present - property :title, String, :serialize => :if_present - property :email, String, :serialize => :if_present - property :password, String, :serialize => :if_present - property :password_confirmation, String, :serialize => :if_present - property :perishable_token, String, :serialize => false - property :perishable_token_set_at, String, :serialize => false - property :ghost, Boolean, :serialize => false, :default => false - - property :phone, String, :serialize => :if_present - property :company, String, :serialize => :if_present - property :license, String, :serialize => :if_present - property :linkedin, String, :serialize => :if_present - property :twitter, String, :serialize => :if_present - property :facebook, String, :serialize => :if_present - property :web, String, :serialize => :if_present - - property :city, String, :serialize => :if_present - property :state, String, :serialize => :if_present - property :country, String, :serialize => :if_present - - property :created_at, DateTime, :serialize => :false - property :updated_at, DateTime, :serialize => :false - - property :email_token, String, :serialize => false - property :auth_key, String, :serialize => false - property :start_hours_of_operation, Fixnum, :serialize => :if_present - property :end_hours_of_operation, Fixnum, :serialize => :if_present - property :days_of_operation, String, :serialize => :if_present - property :timezone, String, :serialize => :if_present - property :lead_status, String, :serialize => :if_present - property :buyer_id, Fixnum, :serialize => :if_present + attribute :id, Fixnum, :serialize => :if_present + attribute :type, String, :default => 'Account' + attribute :name, String, :serialize => :if_present + attribute :title, String, :serialize => :if_present + attribute :email, String, :serialize => :if_present + attribute :password, String, :serialize => :if_present + attribute :password_confirmation, String, :serialize => :if_present + attribute :perishable_token, String, :serialize => false + attribute :perishable_token_set_at, String, :serialize => false + attribute :ghost, Boolean, :serialize => false, :default => false + + attribute :phone, String, :serialize => :if_present + attribute :company, String, :serialize => :if_present + attribute :license, String, :serialize => :if_present + attribute :linkedin, String, :serialize => :if_present + attribute :twitter, String, :serialize => :if_present + attribute :facebook, String, :serialize => :if_present + attribute :web, String, :serialize => :if_present + attribute :direct_phone, String, :serialize => :if_present + attribute :direct_email, String, :serialize => :if_present + + attribute :city, String, :serialize => :if_present + attribute :state, String, :serialize => :if_present + attribute :country, String, :serialize => :if_present + + attribute :created_at, DateTime, :serialize => :false + attribute :updated_at, DateTime, :serialize => :false + + attribute :email_token, String, :serialize => false + attribute :auth_cookie, String, :serialize => false + attribute :start_hours_of_operation, Fixnum, :serialize => :if_present + attribute :end_hours_of_operation, Fixnum, :serialize => :if_present + attribute :days_of_operation, String, :serialize => :if_present + attribute :timezone, String, :serialize => :if_present + attribute :lead_status, String, :serialize => :if_present + attribute :buyer_id, Fixnum, :serialize => :if_present + exclude_from_comparison :password, :password_confirmation - attr_accessor :password_required + attr_accessor :password_required, :brokerage attr_writer :favorites def update MLS.put('/account', {:account => to_hash}, 400) do |response, code| MLS::Account::Parser.update(self, response.body) code == 200 end end - + # Save the Account to the MLS. @errors will be set on the account if there # are any errors. @persisted will also be set to +true+ if the Account was # succesfully created def create MLS.post('/account', {:account => to_hash}, 400) do |response, code| @@ -79,11 +81,11 @@ end def favorited?(listing) favorites.include?(listing) end - + def favorite(listing) # TODO: test me, i don't work on failures params_hash = {:id => listing.is_a?(MLS::Listing) ? listing.id : listing } MLS.post('/account/favorites', params_hash) do |response, code| @favorites = nil true @@ -101,36 +103,40 @@ def to_hash hash = super hash[:password_required] = password_required unless password_required.nil? hash end - + class << self def current response = MLS.get('/account') MLS::Account::Parser.parse(response.body) end - + # Authenticate and Account via <tt>email</tt> and <tt>password</tt>. Returns # the <tt>Account</tt> object if successfully authenticated. Returns <tt>nil</tt> # if the account could not be found, password was incorrect, or the account # was revoked # # ==== Examples # #!ruby # Account.authenticate(:email => 'jon@does.net', :password => 'opensesame') # => #<Account> - # + # # Account.authenticate('jon@does.net', 'opensesame') # => #<Account> # # Account.authenticate('jon@does.net', 'wrong') # => nil def authenticate(attrs_or_email, password=nil) email = attrs_or_email.is_a?(Hash) ? attrs_or_email[:email] : attrs_or_email password = attrs_or_email.is_a?(Hash) ? attrs_or_email[:password] : password - - response = MLS.get('/account', {:email => email, :password => password}) - MLS::Account::Parser.parse(response.body) + + response = MLS.post('/login', {:email => email, :password => password}) + MLS.auth_cookie = response['set-cookie'] + + account = MLS::Account::Parser.parse(response.body) + account.auth_cookie = MLS.auth_cookie + account rescue MLS::Exception::Unauthorized => response nil end def reset_password!(email) @@ -149,20 +155,23 @@ response = MLS.get('/account/search', :query => terms) MLS::Account::Parser.parse_collection(response.body) end def find(id) - response = MLS.get("/account/find", :id => id) + response = MLS.get("/account/find", :id => id) MLS::Account::Parser.parse(response.body) end end - + end class MLS::Account::Parser < MLS::Parser def favorites=(favorites) @object.favorites = favorites.map {|a| MLS::Listing::Parser.build(a) } end + def brokerage=(brokerage) + @object.brokerage = MLS::Brokerage::Parser.build(brokerage) + end end