remote_partial 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/README.rdoc +58 -15
  2. data/Rakefile +5 -22
  3. data/{app/models → lib}/remote_partial/builder.rb +1 -1
  4. data/{app/models → lib}/remote_partial/exceptions.rb +0 -0
  5. data/{app/models → lib}/remote_partial/partial.rb +25 -4
  6. data/lib/remote_partial/railtie.rb +9 -0
  7. data/{app/models → lib}/remote_partial/resource_manager.rb +0 -0
  8. data/lib/remote_partial/time_calc.rb +45 -0
  9. data/lib/remote_partial/version.rb +8 -1
  10. data/{app/models → lib}/remote_partial/yaml_store.rb +13 -3
  11. data/lib/remote_partial.rb +46 -3
  12. data/lib/tasks/remote_partial_tasks.rake +3 -4
  13. data/test/log/test.log +71 -0
  14. data/test/test_helper.rb +36 -15
  15. data/test/unit/remote_partial/builder_test.rb +12 -9
  16. data/test/unit/remote_partial/exception_test.rb +1 -1
  17. data/test/unit/remote_partial/partial_test.rb +8 -8
  18. data/test/unit/remote_partial/resource_manager_test.rb +3 -3
  19. data/test/unit/remote_partial/yaml_store_test.rb +9 -9
  20. data/test/unit/remote_partial_test.rb +23 -9
  21. metadata +11 -173
  22. data/app/helpers/remote_partial/application_helper.rb +0 -4
  23. data/app/helpers/remote_partial/host_app_helper.rb +0 -11
  24. data/config/routes.rb +0 -2
  25. data/lib/remote_partial/engine.rb +0 -11
  26. data/test/dummy/README.rdoc +0 -261
  27. data/test/dummy/Rakefile +0 -7
  28. data/test/dummy/app/assets/javascripts/application.js +0 -15
  29. data/test/dummy/app/assets/javascripts/demos.js +0 -2
  30. data/test/dummy/app/assets/javascripts/samples.js +0 -2
  31. data/test/dummy/app/assets/stylesheets/application.css +0 -13
  32. data/test/dummy/app/assets/stylesheets/demos.css +0 -4
  33. data/test/dummy/app/assets/stylesheets/samples.css +0 -4
  34. data/test/dummy/app/controllers/application_controller.rb +0 -3
  35. data/test/dummy/app/controllers/demos_controller.rb +0 -18
  36. data/test/dummy/app/helpers/application_helper.rb +0 -2
  37. data/test/dummy/app/helpers/demos_helper.rb +0 -2
  38. data/test/dummy/app/helpers/samples_helper.rb +0 -2
  39. data/test/dummy/app/views/demos/_clock.html.erb +0 -6
  40. data/test/dummy/app/views/demos/_fixed.html.erb +0 -3
  41. data/test/dummy/app/views/demos/_ruby.html.erb +0 -4
  42. data/test/dummy/app/views/demos/index.html.erb +0 -7
  43. data/test/dummy/app/views/demos/show.html.erb +0 -3
  44. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  45. data/test/dummy/app/views/remote_partials/_clock.html.erb +0 -1
  46. data/test/dummy/app/views/remote_partials/_fixed.html.erb +0 -2
  47. data/test/dummy/app/views/remote_partials/_partial_with_mod.html.erb +0 -1
  48. data/test/dummy/app/views/remote_partials/_ruby.html.erb +0 -11
  49. data/test/dummy/app/views/remote_partials/_wcc.html.erb +0 -1
  50. data/test/dummy/config/application.rb +0 -63
  51. data/test/dummy/config/boot.rb +0 -10
  52. data/test/dummy/config/environment.rb +0 -5
  53. data/test/dummy/config/environments/development.rb +0 -39
  54. data/test/dummy/config/environments/production.rb +0 -69
  55. data/test/dummy/config/environments/test.rb +0 -39
  56. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  57. data/test/dummy/config/initializers/inflections.rb +0 -15
  58. data/test/dummy/config/initializers/mime_types.rb +0 -5
  59. data/test/dummy/config/initializers/remote_partial.rb +0 -16
  60. data/test/dummy/config/initializers/secret_token.rb +0 -8
  61. data/test/dummy/config/initializers/session_store.rb +0 -8
  62. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  63. data/test/dummy/config/locales/en.yml +0 -5
  64. data/test/dummy/config/routes.rb +0 -8
  65. data/test/dummy/config.ru +0 -4
  66. data/test/dummy/db/development.sqlite3 +0 -0
  67. data/test/dummy/db/migrate/20130703141929_create_remote_partial_partials.remote_partial.rb +0 -13
  68. data/test/dummy/db/remote_partial/partials.yml +0 -13
  69. data/test/dummy/db/test.sqlite3 +0 -0
  70. data/test/dummy/log/development.log +0 -5518
  71. data/test/dummy/log/test.log +0 -33709
  72. data/test/dummy/public/404.html +0 -26
  73. data/test/dummy/public/422.html +0 -26
  74. data/test/dummy/public/500.html +0 -25
  75. data/test/dummy/public/favicon.ico +0 -0
  76. data/test/dummy/script/rails +0 -6
  77. data/test/dummy/test/functional/demos_controller_test.rb +0 -20
  78. data/test/dummy/test/unit/helpers/demos_helper_test.rb +0 -20
  79. data/test/dummy/test/unit/helpers/samples_helper_test.rb +0 -4
  80. data/test/dummy/tmp/cache/assets/BE9/0F0/sprockets%2Fb918d6cc641337193b96751002451244 +0 -0
  81. data/test/dummy/tmp/cache/assets/C2E/4E0/sprockets%2F5b29288e435665a224409e7d76530c95 +0 -0
  82. data/test/dummy/tmp/cache/assets/CB3/DC0/sprockets%2F157201713d2fd4954447e0bf4d8e853e +0 -0
  83. data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  84. data/test/dummy/tmp/cache/assets/CE3/080/sprockets%2F7d4d7689d6fa8236f0b4848c03ba1215 +0 -0
  85. data/test/dummy/tmp/cache/assets/CE5/D30/sprockets%2F8c9834a63b25a66203918a75ff56e2ac +0 -0
  86. data/test/dummy/tmp/cache/assets/D16/E10/sprockets%2Fd5d9c442561ec08a55d5d843a0d9792d +0 -0
  87. data/test/dummy/tmp/cache/assets/D20/020/sprockets%2F488c2c97fede45c86a62f1aa14554957 +0 -0
  88. data/test/dummy/tmp/cache/assets/D2E/690/sprockets%2F840591ad233bc7bda28b7f6a2b27c205 +0 -0
  89. data/test/dummy/tmp/cache/assets/D30/990/sprockets%2F995cc20d042afc03241f58f7d6ca1f02 +0 -0
  90. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  91. data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  92. data/test/dummy/tmp/cache/assets/D52/430/sprockets%2F6ada07f0c9869f9f35b05fa0988dc717 +0 -0
  93. data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  94. data/test/dummy/tmp/cache/assets/DBB/BE0/sprockets%2Fc6eb578d23a69b5abb056e3abaa5c060 +0 -0
  95. data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  96. data/test/dummy/tmp/cache/assets/E02/FA0/sprockets%2Fca8d5771d03e669be1d96acb6beb6cb6 +0 -0
  97. data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  98. data/test/remote_partial_test.rb +0 -7
