Class: FlexmlsApi::Models::Account

Inherits:
Base
  • Object
show all
Extended by:
Finders
Defined in:
lib/flexmls_api/models/account.rb

Defined Under Namespace

Classes: Address, Email, Image, Phone, Website

Constant Summary collapse

SUBELEMENTS =
[:emails, :phones, :websites, :addresses, :images]

Constants included from Paginate

Paginate::DEFAULT_PAGE_SIZE

Instance Attribute Summary

Attributes inherited from Base

#attributes, #changed, #errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Finders

find, first, last

Methods inherited from Base

#connection, connection, count, element_name, element_name=, first, get, #load, #method_missing, #parse_id, path, prefix, prefix=, #respond_to?

Methods included from Paginate

#collect, #paginate, #per_page

Constructor Details

#initialize(attributes = {}) ⇒ Account

Returns a new instance of Account.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/flexmls_api/models/account.rb', line 10

def initialize(attributes={})
  @emails = subresource(Email, "Emails", attributes)
  @phones = subresource(Phone, "Phones", attributes)
  @websites = subresource(Website, "Websites", attributes)
  @addresses = subresource(Address, "Addresses", attributes)
  if attributes["Images"]
    @images = [] 
    attributes["Images"].each { |i| @images << Image.new(i) }
  else
    @images = nil
  end
  @my_account = false
  super(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FlexmlsApi::Models::Base

Class Method Details

.by_office(office_id, arguments = {}) ⇒ Object



35
36
37
# File 'lib/flexmls_api/models/account.rb', line 35

def self.by_office(office_id, arguments={})
  collect(connection.get("#{self.path()}/by/office/#{office_id}", arguments))
end

.my(arguments = {}) ⇒ Object



25
26
27
28
29
# File 'lib/flexmls_api/models/account.rb', line 25

def self.my(arguments={})
    = collect(connection.get("/my/account", arguments)).first
  . = true
  
end

Instance Method Details

#my_account?Boolean

Returns:

  • (Boolean)


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

def my_account?
  @my_account
end

#primary_img(typ) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/flexmls_api/models/account.rb', line 39

def primary_img(typ)
  if @images.is_a?(Array)
    matches = @images.select {|i| i.Type == typ}
    matches.sort {|a,b| a.Name <=> b.Name }.first
  else
    nil
  end
end

#save(arguments = {}) ⇒ Object



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

def save(arguments={})
  begin
    return save!(arguments)
  rescue NotFound, BadResourceRequest => e
    FlexmlsApi.logger.error("Failed to save resource #{self}: #{e.message}")
  end
  false
end

#save!(arguments = {}) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/flexmls_api/models/account.rb', line 56

def save!(arguments={})
  # The long-term idea is that any setting in the user's account could be updated by including
  # an attribute and calling PUT /my/account, but for now only the GetEmailUpdates attribute 
  # is supported
  save_path = my_account? ? "/my/account" : self.class.path
  results = connection.put save_path, {"GetEmailUpdates" => self.GetEmailUpdates }, arguments
  true
end