Class: SparkApi::Models::SavedSearch

Inherits:
Base
  • Object
show all
Extended by:
Finders
Includes:
Concerns::Destroyable, Concerns::Savable
Defined in:
lib/spark_api/models/saved_search.rb

Constant Summary collapse

QUALIFYING_FIELDS_FOR_NEWSFEED =
%w(BathsTotal BedsTotal City CountyOrParish ListPrice Location MlsStatus 
PostalCode PropertyType RoomsTotal State)

Constants included from Paginate

Paginate::DEFAULT_PAGE_SIZE

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes, #errors, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Finders

find, find_one, first, last

Methods included from Concerns::Savable

#create!, #params_for_save, #post_data, #save, #save!, #update!, #update_attributes

Methods included from Concerns::Destroyable

#destroy, #destroy!, #destroyed?

Methods inherited from Base

#connection, connection, count, element_name, element_name=, first, get, #load, #method_missing, #parse_id, #path, path, #persisted?, prefix, prefix=, #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 = {}) ⇒ SavedSearch

Returns a new instance of SavedSearch.



18
19
20
21
# File 'lib/spark_api/models/saved_search.rb', line 18

def initialize(attributes={})
  @newsfeeds = nil
  super(attributes)
end

Dynamic Method Handling

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

Instance Attribute Details

#newsfeeds ⇒ Object

Returns the value of attribute newsfeeds.



9
10
11
# File 'lib/spark_api/models/saved_search.rb', line 9

def newsfeeds
  @newsfeeds
end

Class Method Details

.provided ⇒ Object



23
24
25
26
27
28
29
# File 'lib/spark_api/models/saved_search.rb', line 23

def self.provided()
  Class.new(self).tap do |provided|
    provided.element_name = '/savedsearches'
    provided.prefix = '/provided'
    SparkApi.logger.info("#{self.name}.path: #{provided.path}")
  end
end

.tagged(tag, arguments = {}) ⇒ Object



31
32
33
# File 'lib/spark_api/models/saved_search.rb', line 31

def self.tagged(tag, arguments={})
  collect(connection.get("/#{self.element_name}/tags/#{tag}", arguments))
end

Instance Method Details

#can_have_newsfeed? ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/spark_api/models/saved_search.rb', line 79

def can_have_newsfeed?

  return true  if has_active_newsfeed? || has_inactive_newsfeed?

  number_of_filters = 0

  QUALIFYING_FIELDS_FOR_NEWSFEED.each do |field|
    number_of_filters += 1 if self.Filter.include? field
  end
  
  number_of_filters >= 3

end

#contacts ⇒ Object

list contacts (private role)



40
41
42
43
44
# File 'lib/spark_api/models/saved_search.rb', line 40

def contacts
  return [] unless persisted?
  results = connection.get("#{self.class.path}/#{@attributes["Id"]}")
  @attributes['ContactIds'] = results.first['ContactIds']
end

#favorite? ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/spark_api/models/saved_search.rb', line 35

def favorite?
  @attributes["Tags"] && @attributes["Tags"].include?( "Favorites")
end

#has_active_newsfeed? ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
# File 'lib/spark_api/models/saved_search.rb', line 93

def has_active_newsfeed?
  if self.respond_to? "NewsFeedSubscriptionSummary"
    self.NewsFeedSubscriptionSummary['ActiveSubscription']
  else
    search = connection.get "#{self.class.path}/#{@attributes['Id']}", 
      {"_expand" => "NewsFeedSubscriptionSummary"}
    search.first["NewsFeedSubscriptionSummary"]["ActiveSubscription"]
  end
end

#has_inactive_newsfeed? ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
# File 'lib/spark_api/models/saved_search.rb', line 103

def has_inactive_newsfeed?
  if self.respond_to?("NewsFeeds") && self.respond_to?("NewsFeedSubscriptionSummary")
    self.NewsFeeds.any? && !self.NewsFeedSubscriptionSummary['ActiveSubscription']
  else
    search = connection.get("#{self.class.path}/#{@attributes['Id']}", 
      {"_expand" => "NewsFeedSubscriptionSummary, NewsFeeds"}).first
    search["NewsFeeds"].any? && !search["NewsFeedSubscriptionSummary"]["ActiveSubscription"]
  end
end

#listings(args = {}) ⇒ Object



68
69
70
71
72
# File 'lib/spark_api/models/saved_search.rb', line 68

def listings(args = {})
  arguments = {:_filter => "SavedSearch Eq '#{self.Id}'"}
  arguments.merge!(:RequestMode => 'permissive') if Provided?
  @listings ||= Listing.collect(connection.get("/listings", arguments.merge(args)))
end