data/README.rdoc CHANGED
@@ -1,7 +1,7 @@
1
1
  {<img src="https://codeclimate.com/github/warwickshire/remote_partial.png" />}[https://codeclimate.com/github/warwickshire/remote_partial]
2
2
  = RemotePartial
3
3
 
4
- This rails engine allows partials to be created from remote content.
4
+ Remote Partial allows partials to be created from remote content.
5
5
 
6
6
  == Installation
7
7
 
@@ -9,9 +9,47 @@ Add this to your Gemfile:
9
9
 
10
10
  gem 'remote_partial'
11
11
 
12
+ == Configuration
13
+
14
+ Prior to version 0.7.0, RemotePartial depended upon Rails to determine some
15
+ of the configuration options. This dependency has now been removed, and therefore
16
+ some paths will have to be defined when RemotePartial is run outside of Rails
17
+ (Standalone mode).
18
+
19
+ === Standalone
20
+
21
+ The root location needs to be defined. For example:
22
+
23
+ RemotePartial.root = 'some/path'
24
+
25
+ With this defined:
26
+
27
+ some/path/db/remote_partial --> location for yaml file storing state information
28
+ some/path/remote_partials --> output path, where partials will be created
29
+
30
+ === Within a Rails environment
31
+
32
+ The root is defined as Rails.root, and partials will be placed in the app/views
33
+ folder. Paths relative to Rails.root will then be:
34
+
35
+ db/remote_partial --> location for yaml file storing state information
36
+ app/views/remote_partials --> output path, where partials will be created
37
+
38
+ === Logging
39
+
40
+ In a Rails environment, the Rails logger will be used, otherwise the default
41
+ logging is STDOUT.
42
+
43
+ Logging output to a specific file can be configured like this:
44
+
45
+ RemotePartial.logger_file = 'log/remote_partial.log'
46
+
47
+ Note that the location will be relative to RemotePartial.root, and the 'log'
48
+ folder will need to exist.
49
+
12
50
  == Defining a remote partial
13
51
 
14
- Define the remote partials in an initializer (config/initializers/remote_partial.rb)
52
+ Running this command:
15
53
 
16
54
  RemotePartial.define(
17
55
  url: 'http://www.ruby-lang.org/en/',
@@ -20,14 +58,20 @@ Define the remote partials in an initializer (config/initializers/remote_partial
20
58
  minimum_life: 3.hours
21
59
  )
22
60
 
23
- This will create a partial at:
61
+ will create a partial at:
24
62
 
25
- app/veiws/remote_partials/_ruby.html.erb
63
+ app/views/remote_partials/_ruby.html.erb
26
64
 
27
65
  The content of this partial will be grabbed from the page at the url, and will
28
66
  comprise the content defined by '#intro' (that is, the content of the tag with
29
67
  an id='intro'). This content will not be updated for at least 3 hours.
30
68
 
69
+ === In Rails
70
+
71
+ If you define the remote partials in an initializer (for example
72
+ config/initializers/remote_partial.rb), the remote partial will be updated
73
+ each time the Rails app is started.
74
+
31
75
  == Criteria
32
76
 
33
77
  Nokogiri is used to extract content from within the target page. Criteria are
@@ -58,12 +102,17 @@ For example, to change all instances of 'foo' to 'bar' in the partial:
58
102
 
59
103
  To output the content of the remote partial 'ruby' to a rails view add this:
60
104
 
61
- <%= render_remote_partial 'ruby' %>
105
+ <%= render 'remote_partials/ruby' %>
106
+
107
+ Alternatively, the content of the remote partial can be accessed via:
108
+
109
+ partial = RemotePartial::Partial.find('ruby')
110
+ File.read(partial.output_file_name)
62
111
 
63
112
  == Updating the content
64
113
 
65
- The content update process is run each time the host app is loaded. An update can
66
- also be triggered by running the follow rake task:
114
+ An update of the existing remote partials, can be triggered by running the
115
+ following rake task:
67
116
 
68
117
  rake remote_partial:update
69
118
 
@@ -73,7 +122,7 @@ If an update process is run before the stale_at time, that partial will not be
73
122
  updated.
74
123
 
75
124
  So once the 'ruby' remote partial has been updated, it will be at least three
76
- hours before it will be updated again.
125
+ hours before it will be updated again (unless forced).
77
126
 
78
127
  These is also a rake task that will force all partials to update irrespective
79
128
  of the stale_at time:
@@ -85,16 +134,10 @@ of the stale_at time:
85
134
  If remote partial is unable to retrieve remote content, the problem will be
86
135
  logged, and a retrieval will be tried again at the next update.
87
136
 
88
- == The partial view folder
89
-
90
- Remote partials are held in:
91
-
92
- Rails.root + '/app/views/remote_partials'
93
-
94
137
  == Persistence
95
138
 
96
139
  The current state of each defined remote partial is stored in a YAML file:
97
140
 
98
- db/remote_partial/partials.yml
141
+ db/remote_partial/partial.yml
99
142
 
100
143
  This project wobbles and uses MIT-LICENSE.
data/Rakefile CHANGED
@@ -1,16 +1,8 @@
1
- #!/usr/bin/env rake
2
- begin
3
- require 'bundler/setup'
4
- rescue LoadError
5
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
- end
7
- begin
8
- require 'rdoc/task'
9
- rescue LoadError
10
- require 'rdoc/rdoc'
11
- require 'rake/rdoctask'
12
- RDoc::Task = Rake::RDocTask
13
- end
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rdoc/task'
5
+ require 'rake/testtask'
14
6
 
15
7
  RDoc::Task.new(:rdoc) do |rdoc|
16
8
  rdoc.rdoc_dir = 'rdoc'
@@ -20,14 +12,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
20
12
  rdoc.rdoc_files.include('lib/**/*.rb')
21
13
  end
22
14
 
23
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
- load 'rails/tasks/engine.rake'
25
-
26
-
27
-
28
- Bundler::GemHelper.install_tasks
29
-
30
- require 'rake/testtask'
31
15
 
32
16
  Rake::TestTask.new(:test) do |t|
33
17
  t.libs << 'lib'
@@ -36,5 +20,4 @@ Rake::TestTask.new(:test) do |t|
36
20
  t.verbose = false
37
21
  end
38
22
 
39
-
40
23
  task :default => :test
@@ -16,7 +16,7 @@ module RemotePartial
16
16
  create_or_update_partial
17
17
  partial.update_stale_file
18
18
  rescue RemotePartialRetrivalError => error
19
- Rails.logger.warn error.message
19
+ RemotePartial.logger.warn error.message
20
20
  end
21
21
 
22
22
  def create_or_update_partial
File without changes
@@ -4,6 +4,11 @@ module RemotePartial
4
4
 
5
5
  attr_accessor :stale_at, :repeat_period
6
6
 
7
+ def self.create(hash)
8
+ super
9
+
10
+ end
11
+
7
12
  def output_file_name
8
13
  [partial_folder, file_name].join("/")
9
14
  end
@@ -18,7 +23,7 @@ module RemotePartial
18
23
  end
19
24
 
20
25
  def criteria
21
- super if super.present? # ensure criteria doesn't return empty string
26
+ super if present?(super)
22
27
  end
23
28
 
24
29
  def resource_manager
@@ -26,11 +31,11 @@ module RemotePartial
26
31
  end
27
32
 
28
33
  def repeat_period
29
- @repeat_period ||= default_repeat_period
34
+ @repeat_period ||= determine_repeat_period.to_f
30
35
  end
31
36
 
32
37
  def default_repeat_period
33
- 1.minute
38
+ TimeCalc.minutes(1)
34
39
  end
35
40
 
36
41
  def update_stale_at
@@ -47,7 +52,7 @@ module RemotePartial
47
52
  end
48
53
 
49
54
  def stale?
50
- stale_at.blank? or stale_at < Time.now
55
+ stale_at_blank? or stale_at < Time.now
51
56
  end
52
57
 
53
58
  def to_hash
@@ -67,5 +72,21 @@ module RemotePartial
67
72
  output_modifier? ? instance_eval("lambda #{output_modifier}") : nil
68
73
  end
69
74
 
75
+ def stale_at_blank?
76
+ return true unless stale_at
77
+ !stale_at.kind_of? Time
78
+ end
79
+
80
+ def present?(item)
81
+ item and item.to_s !~ empty_string_pattern
82
+ end
83
+
84
+ def empty_string_pattern
85
+ /\A\s*\Z/
86
+ end
87
+
88
+ def determine_repeat_period
89
+ self[:repeat_period] || self['repeat_period'] || default_repeat_period
90
+ end
70
91
  end
71
92
  end
@@ -0,0 +1,9 @@
1
+ module RemotePartial
2
+ class Railtie < Rails::Railtie
3
+ railtie_name :remote_partial
4
+
5
+ rake_tasks do
6
+ load "tasks/remote_partial_tasks.rake"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+
2
+ module TimeCalc
3
+
4
+ def self.days(number)
5
+ hours(number) * hours_in_a_day
6
+ end
7
+
8
+ def self.hours(number)
9
+ minutes(number) * minutes_in_an_hour
10
+ end
11
+
12
+ # returns seconds in number of minutes
13
+ def self.minutes(number)
14
+ number * seconds_in_a_minute
15
+ end
16
+
17
+ def self.ago(seconds)
18
+ Time.now - seconds
19
+ end
20
+
21
+ def self.minutes_ago(number)
22
+ ago(minutes(number))
23
+ end
24
+
25
+ def self.hours_ago(number)
26
+ ago(hours(number))
27
+ end
28
+
29
+ def self.days_ago(number)
30
+ ago(days(number))
31
+ end
32
+
33
+ def self.seconds_in_a_minute
34
+ 60
35
+ end
36
+
37
+ def self.minutes_in_an_hour
38
+ 60
39
+ end
40
+
41
+ def self.hours_in_a_day
42
+ 24
43
+ end
44
+
45
+ end
@@ -1,10 +1,17 @@
1
1
  module RemotePartial
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
4
4
 
5
5
  # History
6
6
  # =======
7
7
  #
8
+ # 0.7.0 Removes dependency on Rails
9
+ # ---------------------------------
10
+ # The rails dependency was only required to determine a couple of paths,
11
+ # and to generate a helper that I don't think was actually that useful.
12
+ # RemotePartial will stills work with Rails to determine default output
13
+ # paths, but is no longer dependant on it.
14
+ #
8
15
  # 0.6.0 Fixes failure to load content from https sites
9
16
  # ----------------------------------------------------
10
17
  # Whoops. This should have been there from the start.
@@ -33,12 +33,12 @@ module RemotePartial
33
33
  end
34
34
 
35
35
  def self.file
36
- @file ||= File.expand_path("#{name.tableize}.yml", root)
36
+ @file ||= File.expand_path("#{underscore(name)}.yml", root)
37
37
  end
38
38
 
39
39
  def self.root
40
- location = Rails.env == 'test' ? 'test/db' : 'db'
41
- File.expand_path(location, Rails.root)
40
+ location = 'db'
41
+ File.expand_path(location, RemotePartial.root)
42
42
  end
43
43
 
44
44
  def self.dir
@@ -76,6 +76,16 @@ module RemotePartial
76
76
  end
77
77
  end
78
78
 
79
+ def self.underscore(camel_cased_word)
80
+ word = camel_cased_word.to_s.dup
81
+ word.gsub!('::', '/')
82
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
83
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
84
+ word.tr!("-", "_")
85
+ word.downcase!
86
+ word
87
+ end
88
+
79
89
  def update_time_stamps
80
90
  self.created_at = Time.now unless created_at?
81
91
  self.updated_at = Time.now
@@ -1,5 +1,6 @@
1
- require "remote_partial/engine"
2
- require_relative '../app/models/remote_partial/exceptions'
1
+ required_items = [:exceptions, :time_calc, :yaml_store, :partial, :resource_manager, :builder]
2
+ required_items.each{|item| require_relative "remote_partial/#{item}"}
3
+ require_relative('remote_partial/railtie') if defined? Rails
3
4
 
4
5
  module RemotePartial
5
6
 
@@ -8,7 +9,49 @@ module RemotePartial
8
9
  end
9
10
 
10
11
  def self.partial_location
11
- File.expand_path('app/views/remote_partials', Rails.root)
12
+ if defined?(Rails)
13
+ File.expand_path('app/views/remote_partials', Rails.root)
14
+ else
15
+ File.expand_path('remote_partials', root)
16
+ end
17
+ end
18
+
19
+ def self.root
20
+ @root || default_root
21
+ end
22
+
23
+ def self.root=(path)
24
+ @root = path
25
+ end
26
+
27
+ def self.default_root
28
+ if defined? Rails
29
+ Rails.root
30
+ else
31
+ raise("You must define a root via: RemotePartial.root = 'some/path' ")
32
+ end
33
+ end
34
+
35
+ def self.logger
36
+ if defined? Rails
37
+ Rails.logger
38
+ else
39
+ require 'logger'
40
+ @logger ||= ruby_logger
41
+ end
42
+ end
43
+
44
+ def self.ruby_logger
45
+ require 'logger'
46
+ Logger.new(logger_file)
47
+ end
48
+
49
+ def self.logger_file
50
+ @logger_file || STDOUT
51
+ end
52
+
53
+ def self.logger_file=(path)
54
+ @logger_file = File.expand_path(path, root)
12
55
  end
13
56
 
14
57
  end
@@ -2,15 +2,14 @@ namespace :remote_partial do
2
2
 
3
3
  desc "Updates remote partials"
4
4
  task :update => :environment do
5
- # Don't need to do anything more than reload the environment, as remote
6
- # partials are reloaded on application start.
7
- Rails.logger.info "Updated remote partials at #{Time.now.to_s(:db)}"
5
+ RemotePartial::Partial.all.each{|partial| puts partial.name if partial.update_stale_file }
6
+ RemotePartial.logger.info "Updated remote partials at #{Time.now.to_s(:db)}"
8
7
  end
9
8
 
10
9
  desc "Force update of all remote partials"
11
10
  task :force_update => :environment do
12
11
  RemotePartial::Partial.all.each{|partial| puts partial.name if partial.update_file }
13
- Rails.logger.info "Forced update of remote partials at #{Time.now.to_s(:db)}"
12
+ RemotePartial.logger.info "Forced update of remote partials at #{Time.now.to_s(:db)}"
14
13
  end
15
14
 
16
15
  end
data/test/log/test.log ADDED
@@ -0,0 +1,71 @@
1
+ # Logfile created on 2013-08-27 12:15:14 +0100 by logger.rb/31641
2
+ W, [2013-08-27T12:15:14.265154 #4124] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
3
+ W, [2013-08-27T12:15:14.359792 #4124] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
4
+ I, [2013-08-27T12:22:34.914264 #4152] INFO -- : Preparing to test logging via assert_log_entry_added
5
+ W, [2013-08-27T12:22:34.931198 #4152] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
6
+ I, [2013-08-27T12:22:34.997206 #4152] INFO -- : Preparing to test logging via assert_log_entry_added
7
+ W, [2013-08-27T12:22:35.003639 #4152] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
8
+ I, [2013-08-27T12:23:35.450015 #4158] INFO -- : Preparing to test logging via assert_log_entry_added
9
+ W, [2013-08-27T12:23:35.466803 #4158] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
10
+ I, [2013-08-27T12:23:35.553864 #4158] INFO -- : Preparing to test logging via assert_log_entry_added
11
+ W, [2013-08-27T12:23:35.561805 #4158] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
12
+ I, [2013-08-27T12:24:16.551530 #4164] INFO -- : Preparing to test logging via assert_log_entry_added
13
+ W, [2013-08-27T12:24:16.572608 #4164] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
14
+ W, [2013-08-27T12:24:16.577606 #4164] WARN -- : Something
15
+ I, [2013-08-27T12:24:16.669041 #4164] INFO -- : Preparing to test logging via assert_log_entry_added
16
+ W, [2013-08-27T12:24:16.678542 #4164] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
17
+ W, [2013-08-27T12:24:16.678898 #4164] WARN -- : Something
18
+ I, [2013-08-27T12:25:04.174618 #4170] INFO -- : Preparing to test logging via assert_log_entry_added
19
+ W, [2013-08-27T12:25:04.183392 #4170] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
20
+ W, [2013-08-27T12:25:04.183934 #4170] WARN -- : Something
21
+ I, [2013-08-27T12:25:04.239296 #4170] INFO -- : Preparing to test logging via assert_log_entry_added
22
+ W, [2013-08-27T12:25:04.248463 #4170] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
23
+ W, [2013-08-27T12:25:04.248969 #4170] WARN -- : Something
24
+ I, [2013-08-27T12:25:41.486448 #4176] INFO -- : Preparing to test logging via assert_log_entry_added
25
+ W, [2013-08-27T12:25:41.495794 #4176] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
26
+ I, [2013-08-27T12:25:41.576469 #4176] INFO -- : Preparing to test logging via assert_log_entry_added
27
+ W, [2013-08-27T12:25:41.582383 #4176] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
28
+ I, [2013-08-27T12:30:16.660423 #4193] INFO -- : Preparing to test logging via assert_log_entry_added
29
+ W, [2013-08-27T12:30:16.670642 #4193] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
30
+ I, [2013-08-27T12:30:16.681108 #4193] INFO -- : Preparing to test logging via assert_log_entry_added
31
+ W, [2013-08-27T12:30:16.689568 #4193] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
32
+ I, [2013-08-27T12:33:49.541983 #4208] INFO -- : Preparing to test logging via assert_log_entry_added
33
+ W, [2013-08-27T12:33:49.559204 #4208] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
34
+ I, [2013-08-27T12:33:49.628574 #4208] INFO -- : Preparing to test logging via assert_log_entry_added
35
+ W, [2013-08-27T12:33:49.633089 #4208] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
36
+ I, [2013-08-27T12:36:55.514639 #4220] INFO -- : Preparing to test logging via assert_log_entry_added
37
+ W, [2013-08-27T12:36:55.522597 #4220] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
38
+ I, [2013-08-27T12:36:55.573305 #4220] INFO -- : Preparing to test logging via assert_log_entry_added
39
+ W, [2013-08-27T12:36:55.581054 #4220] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
40
+ I, [2013-08-27T12:39:49.212560 #4245] INFO -- : Preparing to test logging via assert_log_entry_added
41
+ W, [2013-08-27T12:39:49.221175 #4245] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
42
+ I, [2013-08-27T12:39:49.276287 #4245] INFO -- : Preparing to test logging via assert_log_entry_added
43
+ W, [2013-08-27T12:39:49.282917 #4245] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
44
+ I, [2013-08-27T12:41:32.989079 #4261] INFO -- : Preparing to test logging via assert_log_entry_added
45
+ W, [2013-08-27T12:41:32.998436 #4261] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
46
+ I, [2013-08-27T12:41:33.001913 #4261] INFO -- : Preparing to test logging via assert_log_entry_added
47
+ W, [2013-08-27T12:41:33.006315 #4261] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
48
+ I, [2013-08-27T12:42:17.797802 #4267] INFO -- : Preparing to test logging via assert_log_entry_added
49
+ W, [2013-08-27T12:42:17.811087 #4267] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
50
+ I, [2013-08-27T12:42:17.862230 #4267] INFO -- : Preparing to test logging via assert_log_entry_added
51
+ W, [2013-08-27T12:42:17.869433 #4267] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
52
+ I, [2013-08-27T13:23:37.373208 #4359] INFO -- : Preparing to test logging via assert_log_entry_added
53
+ W, [2013-08-27T13:23:37.379581 #4359] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
54
+ I, [2013-08-27T13:23:37.414101 #4359] INFO -- : Preparing to test logging via assert_log_entry_added
55
+ W, [2013-08-27T13:23:37.420912 #4359] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
56
+ I, [2013-08-27T13:38:35.893444 #4547] INFO -- : Preparing to test logging via assert_log_entry_added
57
+ W, [2013-08-27T13:38:35.911561 #4547] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
58
+ I, [2013-08-27T13:38:36.019684 #4547] INFO -- : Preparing to test logging via assert_log_entry_added
59
+ W, [2013-08-27T13:38:36.029552 #4547] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
60
+ I, [2013-08-27T13:38:40.715087 #4557] INFO -- : Preparing to test logging via assert_log_entry_added
61
+ W, [2013-08-27T13:38:40.720867 #4557] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
62
+ I, [2013-08-27T13:38:40.787452 #4557] INFO -- : Preparing to test logging via assert_log_entry_added
63
+ W, [2013-08-27T13:38:40.795165 #4557] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
64
+ I, [2013-08-27T13:38:44.050174 #4563] INFO -- : Preparing to test logging via assert_log_entry_added
65
+ W, [2013-08-27T13:38:44.059219 #4563] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
66
+ I, [2013-08-27T13:38:44.128763 #4563] INFO -- : Preparing to test logging via assert_log_entry_added
67
+ W, [2013-08-27T13:38:44.133836 #4563] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
68
+ I, [2013-08-27T14:59:07.213269 #5477] INFO -- : Preparing to test logging via assert_log_entry_added
69
+ W, [2013-08-27T14:59:07.222037 #5477] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
70
+ I, [2013-08-27T14:59:07.238897 #5477] INFO -- : Preparing to test logging via assert_log_entry_added
71
+ W, [2013-08-27T14:59:07.244542 #5477] WARN -- : Unable to retrieve remote partial at http://www.warwickshire.gov.uk: #<Net::HTTPBadRequest 400 readbody=true>
data/test/test_helper.rb CHANGED
@@ -1,23 +1,13 @@
1
- # Configure Rails Environment
2
- ENV["RAILS_ENV"] = "test"
3
-
4
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
- require "rails/test_help"
1
+ require 'minitest/unit'
6
2
  require 'webmock'
7
3
  require 'webmock/minitest'
8
4
  require 'webmock/test_unit'
5
+ require_relative '../lib/remote_partial'
9
6
 
10
- Rails.backtrace_cleaner.remove_silencers!
11
-
12
- # Load support files
13
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
7
+ RemotePartial.root = File.expand_path('..', __FILE__)
8
+ RemotePartial.logger_file = 'log/test.log'
14
9
 
15
- # Load fixtures from the engine
16
- if ActiveSupport::TestCase.method_defined?(:fixture_path=)
17
- ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
18
- end
19
-
20
- class ActiveSupport::TestCase
10
+ class MiniTest::Unit::TestCase
21
11
 
22
12
  def enable_mock(url, body = '<body><h1>Something</h1><p>Else</p></body>')
23
13
  stub_request(:get, url).
@@ -67,6 +57,37 @@ class ActiveSupport::TestCase
67
57
  assert_file_does_not_exist @partial.output_file_name
68
58
  end
69
59
 
60
+ def assert_difference(expression, difference = 1, message = nil, &block)
61
+ expressions = Array(expression)
62
+
63
+ exps = expressions.map { |e|
64
+ e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
65
+ }
66
+ before = exps.map { |e| e.call }
67
+
68
+ yield
69
+
70
+ expressions.zip(exps).each_with_index do |(code, e), i|
71
+ error = "#{code.inspect} didn't change by #{difference}"
72
+ error = "#{message}.\n#{error}" if message
73
+ assert_equal(before[i] + difference, e.call, error)
74
+ end
75
+ end
76
+
77
+ def assert_no_difference(expression, message = nil, &block)
78
+ assert_difference expression, 0, message, &block
79
+ end
80
+
81
+ def assert_log_entry_added(text, &block)
82
+ RemotePartial.logger.info 'Preparing to test logging via assert_log_entry_added'
83
+ before = File.size(RemotePartial.logger_file)
84
+ block.call
85
+ assert(before < File.size(RemotePartial.logger_file), "#{RemotePartial.logger_file} should have increased in size")
86
+ last_entry = File.readlines(RemotePartial.logger_file).last
87
+ assert_match text, last_entry, "Logger output should contain #{text}"
88
+
89
+ end
90
+
70
91
  def remove_output_file
71
92
  remove_file @partial.output_file_name
72
93
  end
@@ -1,14 +1,14 @@
1
1
  require 'test_helper'
2
2
 
3
3
  module RemotePartial
4
- class BuilderTest < ActiveSupport::TestCase
4
+ class BuilderTest < MiniTest::Unit::TestCase
5
5
 
6
6
  def setup
7
7
  @partial = Partial.create(
8
8
  name: 'simple',
9
9
  url: 'http://www.warwickshire.gov.uk',
10
10
  criteria: 'p:first-child',
11
- repeat_period: 10.minutes
11
+ repeat_period: TimeCalc.minutes(10)
12
12
  )
13
13
  @name = 'foo'
14
14
  @url = @partial.url
@@ -76,12 +76,14 @@ module RemotePartial
76
76
 
77
77
  def test_build_with_http_error
78
78
  enable_mock_connection_failure @url
79
- assert_output_file_not_updated do
80
- assert_no_difference 'RemotePartial::Partial.count' do
81
- Builder.build(
82
- url: @partial.url,
83
- name: @partial.name
84
- )
79
+ assert_log_entry_added('HTTPBadRequest') do
80
+ assert_output_file_not_updated do
81
+ assert_no_difference 'RemotePartial::Partial.count' do
82
+ Builder.build(
83
+ url: @partial.url,
84
+ name: @partial.name
85
+ )
86
+ end
85
87
  end
86
88
  end
87
89
  end
@@ -98,12 +100,13 @@ module RemotePartial
98
100
  assert_difference 'RemotePartial::Partial.count' do
99
101
  Builder.build(
100
102
  url: @partial.url,
101
- name: 'partial_with_mod',
103
+ name: name,
102
104
  output_modifier: output_modifier
103
105
  )
104
106
  end
105
107
  partial = Partial.find(name)
106
108
  assert_equal(output_modifier, partial.output_modifier)
109
+ remove_file(partial.output_file_name)
107
110
  end
108
111
 
109
112
  def assert_expected_file_created(&test)
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  module RemotePartial
4
- class ExceptionTest < ActiveSupport::TestCase
4
+ class ExceptionTest < MiniTest::Unit::TestCase
5
5
  def test_remote_partial_retrival_error
6
6
  inner_message = 'Whoops'
7
7
  catch_exception { raise RemotePartialRetrivalError.new(inner_message, Array)}