lib/flexmls_api/models/account.rb in flexmls_api-0.4.5 vs lib/flexmls_api/models/account.rb in flexmls_api-0.6.4

- old
+ new

@@ -1,26 +1,69 @@ module FlexmlsApi module Models class Account < Base + extend Finders self.element_name="accounts" SUBELEMENTS = [:emails, :phones, :websites, :addresses, :images] - attr_accessor *SUBELEMENTS + attr_accessor :my_account, *SUBELEMENTS def initialize(attributes={}) @emails = subresource(Email, "Emails", attributes) @phones = subresource(Phone, "Phones", attributes) @websites = subresource(Website, "Websites", attributes) @addresses = subresource(Address, "Addresses", attributes) - @images = subresource(Image, "Images", attributes) + if attributes["Images"] + @images = [] + attributes["Images"].each { |i| @images << Image.new(i) } + else + @images = nil + end + @my_account = false super(attributes) end def self.my(arguments={}) - collect(connection.get("/my/account", arguments)).first + account = collect(connection.get("/my/account", arguments)).first + account.my_account = true + account end + + def my_account? + @my_account + end + + def self.by_office(office_id, arguments={}) + collect(connection.get("#{self.path()}/by/office/#{office_id}", arguments)) + end + 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 + + def save(arguments={}) + begin + return save!(arguments) + rescue NotFound, BadResourceRequest => e + FlexmlsApi.logger.error("Failed to save resource #{self}: #{e.message}") + end + false + end + 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 + private def subresource(klass, key, attributes) return nil unless attributes.key?(key) array = attributes[key] @@ -44,12 +87,12 @@ end class Address < Base include Primary end + class Image < Base - include Primary end end end -end \ No newline at end of file +end