@@ -22,9 +22,6 @@ def self.provided()
22
22
def provided_search?
23
23
true
24
24
end
25
- def newsfeeds
26
- [ ]
27
- end
28
25
SparkApi . logger . info ( "#{ self . name } .path: #{ provided . path } " )
29
26
end
30
27
end
@@ -69,30 +66,32 @@ def listings(args = {})
69
66
end
70
67
71
68
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" ] )
78
71
end
79
72
80
73
def provided_search?
81
74
false
82
75
end
83
76
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
+
84
88
def can_have_newsfeed?
85
89
86
- return false if provided_search?
87
90
return true if has_active_newsfeed? || has_inactive_newsfeed?
88
91
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
92
number_of_filters = 0
94
93
95
- standard_fields . each do |field |
94
+ self . class . qualifying_fields_for_newsfeed . each do |field |
96
95
number_of_filters += 1 if self . Filter . include? field
97
96
end
98
97
@@ -101,24 +100,22 @@ def can_have_newsfeed?
101
100
end
102
101
103
102
def has_active_newsfeed?
104
- return false if provided_search?
105
-
106
103
if self . respond_to? "NewsFeedSubscriptionSummary"
107
104
self . NewsFeedSubscriptionSummary [ 'ActiveSubscription' ]
108
105
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" ]
111
109
end
112
110
end
113
111
114
112
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' ]
119
115
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" ]
122
119
end
123
120
end
124
121
0 commit comments