Class: GoogleContacts::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/google_contacts/wrapper.rb

Defined Under Namespace

Classes: CollectionProxy

Constant Summary collapse

CONTACTS_BATCH =
"http://www.google.com/m8/feeds/contacts/default/full/batch".freeze
GROUPS_BATCH =
"http://www.google.com/m8/feeds/groups/default/full/batch".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consumer) ⇒ Wrapper

Returns a new instance of Wrapper.



12
13
14
15
16
17
# File 'lib/google_contacts/wrapper.rb', line 12

def initialize(consumer)
  @consumer = consumer

  @contacts = CollectionProxy.new(self, Contact)
  @groups   = CollectionProxy.new(self, Group)
end

Instance Attribute Details

#consumerObject (readonly)

Returns the value of attribute consumer.



3
4
5
# File 'lib/google_contacts/wrapper.rb', line 3

def consumer
  @consumer
end

#contactsObject (readonly)

Proxies for crud



9
10
11
# File 'lib/google_contacts/wrapper.rb', line 9

def contacts
  @contacts
end

#groupsObject (readonly)

Returns the value of attribute groups.



10
11
12
# File 'lib/google_contacts/wrapper.rb', line 10

def groups
  @groups
end

Instance Method Details

#append_operation(instance, operation) ⇒ Object



72
73
74
75
# File 'lib/google_contacts/wrapper.rb', line 72

def append_operation(instance, operation)
  entry = instance.entry_for_batch(operation)
  append_to_batch(entry)
end

#batch(options = {}) {|blk| ... } ⇒ Object

Yields:

  • (blk)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/google_contacts/wrapper.rb', line 31

def batch(options = {}, &blk)
  raise "Nesting of calls to batch is not allowed" if @batching
  @batching = true
  @batch ||= []

  yield(blk)
  @batching = false

  # create documents to be flushed
  documents = @batch.each_slice(100).map do |chunk|
    batch_document(chunk)
  end
  @batch.clear

  if options[:return_documents]
    documents
  else
    documents.each do |doc|
      flush_batch(doc)
    end
  end
end

#find(what, options = {}, &blk) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/google_contacts/wrapper.rb', line 54

def find(what, options = {}, &blk)
  options['max-results'] ||= 200
  options['start-index'] = 1

  result = []
  begin
    xml = get("http://www.google.com/m8/feeds/#{what}/default/full", options)
    result.concat xml.xpath('/xmlns:feed/xmlns:entry').map(&blk)

    total_results = xml.at('//openSearch:totalResults').text.to_i
    start_index   = xml.at('//openSearch:startIndex'  ).text.to_i
    per_page      = xml.at('//openSearch:itemsPerPage').text.to_i
    options['start-index'] = start_index + per_page
  end while (options['start-index'] <= total_results)

  result
end

#get(url, options = {}) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/google_contacts/wrapper.rb', line 19

def get(url, options = {})
  query = options.map { |k,v| "#{k}=#{v}" }.join('&')
  url += "?#{query}" if query.size > 0

  body = consumer.get(url).body
  Nokogiri::XML.parse body
end

#post(url, body) ⇒ Object



27
28
29
# File 'lib/google_contacts/wrapper.rb', line 27

def post(url, body)
  consumer.post(url, body, 'Content-Type' => 'application/atom+xml')
end