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



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

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.



34
35
36
# File 'lib/mls/models/account.rb', line 34

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


120
121
122
123
124
125
126
127
128
# File 'lib/mls/models/account.rb', line 120

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



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

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

.find(id) ⇒ Object



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

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

.reset_password!(email) ⇒ Object



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

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

.search(terms) ⇒ Object



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

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

.update_password!(params_hash) ⇒ Object



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

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:



61
62
63
# File 'lib/mls/models/account.rb', line 61

def agent?
  type == 'Agent'
end

#agent_profileObject



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

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



48
49
50
51
52
53
54
55
# File 'lib/mls/models/account.rb', line 48

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



57
58
59
# File 'lib/mls/models/account.rb', line 57

def display_name
  name || email
end

#favorite(listing) ⇒ Object

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



79
80
81
82
83
84
85
# File 'lib/mls/models/account.rb', line 79

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:



75
76
77
# File 'lib/mls/models/account.rb', line 75

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

#to_hashObject



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

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



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

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



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

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