11require 'uri'
22require 'set'
33
4+ require 'lonely_coder/search/magic_constants'
5+ require 'lonely_coder/search/search_pagination_parser'
6+ require 'lonely_coder/search/options/filter'
7+
8+ # these are the included filters. See Filter class documentation for adding your own.
9+ require 'lonely_coder/search/options/age'
10+ require 'lonely_coder/search/options/ethnicity'
11+ require 'lonely_coder/search/options/order_by'
12+ require 'lonely_coder/search/options/location'
13+ require 'lonely_coder/search/options/paginator'
14+ require 'lonely_coder/search/options/radius'
15+ require 'lonely_coder/search/options/require_photo'
16+
417class OKCupid
518 # Creates a new Search with the passed options to act as query parameters.
619 # A search will not trigger a query to OKCupid until `results` is called.
720 #
821 # @param [Hash] options a list of options for the search
9- # @option options [Integer] :min_age (18) Minimum age to search for
10- # @option options [Integer] :max_age (99) Maximum age to search for
22+ # @option options [Integer] :min_age (18) Minimum age to search for.
23+ # @option options [Integer] :max_age (99) Maximum age to search for.
24+ # @option options [String] :gentation Gentation is OKCupid's portmanteau for 'gender and orientation'.
25+ # Acceptable values are:
26+ # "girls who like guys", "guys who like girls", "girls who like girls",
27+ # "guys who like guys", "both who like bi guys", "both who like bi girls",
28+ # "straight girls only", "straight guys only", "gay girls only",
29+ # "gay guys only", "bi girls only", "bi guys only", "everybody"
30+ # this option is required.
31+ #
1132 # @option options [String] :order_by ('match %') The sort order of the search results.
1233 # Acceptable values are 'match %','friend %', 'enemy %',
1334 # 'special blend', 'join', and 'last login'.
@@ -24,7 +45,7 @@ class OKCupid
2445 #
2546 # @option options [true, false] :require_photo (true). Search for profiles that have photos
2647 # @option options [String] :relationship_status ('single'). Acceptable values are 'single', 'not single', 'any'
27- # @return [Search] A Searhc without results loaded. To trigger a query against OKCupid call `results`
48+ # @return [Search] A Search without results loaded. To trigger a query against OKCupid call `results`
2849 def search ( options = { } )
2950 Search . new ( options , @browser )
3051 end
@@ -76,42 +97,6 @@ def parse(options)
7697 @timekey = 1
7798 end
7899
79- def add_order_by_option ( value )
80- @parameters << OrderByParameter . new ( value )
81- end
82-
83- def add_last_login_option ( value )
84- @filters << Filter . new ( 'last_login' , value )
85- end
86-
87- def add_location_option ( value )
88- @parameters << LocationParameter . new ( value )
89- end
90-
91- def add_radius_option ( value )
92- @filters << RadiusFilter . new ( 'radius' , value )
93- end
94-
95- def add_require_photo_option ( value )
96- @filters << RequirePhotoFilter . new ( 'require_photo' , value )
97- end
98-
99- def add_relationship_status_option ( value )
100- @filters << Filter . new ( 'relationship_status' , value )
101- end
102-
103- def add_gentation_option ( value )
104- @filters << Filter . new ( 'gentation' , value )
105- end
106-
107- def add_age_option ( value )
108- @filters << AgeFilter . new ( 'age' , value )
109- end
110-
111- def add_pagination_option ( value )
112- @parameters << @pagination = Paginator . new ( value )
113- end
114-
115100 def check_for_required_options ( options )
116101 raise ( FilterError , 'gentation is a required option' ) unless options . has_key? ( :gentation )
117102 end
@@ -219,120 +204,4 @@ def filters_as_query
219204 filters . compact . to_enum ( :each_with_index ) . map { |filter , index | filter . to_param ( index +1 ) } . join ( '&' )
220205 end
221206 end
222-
223- # used to create the pagination part of a search url:
224- # low=1&count=10&ajax_load=1
225- # where low is the start value
226- # count is the number of items per page
227- class Paginator
228- attr_reader :page , :per_page
229-
230- def initialize ( options )
231- @per_page = options [ :per_page ]
232- @page = options [ :page ]
233- end
234-
235- def low
236- @low = ( ( @page - 1 ) * @per_page ) + 1
237- end
238-
239- def next
240- @page +=1
241- self
242- end
243-
244- def to_param
245- "low=#{ low } &count=#{ @per_page } "
246- end
247- end
248-
249- class Filter
250- class NoSuchFilter < StandardError ; end
251- class BadValue < StandardError ; end
252-
253- attr_reader :name , :value , :code
254-
255- def initialize ( name , value )
256- @code = MagicNumbers ::Filters [ name . to_s ]
257- raise ( NoSuchFilter , name ) unless @code
258-
259- @name = name . to_s
260- @value = value
261- @encoded_value = lookup ( @value )
262- unless @encoded_value
263- raise ( BadValue , "#{ @value . inspect } is not a possible value for #{ @name } . Try one of #{ allowed_values . map ( &:inspect ) . join ( ', ' ) } " )
264- end
265- end
266-
267- def allowed_values
268- MagicNumbers . const_get ( @name . camelize ) . keys
269- end
270-
271- def lookup ( value )
272- MagicNumbers . const_get ( @name . camelize ) [ value . downcase ]
273- end
274-
275- def to_param ( n )
276- "filter#{ n } =#{ @code } ,#{ @encoded_value } "
277- end
278- end
279-
280- class EthnicityFilter < Filter
281- def lookup ( values )
282- # lookup the race values and sum them. I think OKC is doing some kind of base2 math on them
283- values . collect { |v | MagicNumbers ::Ethnicity [ v . downcase ] } . inject ( 0 , :+ )
284- end
285- end
286-
287- class LocationParameter
288- def initialize ( value )
289- @value = value
290- end
291-
292- def to_param
293- if @value . is_a? ( String )
294- if @value . downcase == 'near me'
295- "locid=0"
296- else
297- "locid=#{ Search . location_id_for ( @value ) } &lquery=#{ URI . escape ( @value ) } "
298- end
299- else
300- "locid=#{ @value } "
301- end
302- end
303- end
304-
305- class OrderByParameter
306- def initialize ( value )
307- @value = value
308- @encoded_value = MagicNumbers ::OrderBy [ value . downcase ]
309- end
310-
311- def to_param
312- "matchOrderBy=#{ @encoded_value } "
313- end
314- end
315-
316- class RequirePhotoFilter < Filter
317- def lookup ( value )
318- value ? 1 : 0
319- end
320- end
321-
322- class RadiusFilter < Filter
323- def lookup ( value )
324- value . nil? ? '' : value
325- end
326-
327- def to_param ( n )
328- return nil if @encoded_value === ''
329- super
330- end
331- end
332-
333- class AgeFilter < Filter
334- def lookup ( value )
335- "#{ value [ 0 ] } ,#{ value [ 1 ] } "
336- end
337- end
338207end
0 commit comments