Skip to content

Commit 4f10c6c

Browse files
committed
Merge pull request #99 from ryanhertz/saved-search-subscriptions
updated provided saved searches to allow newsfeeds
2 parents b634056 + 2a2beb4 commit 4f10c6c

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

lib/spark_api/models/saved_search.rb

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ class SavedSearch < Base
88

99
attr_accessor :newsfeeds
1010

11+
# Newsfeed restriction criteria for saved searches:
12+
# http://alpha.sparkplatform.com/docs/api_services/newsfeed/restrictions#criteria
13+
QUALIFYING_FIELDS_FOR_NEWSFEED = %w(BathsTotal BedsTotal City CountyOrParish ListPrice Location MlsStatus
14+
PostalCode PropertyType RoomsTotal State)
15+
1116
self.element_name="savedsearches"
1217

1318
def initialize(attributes={})
@@ -22,9 +27,6 @@ def self.provided()
2227
def provided_search?
2328
true
2429
end
25-
def newsfeeds
26-
[]
27-
end
2830
SparkApi.logger.info("#{self.name}.path: #{provided.path}")
2931
end
3032
end
@@ -69,12 +71,8 @@ def listings(args = {})
6971
end
7072

7173
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
74+
Newsfeed.collect(connection.get("#{self.class.path}/#{@attributes["Id"]}",
75+
:_expand => "NewsFeeds").first["NewsFeeds"])
7876
end
7977

8078
def provided_search?
@@ -83,16 +81,11 @@ def provided_search?
8381

8482
def can_have_newsfeed?
8583

86-
return false if provided_search?
8784
return true if has_active_newsfeed? || has_inactive_newsfeed?
8885

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-
9386
number_of_filters = 0
9487

95-
standard_fields.each do |field|
88+
QUALIFYING_FIELDS_FOR_NEWSFEED.each do |field|
9689
number_of_filters += 1 if self.Filter.include? field
9790
end
9891

@@ -101,24 +94,22 @@ def can_have_newsfeed?
10194
end
10295

10396
def has_active_newsfeed?
104-
return false if provided_search?
105-
10697
if self.respond_to? "NewsFeedSubscriptionSummary"
10798
self.NewsFeedSubscriptionSummary['ActiveSubscription']
10899
else
109-
saved_search = SavedSearch.find( self.Id, {"_expand" => "NewsFeedSubscriptionSummary"})
110-
saved_search.NewsFeedSubscriptionSummary['ActiveSubscription']
100+
search = connection.get "#{self.class.path}/#{@attributes['Id']}",
101+
{"_expand" => "NewsFeedSubscriptionSummary"}
102+
search.first["NewsFeedSubscriptionSummary"]["ActiveSubscription"]
111103
end
112104
end
113105

114106
def has_inactive_newsfeed?
115-
return false if provided_search?
116-
117-
if self.respond_to? "NewsFeedSubscriptionSummary"
118-
!self.NewsFeedSubscriptionSummary['ActiveSubscription']
107+
if self.respond_to?("NewsFeeds") && self.respond_to?("NewsFeedSubscriptionSummary")
108+
self.NewsFeeds.any? && !self.NewsFeedSubscriptionSummary['ActiveSubscription']
119109
else
120-
saved_search = SavedSearch.find( self.Id, {"_expand" => "NewsFeedSubscriptionSummary"})
121-
!saved_search.NewsFeedSubscriptionSummary['ActiveSubscription']
110+
search = connection.get("#{self.class.path}/#{@attributes['Id']}",
111+
{"_expand" => "NewsFeedSubscriptionSummary, NewsFeeds"}).first
112+
search["NewsFeeds"].any? && !search["NewsFeedSubscriptionSummary"]["ActiveSubscription"]
122113
end
123114
end
124115

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)