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



58
59
60
61
62
# File 'lib/mls/models/account.rb', line 58

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.



31
32
33
# File 'lib/mls/models/account.rb', line 31

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


109
110
111
112
113
114
115
116
117
# File 'lib/mls/models/account.rb', line 109

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



92
93
94
95
# File 'lib/mls/models/account.rb', line 92

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

.find(id) ⇒ Object



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

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

.reset_password!(email) ⇒ Object



119
120
121
122
123
124
# File 'lib/mls/models/account.rb', line 119

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

.search(terms) ⇒ Object



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

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

.update_password!(params_hash) ⇒ Object



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

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:



54
55
56
# File 'lib/mls/models/account.rb', line 54

def agent?
  type == 'Agent'
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



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

def create
  MLS.post('/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

#favorite(listing) ⇒ Object

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



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

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:



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

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

#to_hashObject



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

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



76
77
78
79
80
81
82
# File 'lib/mls/models/account.rb', line 76

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



35
36
37
38
39
40
# File 'lib/mls/models/account.rb', line 35

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