Skip to content

Commit 5142e29

Browse files
committed
Merge pull request #94 from ryanhertz/add-newsfeed-methods-to-saved-search-model
Add newsfeed methods to saved search model
2 parents 7f2c7fb + cdbca9c commit 5142e29

File tree

13 files changed

+317
-4
lines changed

13 files changed

+317
-4
lines changed

lib/spark_api/models.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
require 'spark_api/models/listing_cart'
1919
require 'spark_api/models/market_statistics'
2020
require 'spark_api/models/message'
21+
require 'spark_api/models/newsfeed'
2122
require 'spark_api/models/note'
2223
require 'spark_api/models/notification'
2324
require 'spark_api/models/open_house'

lib/spark_api/models/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def method_missing(method_symbol, *arguments)
9494
end
9595
else
9696
return attributes[method_name] if attributes.include?(method_name)
97-
super # GTFO
97+
super
9898
end
9999
end
100100

lib/spark_api/models/finders.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def last(*arguments)
3232
private
3333

3434
def find_every(options)
35-
collect(connection.get("/#{element_name}", options))
35+
collect(connection.get("#{path}", options))
3636
end
3737

3838
def find_single(scope, options)
39-
resp = connection.get("/#{element_name}/#{scope}", options)
39+
resp = connection.get("#{path}/#{scope}", options)
4040
new(resp.first)
4141
end
4242

lib/spark_api/models/listing.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def method_missing(method_symbol, *arguments)
233233
else
234234
return attributes[method_name] if attributes.include?(method_name)
235235
return @attributes['StandardFields'][method_name] if attributes['StandardFields'].include?(method_name)
236-
super # GTFO
236+
super
237237
end
238238
end
239239

lib/spark_api/models/newsfeed.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module SparkApi
2+
module Models
3+
class Newsfeed < Base
4+
5+
extend Finders
6+
include Concerns::Savable
7+
8+
self.element_name = 'newsfeeds'
9+
10+
end
11+
end
12+
end

lib/spark_api/models/saved_search.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@ class SavedSearch < Base
66
include Concerns::Savable,
77
Concerns::Destroyable
88

9+
attr_accessor :newsfeeds
10+
911
self.element_name="savedsearches"
1012

13+
def initialize(attributes={})
14+
@newsfeeds = nil
15+
super(attributes)
16+
end
17+
1118
def self.provided()
1219
Class.new(self).tap do |provided|
1320
provided.element_name = '/savedsearches'
1421
provided.prefix = '/provided'
22+
def provided_search?
23+
true
24+
end
25+
def newsfeeds
26+
[]
27+
end
1528
SparkApi.logger.info("#{self.name}.path: #{provided.path}")
1629
end
1730
end
@@ -49,6 +62,65 @@ def contacts
4962
end
5063
end
5164

65+
def listings(args = {})
66+
arguments = {:_filter => self.Filter}.merge(args)
67+
@listings ||= Listing.collect(connection.get("/listings", arguments))
68+
end
69+
70+
def newsfeeds
71+
if @newsfeeds.nil?
72+
response = SparkApi.client.get("/savedsearches/#{@attributes["Id"]}", _expand: "NewsFeeds").first["NewsFeeds"]
73+
# the response from the api is just a bunch of hashes, but we can turn them into Newsfeed instances
74+
@newsfeeds = response.map { |hash| Newsfeed.new(hash) }
75+
end
76+
@newsfeeds
77+
end
78+
79+
def provided_search?
80+
false
81+
end
82+
83+
def can_have_newsfeed?
84+
85+
return false if provided_search?
86+
return true if has_active_newsfeed? || has_inactive_newsfeed?
87+
88+
# Newsfeed restriction criteria for saved searches:
89+
# http://alpha.sparkplatform.com/docs/api_services/newsfeed/restrictions#criteria
90+
standard_fields = %w(BathsTotal BedsTotal City CountyOrParish ListPrice Location MlsStatus PostalCode PropertyType RoomsTotal State)
91+
92+
number_of_filters = 0
93+
94+
standard_fields.each do |field|
95+
number_of_filters += 1 if self.Filter.include? field
96+
end
97+
98+
number_of_filters >= 3
99+
100+
end
101+
102+
def has_active_newsfeed?
103+
return false if provided_search?
104+
105+
if self.respond_to? "NewsFeedSubscriptionSummary"
106+
self.NewsFeedSubscriptionSummary['ActiveSubscription']
107+
else
108+
saved_search = SavedSearch.find( self.Id, {"_expand" => "NewsFeedSubscriptionSummary"})
109+
saved_search.NewsFeedSubscriptionSummary['ActiveSubscription']
110+
end
111+
end
112+
113+
def has_inactive_newsfeed?
114+
return false if provided_search?
115+
116+
if self.respond_to? "NewsFeedSubscriptionSummary"
117+
!self.NewsFeedSubscriptionSummary['ActiveSubscription']
118+
else
119+
saved_search = SavedSearch.find( self.Id, {"_expand" => "NewsFeedSubscriptionSummary"})
120+
!saved_search.NewsFeedSubscriptionSummary['ActiveSubscription']
121+
end
122+
end
123+
52124
private
53125

