Class: Rubyfocus::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyfocus/fetchers/fetcher.rb

Overview

The Fetcher class is a class that fetches data from a place - generally either a file location or a web address. The Fetcher is initialized with (by default) no options, although subclasses of Fetcher may change this.

Each Fetcher has two important methods:

  • Fetcher#base returns the text of the main xml file, upon which all patch are applied.

  • Fetcher#patch returns the text of each update file as an array.

You may also use the Fetcher#each_patch to iterate through all updates.

To patch a document, simply call Fetcher#update_full(), passing it the document to be patched. Document#update will automatically send the fetcher the document.

Direct Known Subclasses

LocalFetcher, OSSFetcher

Instance Method Summary collapse

Instance Method Details

#baseObject

Returns the content of the base file

Raises:

  • (RuntimeError)


22
23
24
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 22

def base
  raise RuntimeError, "Method Fetcher#base called for abstract class Fetcher."
end

#base_idObject

Returns the id of the base

Raises:

  • (RuntimeError)


27
28
29
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 27

def base_id
  raise RuntimeError, "Method Fetcher#base_id called for abstract class Fetcher."
end

#can_patch?(document) ⇒ Boolean

Can we patch this document? You should call this before update_onceing

Returns:

  • (Boolean)


60
61
62
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 60

def can_patch?(document)
  !next_patch(document).nil?
end

#encode_with(coder) ⇒ Object


Serialisation info

Raises:

  • (RuntimeError)


74
75
76
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 74

def encode_with(coder)
  raise RuntimeError, "Fetcher#encode_with called on abstract class."
end

#init_with(coder) ⇒ Object

This method is called when loading a fetcher from YAML

Raises:

  • (RuntimeError)


17
18
19
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 17

def init_with(coder)
  raise RuntimeError, "Method Fetcher#init_with called for abstract class Fetcher"
end

#next_patch(document) ⇒ Object

Collect the next patch to the document. If more than one patch can be applied, apply the latest one.



66
67
68
69
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 66

def next_patch(document)
  all_possible_patches = self.patches.select{ |patch| patch.can_patch?(document) }
  return all_possible_patches.sort_by(&:time).last
end

#patch(filename) ⇒ Object

Returns the contents of a given patch

Raises:

  • (RuntimeError)


37
38
39
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 37

def patch(filename)
  raise RuntimeError, "Method Fetcher#patch called for abstract class Fetcher."
end

#patchesObject

Returns an array of patch paths - either files or urls

Raises:

  • (RuntimeError)


32
33
34
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 32

def patches
  raise RuntimeError, "Method Fetcher#patches called for abstract class Fetcher."
end

#resetObject

Remove all cached information



79
80
81
82
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 79

def reset
  @patches = nil
  @base = nil
end

#update_full(document) ⇒ Object

Update the document as far as we can



45
46
47
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 45

def update_full(document)
  update_once(document) while can_patch?(document)
end

#update_once(document) ⇒ Object

Update the document one step. Raises an error if the document cannot be updated.



50
51
52
53
54
55
56
57
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 50

def update_once(document)
  np = self.next_patch(document)
  if np
    np.apply_to(document)
  else
    raise RuntimeError, "Patcher#update_once called, but I can't find a patch to apply!"
  end
end