remote_partial 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -35,6 +35,25 @@ passed to Nokogiri's search method, and can be either xpath or css format. See:
35
35
 
36
36
  http://nokogiri.org/tutorials/searching_a_xml_html_document.html
37
37
 
38
+ If no criteria are specified, the whole page will be retrieved.
39
+
40
+ == Output modifier
41
+
42
+ A lambda can be passed into RemotePartial.define, and this will be called
43
+ on the retrieved content before the partial file is generated.
44
+
45
+ Note that the lambda must be passed in as a string so it can be stored in a
46
+ serialized format. RemotePartial will convert the string into a lambda when it
47
+ is needed.
48
+
49
+ For example, to change all instances of 'foo' to 'bar' in the partial:
50
+
51
+ RemotePartial.define(
52
+ url: 'http://somewhere.com',
53
+ name: 'foo_bar',
54
+ output_modifier: '{|content| content.gsub(/foo/, "bar")}'
55
+ )
56
+
38
57
  == Adding remote partial content to a page
39
58
 
40
59
  To output the content of the remote partial 'ruby' to a rails view add this:
@@ -22,6 +22,7 @@ module RemotePartial
22
22
  def create_or_update_partial
23
23
  @partial = Partial.find(args['name']) || Partial.new(name: args['name'])
24
24
  track_change(args, 'url')
25
+ track_change(args, 'output_modifier')
25
26
  track_change(args, 'criteria')
26
27
  track_change(args, 'repeat_period', @partial.default_repeat_period)
27
28
 
@@ -9,7 +9,7 @@ module RemotePartial
9
9
  end
10
10
 
11
11
  def update_file
12
- resource_manager.output_to output_file_name
12
+ resource_manager.output_to(output_file_name)
13
13
  update_stale_at
14
14
  end
15
15
 
@@ -22,7 +22,7 @@ module RemotePartial
22
22
  end
23
23
 
24
24
  def resource_manager
25
- ResourceManager.new(url, criteria)
25
+ ResourceManager.new(url, criteria, &output_modifier_to_lambda)
26
26
  end
27
27
 
28
28
  def repeat_period
@@ -63,5 +63,9 @@ module RemotePartial
63
63
  "_#{name}.html.erb"
64
64
  end
65
65
 
66
+ def output_modifier_to_lambda
67
+ output_modifier? ? instance_eval("lambda #{output_modifier}") : nil
68
+ end
69
+
66
70
  end
67
71
  end
@@ -3,7 +3,7 @@ require 'net/http'
3
3
 
4
4
  module RemotePartial
5
5
  class ResourceManager
6
- attr_reader :url, :criteria
6
+ attr_reader :url, :criteria, :output_modifier
7
7
 
8
8
  def self.get_page(url)
9
9
  Nokogiri::HTML(get_raw(url))
@@ -24,17 +24,10 @@ module RemotePartial
24
24
  raise RemotePartialRetrivalError.new(url, exception)
25
25
  end
26
26
 
27
- def initialize(url, criteria = nil)
27
+ def initialize(url, criteria = nil, &output_modifier)
28
28
  @url = url
29
29
  @criteria = criteria
30
- end
31
-
32
- def html
33
- if criteria
34
- self.class.get_page(@url).search(criteria).to_s
35
- else
36
- self.class.get_raw(@url).force_encoding('UTF-8')
37
- end
30
+ @output_modifier = output_modifier
38
31
  end
39
32
 
40
33
  def output_to(path)
@@ -43,7 +36,20 @@ module RemotePartial
43
36
  File.write(path, html)
44
37
  end
45
38
 
39
+ def html
40
+ text = criteria ? get_part_of_page : get_whole_page
41
+ output_modifier ? output_modifier.call(text) : text
42
+ end
43
+
46
44
  private
45
+ def get_whole_page
46
+ self.class.get_raw(@url).force_encoding('UTF-8')
47
+ end
48
+
49
+ def get_part_of_page
50
+ self.class.get_page(@url).search(criteria).to_s
51
+ end
52
+
47
53
  def output_folder
48
54
  File.dirname(@path)
49
55
  end
@@ -24,6 +24,10 @@ module RemotePartial
24
24
  new(item.merge(name: name)) if item
25
25
  end
26
26
 
27
+ def self.all
28
+ read.keys.collect{|name| find(name)}
29
+ end
30
+
27
31
  def self.count
28
32
  read.keys.length
29
33
  end
@@ -1,11 +1,16 @@
1
1
  module RemotePartial
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
4
4
 
5
5
  # History
6
6
  # =======
7
7
  #
8
- # 0.3.1 Move location where db is stored in test environment
8
+ # 0.4.0 Add facility to modify the partial file before it is saved
9
+ # ----------------------------------------------------------------
10
+ # A modification can now be made to the content retrieved from the
11
+ # remote page, before it is saved as a partial.
12
+ #
13
+ # 0.3.2 Move location where db is stored in test environment
9
14
  # ----------------------------------------------------------
10
15
  # The yaml file created by the Partial model during tests, had been put into
11
16
  # test/fixtures, but this is the wrong place because this is not a fixture. In