@@ -8,6 +8,11 @@ class SavedSearch < Base
8
8
9
9
attr_accessor :newsfeeds
10
10
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
+
11
16
self . element_name = "savedsearches"
12
17
13
18
def initialize ( attributes = { } )
@@ -22,9 +27,6 @@ def self.provided()
22
27
def provided_search?
23
28
true
24
29
end
25
- def newsfeeds
26
- [ ]
27
- end
28
30
SparkApi . logger . info ( "#{ self . name } .path: #{ provided . path } " )
29
31
end
30
32
end
@@ -69,12 +71,8 @@ def listings(args = {})
69
71
end
70
72
71
73
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" ] )
78
76
end
79
77
80
78
def provided_search?
@@ -83,16 +81,11 @@ def provided_search?
83
81
84
82
def can_have_newsfeed?
85
83
86
- return false if provided_search?
87
84
return true if has_active_newsfeed? || has_inactive_newsfeed?
88
85
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
-
93
86
number_of_filters = 0
94
87
95
- standard_fields . each do |field |
88
+ QUALIFYING_FIELDS_FOR_NEWSFEED . each do |field |
96
89
number_of_filters += 1 if self . Filter . include? field
97
90
end
98
91
@@ -101,24 +94,22 @@ def can_have_newsfeed?
101
94
end
102
95
103
96
def has_active_newsfeed?
104
- return false if provided_search?
105
-
106
97
if self . respond_to? "NewsFeedSubscriptionSummary"
107
98
self . NewsFeedSubscriptionSummary [ 'ActiveSubscription' ]
108
99
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" ]
111
103
end
112
104
end
113
105
114
106
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' ]
119
109
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" ]
122
113
end
123
114
end
124
115
0 commit comments