Class: MLS::Account

Inherits:
Resource show all
Defined in:
lib/mls/models/account.rb

Defined Under Namespace

Classes: Parser

Instance Attribute Summary collapse

Attributes inherited from Resource

#errors, #persisted

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, #create!, inherited, #initialize, #new_record?, #persisted?, #properties, #properties_excluded_from_comparison, #properties_for_comparison, #save, #save!, #set_default_values, #to_key, #update!, #update_attributes

Constructor Details

This class inherits a constructor from MLS::Resource

Instance Attribute Details

#favoritesObject



71
72
73
74
75
# File 'lib/mls/models/account.rb', line 71

def favorites
  return @favorites if @favorites
  response = MLS.get('/account/favorites')
  @favorites = MLS::Listing::Parser.parse_collection(response.body, {:collection_root_element => :favorites})
end

#password_requiredObject

Returns the value of attribute password_required.



40
41
42
# File 'lib/mls/models/account.rb', line 40

def password_required
  @password_required
end

Class Method Details

.authenticate(attrs_or_email, password = nil) ⇒ Object

Authenticate and Account via email and password. Returns the Account object if successfully authenticated. Returns nil if the account could not be found, password was incorrect, or the account was revoked

Examples

#!ruby
Account.authenticate(:email => '[email protected]', :password => 'opensesame') # => #<Account>

Account.authenticate('[email protected]', 'opensesame') # => #<Account>

Account.authenticate('[email protected]', 'wrong') # => nil


126
127
128
129
130
131
132
133
134
# File 'lib/mls/models/account.rb', line 126

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)
rescue MLS::Exception::Unauthorized => response
  nil
end

.currentObject



109
110
111
112
# File 'lib/mls/models/account.rb', line 109

def current
  response = MLS.get('/account')
  MLS::Account::Parser.parse(response.body)
end

.find(id) ⇒ Object



153
154
155
156
# File 'lib/mls/models/account.rb', line 153

def find(id)
  response = MLS.get("/account/find", :id => id) 
  MLS::Account::Parser.parse(response.body)
end

.reset_password!(email) ⇒ Object



136
137
138
139
140
# File 'lib/mls/models/account.rb', line 136

def reset_password!(email)
  MLS.put('/account/reset_password', {:email => email}, 400, 404) do |response, code|
    code == 200
  end
end

.search(terms) ⇒ Object



148
149
150
151
# File 'lib/mls/models/account.rb', line 148

def search(terms)
  response = MLS.get('/account/search', :query => terms)
  MLS::Account::Parser.parse_collection(response.body)
end

.update_password!(params_hash) ⇒ Object



142
143
144
145
146
# File 'lib/mls/models/account.rb', line 142

def update_password!(params_hash)
  MLS.put('/account/update_password', params_hash, 400) do |response, code|
    MLS::Account::Parser.parse(response.body)
  end
end

Instance Method Details

#agent?Boolean

Returns:



67
68
69
# File 'lib/mls/models/account.rb', line 67

def agent?
  type == 'Agent'
end

#agent_profileObject



77
78
79
# File 'lib/mls/models/account.rb', line 77

def agent_profile
  @agent_profile ||= MLS.agent_profile id
end

#createObject

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



54
55
56
57
58
59
60
61
# File 'lib/mls/models/account.rb', line 54

def create
  MLS.post('/account', {:account => to_hash}, 400) do |response, code|
    raise MLS::Exception::UnexpectedResponse if ![201, 400].include?(code)
    MLS::Account::Parser.update(self, response.body)
    @persisted = true
    code == 201
  end
end

#display_nameObject



63
64
65
# File 'lib/mls/models/account.rb', line 63

def display_name
  name || email
end

#favorite(listing) ⇒ Object

TODO: test me, i don’t work on failures



85
86
87
88
89
90
91
# File 'lib/mls/models/account.rb', line 85

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
  end
end

#favorited?(listing) ⇒ Boolean

Returns:



81
82
83
# File 'lib/mls/models/account.rb', line 81

def favorited?(listing)
  favorites.include?(listing)
end

#to_hashObject



101
102
103
104
105
# File 'lib/mls/models/account.rb', line 101

def to_hash
  hash = super
  hash[:password_required] = password_required unless password_required.nil?
  hash
end

#unfavorite(listing_id) ⇒ Object

TODO: test me, i don’t work on failures



93
94
95
96
97
98
99
# File 'lib/mls/models/account.rb', line 93

def unfavorite(listing_id) # TODO: test me, i don't work on failures
  listing_id = listing_id.is_a?(MLS::Listing) ? listing_id.id : listing_id
  MLS.delete("/account/favorites/#{listing_id}") do |response, code|
    @favorites = nil
    true
  end
end

#updateObject



44
45
46
47
48
49
# File 'lib/mls/models/account.rb', line 44

def update
  MLS.put('/account', {:account => to_hash}, 400) do |response, code|
    MLS::Account::Parser.update(self, response.body)
    code == 200
  end
end