Skip to content

Commit aea4729

Browse files
committed
updated provided saved searches to allow newsfeeds
1 parent 6b66ac1 commit aea4729

File tree

3 files changed

+63
-37
lines changed

3 files changed

+63
-37
lines changed

lib/spark_api/models/saved_search.rb

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ def self.provided()
2222
def provided_search?
2323
true
2424
end
25-
def newsfeeds
26-
[]
27-
end
2825
SparkApi.logger.info("#{self.name}.path: #{provided.path}")
2926
end
3027
end
@@ -69,30 +66,32 @@ def listings(args = {})
6966
end
7067

7168
def newsfeeds
72-
if @newsfeeds.nil?
73-
response = SparkApi.client.get("/savedsearches/#{@attributes["Id"]}", :_expand => "NewsFeeds").first["NewsFeeds"]
74-
# the response from the api is just a bunch of hashes, but we can turn them into Newsfeed instances
75-
@newsfeeds = response.map { |hash| Newsfeed.new(hash) }
76-
end
77-
@newsfeeds
69+
Newsfeed.collect(connection.get("#{self.class.path}/#{@attributes["Id"]}",
70+
:_expand => "NewsFeeds").first["NewsFeeds"])
7871
end
7972

8073
def provided_search?
8174
false
8275
end
8376

77+
def self.qualifying_fields_for_newsfeed
78+
# Newsfeed restriction criteria for saved searches:
79+
# http://alpha.sparkplatform.com/docs/api_services/newsfeed/restrictions#criteria
80+
%w(BathsTotal BedsTotal City CountyOrParish ListPrice Location MlsStatus PostalCode PropertyType
81+
RoomsTotal State)
82+
end
83+
84+
def qualifying_fields_for_newsfeed
85+
self.class.qualifying_fields_for_newsfeed
86+
end
87+
8488
def can_have_newsfeed?
8589

86-
return false if provided_search?
8790
return true if has_active_newsfeed? || has_inactive_newsfeed?
8891

89-
# Newsfeed restriction criteria for saved searches:
90-
# http://alpha.sparkplatform.com/docs/api_services/newsfeed/restrictions#criteria
91-
standard_fields = %w(BathsTotal BedsTotal City CountyOrParish ListPrice Location MlsStatus PostalCode PropertyType RoomsTotal State)
92-
9392
number_of_filters = 0
9493

95-
standard_fields.each do |field|
94+
self.class.qualifying_fields_for_newsfeed.each do |field|
9695
number_of_filters += 1 if self.Filter.include? field
9796
end
9897

@@ -101,24 +100,22 @@ def can_have_newsfeed?
101100
end
102101

103102
def has_active_newsfeed?
104-
return false if provided_search?
105-
106103
if self.respond_to? "NewsFeedSubscriptionSummary"
107104
self.NewsFeedSubscriptionSummary['ActiveSubscription']
108105
else
109-
saved_search = SavedSearch.find( self.Id, {"_expand" => "NewsFeedSubscriptionSummary"})
110-
saved_search.NewsFeedSubscriptionSummary['ActiveSubscription']
106+
search = connection.get "#{self.class.path}/#{@attributes['Id']}",
107+
{"_expand" => "NewsFeedSubscriptionSummary"}
108+
search.first["NewsFeedSubscriptionSummary"]["ActiveSubscription"]
111109
end
112110
end
113111

114112
def has_inactive_newsfeed?
115-
return false if provided_search?
116-
117-
if self.respond_to? "NewsFeedSubscriptionSummary"
118-
!self.NewsFeedSubscriptionSummary['ActiveSubscription']
113+
if self.respond_to?("NewsFeeds") && self.respond_to?("NewsFeedSubscriptionSummary")
114+
self.NewsFeeds.any? && !self.NewsFeedSubscriptionSummary['ActiveSubscription']
119115
else
120-
saved_search = SavedSearch.find( self.Id, {"_expand" => "NewsFeedSubscriptionSummary"})
121-
!saved_search.NewsFeedSubscriptionSummary['ActiveSubscription']
116+
search = connection.get("#{self.class.path}/#{@attributes['Id']}",
117+
{"_expand" => "NewsFeedSubscriptionSummary, NewsFeeds"}).first
118+
search["NewsFeeds"].any? && !search["NewsFeedSubscriptionSummary"]["ActiveSubscription"]
122119
end
123120
end
124121

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"D": {
3+
"Success": true,
4+
"Results": [
5+
{
6+
"ResourceUri": "/v1/savedsearches/20100815220615294367000000",
7+
"Id": "20100815220615294367000000",
8+
"Name": "Search name here",
9+
"ContactIds": [
10+
"20100815220615294367000000"
11+
],
12+
"NewsFeedSubscriptionSummary": {
13+
"ActiveSubscription": false
14+
},
15+
"NewsFeeds": []
16+
}
17+
18+
]
19+
}
20+
}

spec/unit/spark_api/models/saved_search_spec.rb

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,11 @@
167167

168168
describe "can_have_newsfeed?" do
169169

170-
it "should return false for a provided search" do
171-
stub_api_get("/provided/#{subject.class.element_name}/#{id}", 'saved_searches/get.json')
172-
resource = subject.class.provided.find(id)
173-
resource.can_have_newsfeed?.should == false
174-
end
175-
176170
it "should return false without at least three filter parameters" do
177171
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/get.json')
178172
resource = subject.class.find(id)
173+
resource.stub(:has_active_newsfeed?) { false }
174+
resource.stub(:has_inactive_newsfeed?) { false }
179175
resource.Filter = "City Eq 'Moorhead' And MlsStatus Eq 'Active'"
180176
resource.can_have_newsfeed?.should == false
181177
end
@@ -201,9 +197,12 @@
201197
resource.has_active_newsfeed?.should == true
202198
end
203199

204-
it "should return false for a provided search" do
205-
stub_api_get("/provided/#{subject.class.element_name}/#{id}", 'saved_searches/get.json')
206-
resource = subject.class.provided.find(id)
200+
it "should return false if the search doesn't have a newsfeed" do
201+
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/get.json')
202+
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/without_newsfeed.json',
203+
{ "_expand" => "NewsFeedSubscriptionSummary" } )
204+
resource = subject.class.find(id)
205+
resource.stub(:provided_search?) { false }
207206
resource.has_active_newsfeed?.should == false
208207
end
209208
end
@@ -218,11 +217,15 @@
218217
resource.has_inactive_newsfeed?.should == true
219218
end
220219

221-
it "should return false for a provided search" do
222-
stub_api_get("/provided/#{subject.class.element_name}/#{id}", 'saved_searches/with_inactive_newsfeed.json')
223-
resource = subject.class.provided.find(id)
220+
it "should return false if the search doesn't have a newsfeed" do
221+
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/get.json')
222+
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/without_newsfeed.json',
223+
{ "_expand" => "NewsFeedSubscriptionSummary, NewsFeeds" } )
224+
resource = subject.class.find(id)
225+
# resource.stub(:provided_search?) { false }
224226
resource.has_inactive_newsfeed?.should == false
225227
end
228+
226229
end
227230

228231
describe "newsfeed" do
@@ -235,4 +238,10 @@
235238
end
236239
end
237240

241+
describe "qualifying_fields_for_newsfeed" do
242+
it "should return an array" do
243+
subject.class.qualifying_fields_for_newsfeed.should be_an(Array)
244+
end
245+
end
246+
238247
end

0 commit comments

Comments
 (0)