Module: Googletastic::Mixins::Requesting::ClassMethods

Defined in:
lib/googletastic/mixins/requesting.rb

Instance Method Summary collapse

Instance Method Details

#build_url(options) ⇒ Object



41
42
43
44
45
# File 'lib/googletastic/mixins/requesting.rb', line 41

def build_url(options)
  base = options.has_key?(:url) ? options[:url] : self.index_url
  options[:url] = base
  urlify(base, extract_params(options))
end

#client(name = self.client_class) ⇒ Object

helper method for google client



37
38
39
# File 'lib/googletastic/mixins/requesting.rb', line 37

def client(name = self.client_class)
  Googletastic.client_for(name.to_s.underscore)
end

#convert_fields(value) ⇒ Object



64
65
66
# File 'lib/googletastic/mixins/requesting.rb', line 64

def convert_fields(value)
  value
end

#extract_params(options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/googletastic/mixins/requesting.rb', line 48

def extract_params(options)
  queries = self.valid_queries
  options.inject({}) do |converted, (key, value)|
    real_key = queries[key]
    if queries.has_key?(key)
      next if self.respond_to?("valid_#{real_key}?") and !self.send("valid_#{real_key}?", value)
      value = self.send("convert_#{real_key}", value) if self.respond_to?("convert_#{real_key}")
      converted[real_key] = value
    end
    converted
  end
end

#urlify(url, params = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/googletastic/mixins/requesting.rb', line 25

def urlify(url, params = {})
  if params && !params.empty?
    query = params.collect do |k,v|
      v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
      "#{k}=#{CGI.escape(v.to_s)}"
    end.join("&")
    url = "#{url}?#{query}"
  end
  url
end

#valid_queriesObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/googletastic/mixins/requesting.rb', line 12

def valid_queries
  {
    :limit => "max-results",
    :offset => "start-index",
    :start => "start-index",
    :end => "end-index",
    :categories => "category",
    :with => "q"
#        :only => "fields", # only the fields we want!
#        :fields => "fields"
  }
end