Class: Contentful::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/sync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options_or_url) ⇒ Sync

Returns a new instance of Sync.



10
11
12
13
14
# File 'lib/contentful/sync.rb', line 10

def initialize(client, options_or_url)
  @client = client
  @next_sync_url = nil
  @first_page_options_or_url = options_or_url
end

Instance Attribute Details

#next_sync_urlObject (readonly)

Returns the value of attribute next_sync_url.



8
9
10
# File 'lib/contentful/sync.rb', line 8

def next_sync_url
  @next_sync_url
end

Instance Method Details

#completed?Boolean

Returns false as long as last sync page has not been reached

Returns:

  • (Boolean)


34
35
36
# File 'lib/contentful/sync.rb', line 34

def completed?
  !!next_sync_url
end

#each_item(&block) ⇒ Object

Directly iterates over all resources that have changed



39
40
41
42
43
44
45
# File 'lib/contentful/sync.rb', line 39

def each_item(&block)
  each_page do |page|
    page.each_item do |item|
      block.call item
    end
  end
end

#each_page(&block) ⇒ Object

Iterates over all pages of the current sync Please Keep in Mind: Iterating fires a new request for each page



18
19
20
21
22
23
24
25
26
# File 'lib/contentful/sync.rb', line 18

def each_page(&block)
  page = first_page
  block.call(page)

  until completed?
    page = page.next_page
    block.call(page)
  end
end

#first_pageObject

Returns the first sync result page



29
30
31
# File 'lib/contentful/sync.rb', line 29

def first_page
  get(@first_page_options_or_url)
end

#get(options_or_url) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/contentful/sync.rb', line 47

def get(options_or_url)
  if options_or_url.is_a? String
    page = Request.new(@client, options_or_url).get
  else
    page = Request.new(@client, '/sync', options_or_url).get
  end

  link_page_to_sync! page
  update_sync_state_from! page

  page
end