Class: Active::Query
- Inherits:
-
Object
- Object
- Active::Query
- Defined in:
- lib/active/query.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#bounding_box(box) ⇒ Object
LatLngBounds(sw?:LatLng, ne?:LatLng) Constructs a rectangle from the points at its south-west and north-east corners.
- #date_range(start_date, end_date) ⇒ Object
- #future ⇒ Object
-
#initialize(options = {}) ⇒ Query
constructor
A new instance of Query.
- #keywords(value) ⇒ Object
- #limit(value) ⇒ Object (also: #per_page)
- #location(value) ⇒ Object
- #near(value) ⇒ Object
- #page(value = 1) ⇒ Object
- #past ⇒ Object
- #radius(value) ⇒ Object
- #results ⇒ Object
- #search ⇒ Object
-
#sort(value) ⇒ Object
(also: #order)
s = sort The default sort for results is by relevance.
- #to_query ⇒ Object
- #today ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Query
Returns a new instance of Query.
9 10 11 12 13 14 15 16 17 |
# File 'lib/active/query.rb', line 9 def initialize(={}) @options = { :s => "relevance", :f => [:facet], :meta => {}, :m => [], :v => 'json' } end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/active/query.rb', line 7 def @options end |
Instance Method Details
#bounding_box(box) ⇒ Object
LatLngBounds(sw?:LatLng, ne?:LatLng) Constructs a rectangle from the points at its south-west and north-east corners.
44 45 46 47 48 49 50 51 52 |
# File 'lib/active/query.rb', line 44 def bounding_box(box) latitude1 = box[:sw].split(",").first.to_f+90 latitude2 = box[:ne].split(",").first.to_f+90 longitude1 = box[:sw].split(",").last.to_f+180 longitude2 = box[:ne].split(",").last.to_f+180 [:meta][:latitudeShifted] = "#{latitude1}..#{latitude2}" [:meta][:longitudeShifted] = "#{longitude1}..#{longitude2}" self end |
#date_range(start_date, end_date) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/active/query.rb', line 85 def date_range(start_date,end_date) if start_date.kind_of?(String) start_date = Date.parse(start_date) end if end_date.kind_of?(String) end_date = Date.parse(end_date) end start_date = URI.escape(start_date.strftime("%m/%d/%Y")).gsub(/\//,"%2F") end_date = URI.escape(end_date.strftime("%m/%d/%Y")).gsub(/\//,"%2F") [:meta][:startDate] = "daterange:#{start_date}..#{end_date}" self end |
#future ⇒ Object
98 99 100 101 |
# File 'lib/active/query.rb', line 98 def future [:meta][:startDate] = "daterange:today..+" self end |
#keywords(value) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/active/query.rb', line 76 def keywords(value) if value.kind_of?(Array) @options[:k] = value.join("+") elsif value.kind_of?(String) @options[:k] = single_encode(value) end self end |
#limit(value) ⇒ Object Also known as: per_page
25 26 27 28 |
# File 'lib/active/query.rb', line 25 def limit(value) @options[:num] = value self end |
#location(value) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/active/query.rb', line 61 def location(value) if value.kind_of?(Hash) [:meta].merge!(value) elsif value.kind_of?(String) @options[:l] = single_encode(value) end self end |
#near(value) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/active/query.rb', line 54 def near(value) raise Active::InvalidOption unless value[:latitude] and value[:longitude] and value[:radius] @options[:l] = "#{value[:latitude]},#{value[:longitude]}" @options[:r] = value[:radius] self end |
#page(value = 1) ⇒ Object
19 20 21 22 23 |
# File 'lib/active/query.rb', line 19 def page(value=1) raise Active::InvalidOption if value.to_i <= 0 @options[:page] = value self end |
#past ⇒ Object
103 104 105 106 |
# File 'lib/active/query.rb', line 103 def past [:meta][:startDate] = "daterange:..#{Date.today}" self end |
#radius(value) ⇒ Object
70 71 72 73 74 |
# File 'lib/active/query.rb', line 70 def radius(value) raise Active::InvalidOption unless value @options[:r] = value.to_i self end |
#results ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/active/query.rb', line 192 def results return @a if @a @res ||= search @a ||= Active::Results.new() # GSA will only return 1000 events but will give us a number larger then 1000. @a.number_of_results = (@res['numberOfResults'] <= 1000) ? @res['numberOfResults'] : 1000 @a.end_index = @res['endIndex'] @a.page_size = @res['pageSize'] @a.search_time = @res['searchTime'] @res['_results'].collect do |d| @a << Active::Asset.factory(d) end @a end |
#search ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/active/query.rb', line 176 def search searchurl = URI.parse(to_query) http = Net::HTTP.new(searchurl.host, searchurl.port) res = http.start do |http| http.get("#{searchurl.path}?#{searchurl.query}") end if (200..307).include?(res.code.to_i) # TODO HANDLE JSON PARSE ERROR return JSON.parse(res.body) else raise Active::ActiveError, "Active Search responded to your query with code: #{res.code}" end end |
#sort(value) ⇒ Object Also known as: order
s = sort The default sort for results is by relevance. The available values are:
date_asc
date_desc
relevance
36 37 38 39 |
# File 'lib/active/query.rb', line 36 def sort(value) @options[:s] = value self end |
#to_query ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/active/query.rb', line 129 def to_query opts = @options.deep_copy # Extract :meta and turn it into a single string for :m # Nested options inside meta get joined as OR # Top-level options get joined with AND opts[:m] += opts[:meta].collect { |k,v| next unless v if v.kind_of?(Array) # Second-level options get joined with OR v.collect do |v2| # clean out empty values if v2.nil? next elsif v2.kind_of?(String) and v2.empty? next elsif k == :assetId "meta:#{k}=#{single_encode(v2)}" else "meta:#{k}=#{double_encode(v2)}" end end.join('+OR+') else # these keys need meta : if k == :latitudeShifted or k == :longitudeShifted or k == :startDate # encoding works for longitudeShifted if k == :latitudeShifted or k == :longitudeShifted double_encode("meta:#{k}:#{v}") # WTF encode the : ? and we don't have to encode it for assetId? else # encoding doesn't work for asset_id, daterange "meta:#{k}:#{v}" end else# these keys need meta= "meta:#{k}=#{double_encode(v)}" end end } opts[:m] << double_encode("meta:startDate:daterange:01-01-2000..") if opts[:meta][:startDate].nil? # clean out empty values opts[:m] = opts[:m].compact.reject { |s| s.nil? or s.empty? } opts[:m] = opts[:m].join('+AND+') opts.delete(:meta) opts.delete_if { |k, v| v.nil? || v.to_s.empty? } # Remove all blank keys "http://search.active.com/search?" + opts.collect{|k,v| "#{k}=#{v}"}.join('&') end |
#today ⇒ Object
108 109 110 111 |
# File 'lib/active/query.rb', line 108 def today [:meta][:startDate] = Date.today self end |