Skip to content

Commit cb1dbdf

Browse files
committed
added can_have_newsfeed? and has_newsfeed? to SavedSearch
1 parent e2f5a30 commit cb1dbdf

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ tmp/*
1616
.buildpath
1717
.project
1818
*.swp
19+
dev.sh

lib/spark_api/models/saved_search.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,29 @@ def contacts
4949
end
5050
end
5151

52+
def can_have_newsfeed?
53+
54+
standard_fields = %w(BathsTotal BedsTotal City CountyOrParish ListPrice Location MlsStatus PostalCode PropertyType RoomsTotal State)
55+
56+
number_of_filters = 0
57+
58+
standard_fields.each do |field|
59+
number_of_filters += 1 if self.Filter.include? field
60+
end
61+
62+
number_of_filters >= 3
63+
64+
end
65+
66+
def has_newsfeed?
67+
if self.respond_to? "NewsFeedSubscriptionSummary"
68+
self.NewsFeedSubscriptionSummary['ActiveSubscription']
69+
else
70+
saved_search = SavedSearch.find( self.Id, {"_expand" => "NewsFeedSubscriptionSummary"})
71+
saved_search.NewsFeedSubscriptionSummary['ActiveSubscription']
72+
end
73+
end
74+
5275
private
5376

5477
def resource_pluralized; "SavedSearches" end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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": true
14+
}
15+
}
16+
]
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}
16+
]
17+
}
18+
}

spec/unit/spark_api/models/saved_search_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,44 @@
136136
end
137137
end
138138

139+
describe "can_have_newsfeed?" do
140+
141+
it "should return false without at least three filter parameters" do
142+
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/get.json')
143+
resource = subject.class.find(id)
144+
resource.Filter = "City Eq 'Moorhead' And MlsStatus Eq 'Active'"
145+
resource.can_have_newsfeed?.should == false
146+
147+
end
148+
149+
it "should return true with three filter parameters" do
150+
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/get.json')
151+
resource = subject.class.find(id)
152+
resource.Filter = "City Eq 'Moorhead' And MlsStatus Eq 'Active' And PropertyType Eq 'A'"
153+
resource.can_have_newsfeed?.should == true
154+
end
155+
156+
end
157+
158+
describe "has_newsfeed?" do
159+
160+
it "should return true if the search already has a newsfeed" do
161+
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/get.json')
162+
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/with_newsfeed.json',
163+
{ "_expand" => "NewsFeedSubscriptionSummary" } )
164+
resource = subject.class.find(id)
165+
resource.has_newsfeed?.should == true
166+
167+
end
168+
169+
it "should return false if the search doesn't already has a newsfeed" do
170+
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/get.json')
171+
stub_api_get("/#{subject.class.element_name}/#{id}", 'saved_searches/without_newsfeed.json',
172+
{ "_expand" => "NewsFeedSubscriptionSummary" } )
173+
resource = subject.class.find(id)
174+
resource.has_newsfeed?.should == false
175+
end
176+
177+
end
178+
139179
end

0 commit comments

Comments
 (0)