Module: Googletastic::Mixins::Finders::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#all(*args) ⇒ Object



23
24
25
# File 'lib/googletastic/mixins/finders.rb', line 23

def all(*args)
  find(:all, *args)
end

#find(*args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/googletastic/mixins/finders.rb', line 27

def find(*args)
  options = args.extract_options!

  case args.first
    when :first then find_initial(options)
    when :last  then find_last(options)
    when :all   then find_every(options)
    else             find_from_ids(args, options)
  end
end

#find_by_api(options) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/googletastic/mixins/finders.rb', line 77

def find_by_api(options)
  url = build_url(options)
  agent = options.has_key?(:client) ? options[:client] : client
  data = agent.get(url)
  return data if options.has_key?(:raw) and options[:raw] == true
  records = unmarshall(Nokogiri::XML(data.body)).collect {|r| r.response = data; r}
end

#find_every(options) ⇒ Object



46
47
48
# File 'lib/googletastic/mixins/finders.rb', line 46

def find_every(options)
  find_by_api(options)
end

#find_from_ids(ids, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/googletastic/mixins/finders.rb', line 50

def find_from_ids(ids, options)
  expects_array = ids.first.kind_of?(Array)
  return ids.first if expects_array && ids.first.empty?
  
  ids = ids.flatten.compact.uniq
  
  case ids.size
    when 0
      raise "Couldn't find #{self.to_s} without an ID"
    when 1
      result = find_one(ids.first, options)
      expects_array ? [ result ] : result
    else
      find_some(ids, options)
  end
end

#find_initial(options) ⇒ Object



38
39
40
# File 'lib/googletastic/mixins/finders.rb', line 38

def find_initial(options)
  find_by_api(options.merge({:limit => 1})).first
end

#find_last(options) ⇒ Object



42
43
44
# File 'lib/googletastic/mixins/finders.rb', line 42

def find_last(options)
  
end

#find_one(id, options) ⇒ Object



67
68
69
70
71
# File 'lib/googletastic/mixins/finders.rb', line 67

def find_one(id, options)
  options[:id] = id
  options[:url] = self.show_url(id)
  find_by_api(options).first
end

#find_some(ids, options) ⇒ Object



73
74
75
# File 'lib/googletastic/mixins/finders.rb', line 73

def find_some(ids, options)
  
end

#first(*args) ⇒ Object



15
16
17
# File 'lib/googletastic/mixins/finders.rb', line 15

def first(*args)
  find(:first, *args)
end

#head(*args) ⇒ Object



9
10
11
12
13
# File 'lib/googletastic/mixins/finders.rb', line 9

def head(*args)
  options = args.extract_options!
  result = find_by_api(options.merge(:limit => 1)).first
  result.nil? ? nil : result.head
end

#last(*args) ⇒ Object



19
20
21
# File 'lib/googletastic/mixins/finders.rb', line 19

def last(*args)
  find(:last, *args)
end