Class: SparkApi::Models::Account
- Extended by:
- Finders
- Defined in:
- lib/spark_api/models/account.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Address, Email, Image, Phone, Website
Constant Summary collapse
- SUBELEMENTS =
[:emails, :phones, :websites, :addresses, :images]
Constants included from Paginate
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Account
constructor
A new instance of Account.
- #my_account? ⇒ Boolean
- #primary_img(typ) ⇒ Object
- #save(arguments = {}) ⇒ Object
- #save!(arguments = {}) ⇒ Object
Methods included from Finders
Methods inherited from Base
#connection, connection, count, element_name, element_name=, first, get, #id, #load, #method_missing, #parse_id, path, #path, #persisted?, prefix, prefix=, #resource_pluralized, #resource_uri, #respond_to?, #to_param, #to_partial_path
Methods included from Paginate
#collect, #paginate, #per_page
Methods included from Dirty
#changed, #changed?, #changed_attributes, #changes, #dirty_attributes, #previous_changes
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/spark_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 SparkApi::Models::Base
Class Method Details
.by_office(office_id, arguments = {}) ⇒ Object
35 36 37 |
# File 'lib/spark_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/spark_api/models/account.rb', line 25 def self.my(arguments={}) account = collect(connection.get("/my/account", arguments)).first account.my_account = true account end |
Instance Method Details
#my_account? ⇒ Boolean
31 32 33 |
# File 'lib/spark_api/models/account.rb', line 31 def my_account? @my_account end |
#primary_img(typ) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/spark_api/models/account.rb', line 39 def primary_img(typ) if @images.is_a?(Array) matches = @images.select {|i| i.Type == typ} matches.sort do |a,b| if a.Name.nil? && !b.Name.nil? 1 elsif b.Name.nil? && !a.Name.nil? -1 else a.Name.to_s <=> b.Name.to_s end end.first else nil end end |
#save(arguments = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/spark_api/models/account.rb', line 56 def save(arguments={}) self.errors = [] # clear the errors hash begin return save!(arguments) rescue BadResourceRequest => e self.errors << { :code => e.code, :message => e. } SparkApi.logger.warn("Failed to save resource #{self}: #{e.}") rescue NotFound => e self.errors << {:code => e.code, :message => e.} SparkApi.logger.error("Failed to save resource #{self}: #{e.}") end false end |
#save!(arguments = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/spark_api/models/account.rb', line 69 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 = "/accounts/"+self.Id ojbsome = {} if attributes['GetEmailUpdates'] save_path = my_account? ? "/my/account" : self.class.path ojbsome["GetEmailUpdates"] = attributes['GetEmailUpdates'] end if attributes['PasswordValidation'] ojbsome["PasswordValidation"] = attributes['PasswordValidation'] end if attributes['Password'] ojbsome["Password"] = attributes['Password'] end results = connection.put save_path, ojbsome, arguments true end |