54126
def resource_pluralized; "SavedSearches" end

spec/fixtures/newsfeeds/get.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"D": {
3+
"Success": true,
4+
"Results": [
5+
{
6+
"Id": "20130625195235039712000000",
7+
"ResourceUri": "/vX/newsfeeds/20130625195235039712000000",
8+
"Active": true,
9+
"NotificationsActive": true,
10+
"Curated": false,
11+
"Name": "My Newsfeed Subscription",
12+
"Type": "SavedSearch",
13+
"NotificationMethods": ["Email"],
14+
"CreatedTimestamp": "2013-06-27T10:01:06-05:00",
15+
"ModificationTimestamp": "2013-06-27T10:01:06-05:00",
16+
"LastEventTimestamp": "2013-07-07T12:12:06-05:00",
17+
"ExpirationDate": "2014-10-10",
18+
"Subscription": {
19+
"ResourceUri": "/vX/savedsearches/20100815220615294367000000",
20+
"Id": "20100815220615294367000000",
21+
"OwnerId": "20090815223215294334000000",
22+
"Name": "Search name here",
23+
"Description": "A longer description of the search",
24+
"Filter": null,
25+
"ModificationTimestamp": "2011-03-14T08:39:38-05:00"
26+
}
27+
}
28+
]
29+
}
30+
}

spec/fixtures/newsfeeds/inactive.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"D": {
3+
"Success": true,
4+
"Results": [
5+
{
6+
"Id": "20130625195235039712000000",
7+
"ResourceUri": "/vX/newsfeeds/20130625195235039712000000",
8+
"Active": false,
9+
"NotificationsActive": true,
10+
"Curated": false,
11+
"Name": "My Newsfeed Subscription",
12+
"Type": "SavedSearch",
13+
"NotificationMethods": ["Email"],
14+
"CreatedTimestamp": "2013-06-27T10:01:06-05:00",
15+
"ModificationTimestamp": "2013-06-27T10:01:06-05:00",
16+
"LastEventTimestamp": "2013-07-07T12:12:06-05:00",
17+
"ExpirationDate": "2014-10-10",
18+
"Subscription": {
19+
"ResourceUri": "/vX/savedsearches/20100815220615294367000000",
20+
"Id": "20100815220615294367000000",
21+
"OwnerId": "20090815223215294334000000",
22+
"Name": "Search name here",
23+
"Description": "A longer description of the search",
24+
"Filter": null,
25+
"ModificationTimestamp": "2011-03-14T08:39:38-05:00"
26+
}
27+
}
28+
]
29+
}
30+
}

spec/fixtures/saved_searches/get.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"ResourceUri": "/v1/savedsearches/20100815220615294367000000",
77
"Id": "20100815220615294367000000",
88
"Name": "Search name here",
9+
"Filter": "City Eq 'Moorhead' And MlsStatus Eq 'Active' And PropertyType Eq 'A'",
910
"ContactIds": [
1011
"20100815220615294367000000"
1112
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
"ResourceUri": "/v1/newsfeeds/20141203082619076323088319",
18+
"Name": "My Very Special Newsfeed",
19+
"OwnerId": "20000426143505724628000000",
20+
"ProspectLinkId": "",
21+
"CreatedTimestamp": "2014-12-03T08:26:19Z",
22+
"Id": "20141203082619076323088319",
23+
"Active": "false",
24+
"ModificationTimestamp": "2014-12-03T08:26:19Z",
25+
"ExpirationDate": "2015-03-03",
26+
"Curated": "false",
27+
"NotificationMethods": ["Email"],
28+
"LastEventTimestamp": "",
29+
"Type": "SavedSearch"
30+
}
31+
]
32+
}
33+
]
34+
}
35+
}

0 commit comments

Comments
 (0)