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.



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

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


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

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



99
100
101
102
# File 'lib/mls/models/account.rb', line 99

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

.find(id) ⇒ Object



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

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

.reset_password!(email) ⇒ Object



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

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



139
140
141
142
# File 'lib/mls/models/account.rb', line 139

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

.update_password!(params_hash) ⇒ Object



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

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

#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



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

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



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

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:



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

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

#to_hashObject



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

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



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

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



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

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