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)


83
84
85
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 83

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

#can_reach_head_from?(id) ⇒ Boolean

Can you reach head from the given ID?

Returns:

  • (Boolean)


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

def can_reach_head_from?(id)
	patch_array = patches.select{ |p| p.from_ids.include?(id) }
	until patch_array.empty?
		p = patch_array.first
		return true if p.to_id == self.head

		next_patches = patches.select{ |np| np.from_ids.include? p.to_id }
		patch_array = patch_array[1..-1] + next_patches
	end
	return false
end

#encode_with(coder) ⇒ Object


Serialisation info

Raises:

  • (RuntimeError)


97
98
99
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 97

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

#encrypted?Boolean

Is this fetcher encrypted?

Returns:

  • (Boolean)

Raises:

  • (RuntimeError)


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

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

#headObject

Returns the ID of head - the latest patch committed to the database.



42
43
44
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 42

def head
	@head ||= (patches.empty? ? base_id : patches.sort.last.to_id)
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.



89
90
91
92
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 89

def next_patch(document)
	all_possible_patches = self.patches.select{ |patch| patch.can_patch?(document) }
	return all_possible_patches.sort.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



102
103
104
105
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 102

def reset
	@patches = nil
	@base = nil
end

#update_full(document) ⇒ Object

Update the document as far as we can



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

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.



73
74
75
76
77
78
79
80
# File 'lib/rubyfocus/fetchers/fetcher.rb', line 73

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