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

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.



13
14
15
16
# File 'lib/spark_api/models/saved_search.rb', line 13

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

#newsfeedsObject

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

.providedObject



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

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



26
27
28
# File 'lib/spark_api/models/saved_search.rb', line 26

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

Instance Method Details

#can_have_newsfeed?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/spark_api/models/saved_search.rb', line 74

def can_have_newsfeed?

  return true  if has_active_newsfeed? || has_inactive_newsfeed?

  number_of_filters = 0

  news_feed_meta.core_fields.each do |field|
    number_of_filters += 1 if self.Filter.include? field
  end
  
  number_of_filters >= news_feed_meta.minimum_core_fields

end

#contactsObject

list contacts (private role)



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

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

#favorite?Boolean

Returns:

  • (Boolean)


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

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

#has_active_newsfeed?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
# File 'lib/spark_api/models/saved_search.rb', line 88

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)


98
99
100
101
102
103
104
105
106
# File 'lib/spark_api/models/saved_search.rb', line 98

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



63
64
65
66
67
# File 'lib/spark_api/models/saved_search.rb', line 63

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