jsmestad-stale_fish 1.1.3 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.3
1
+ 1.2.1
data/lib/stale_fish.rb CHANGED
@@ -4,11 +4,14 @@ require 'yaml'
4
4
  require 'rubygems'
5
5
  require 'activesupport' # only used for time helpers
6
6
  require 'resourceful'
7
- begin
8
- require "fakeweb"
9
- rescue LoadError
10
7
 
11
- end
8
+ require 'stale_fish/utility'
9
+ require 'stale_fish/fixture_definition'
10
+
11
+ begin
12
+ require 'fakeweb'
13
+ require 'stale_fish/fakeweb'
14
+ rescue LoadError; end
12
15
 
13
16
  module StaleFish
14
17
  # no one likes stale fish.
@@ -17,114 +20,38 @@ module StaleFish
17
20
  class MalformedSourceURL < StandardError; end
18
21
 
19
22
  class << self
20
- attr_accessor :use_fakeweb
21
- attr_accessor :config_path
22
- attr_accessor :yaml
23
23
  attr_accessor :configuration
24
24
  attr_accessor :http
25
+ attr_accessor :fixtures
26
+ attr_accessor :force_flag
25
27
  end
26
28
 
27
- self.use_fakeweb = false
28
- self.config_path = 'stale_fish.yml'
29
+ self.fixtures = []
30
+ Utility.config_path = 'stale_fish.yml'
29
31
  self.http = Resourceful::HttpAccessor.new
30
32
 
31
- def self.valid_path?
32
- !config_path.nil? ? File.exist?(config_path) : false
33
- end
34
-
35
33
  def self.update_stale(*args)
36
- # check each file for update
37
- load_yaml if self.yaml.nil?
38
- stale = flag_stale(args)
39
- process(stale)
40
- write_yaml
41
- return stale.size
42
- end
43
-
44
- def self.register_uri(source_uri, response)
45
- FakeWeb.register_uri(:any, source_uri, :body => response) if use_fakeweb && !FakeWeb.registered_uri?(source_uri)
46
- end
47
-
48
- def self.load_yaml
49
- raise Errno::ENOENT, 'invalid path, please set StaleFish.config_path than ensure StaleFish.valid_path? is true' unless valid_path?
50
-
51
- self.yaml = YAML.load_file(config_path)
52
- raise YAML::Error, 'missing stale root element' unless self.yaml['stale']
53
-
54
- # Grab Configuration from YAML
55
- self.configuration = self.yaml['stale'].delete('configuration')
56
- use_fakeweb = (configuration['use_fakeweb'] || false) unless configuration.nil?
57
-
58
- # Process remaining nodes as items
59
- self.yaml['stale'].each do |key, value|
60
- %w{ filepath frequency source }.each { |field| raise YAML::Error, "missing #{field} node for #{key}" unless value[field] }
61
- end
62
- end
63
-
64
- protected
65
-
66
- def self.flag_stale(args)
67
- force = args.pop[:force] if args.last.is_a?(Hash)
68
- stale, scope = {}, self.yaml['stale']
69
- scope.each do |key, value|
70
- if args.empty?
71
- if value['updated'].blank?
72
- stale.merge!({key => value})
73
- else
74
- last_modified = value['updated']
75
- update_on = DateTime.now + eval(value['frequency'])
76
- if last_modified > update_on
77
- stale.merge!({key => value})
78
- else
79
- register_uri(value['source'], value['filepath'])
80
- end
81
- end
82
- else
83
- last_modified = value['updated']
84
- update_on = DateTime.now + eval(value['frequency'])
85
- if force == true
86
- stale.merge!({key => value}) if args.include?(key)
87
- else
88
- if args.include?(key) && (value['updated'].blank? || last_modified > update_on)
89
- stale.merge!({key => value})
90
- else
91
- register_uri(value['source'], value['filepath'])
92
- end
34
+ # TODO move this to its own spot
35
+ Utility.loader if self.fixtures.empty?
36
+ self.force_flag = args.pop[:force] if args.last.is_a?(Hash)
37
+
38
+ if args.empty?
39
+ stale = fixtures.select { |f| f.is_stale? }
40
+ fixtures.each do |fixture|
41
+ fixture.update! if fixture.is_stale?
42
+ fixture.register_uri if StaleFish.use_fakeweb
43
+ end
44
+ else
45
+ stale = fixtures.select { |f| f.is_stale? && args.include?(f.tag) }
46
+ fixtures.each do |fixture|
47
+ if args.include?(fixture.tag)
48
+ fixture.update! if fixture.is_stale?
49
+ fixture.register_uri if StaleFish.use_fakeweb
93
50
  end
94
51
  end
95
52
  end
96
- return stale
97
- end
98
-
99
- def self.process(fixtures)
100
- FakeWeb.allow_net_connect = true if use_fakeweb
101
-
102
- fixtures.each do |key, value|
103
-
104
- begin
105
- @response = http.resource(value['source']).get
106
- File.open(value['filepath'], 'w') { |f| f.write @response.body.to_s }
107
- update_fixture(key)
108
- rescue Resourceful::UnsuccessfulHttpRequestError
109
- raise FixtureUpdateFailure, "#{key}'s source: #{value['source']} returned unsuccessfully."
110
- rescue ArgumentError
111
- raise MalformedSourceURL, "#{key}'s source: #{value['source']} is not a valid URL path. Most likely it's missing a trailing slash."
112
- end
113
-
114
- register_uri(value['source'], value['filepath'])
115
- end
116
-
117
- FakeWeb.allow_net_connect = false if use_fakeweb
118
- end
119
-
120
- def self.update_fixture(key)
121
- self.yaml['stale'][key]['updated'] = DateTime.now
122
- end
123
-
124
- def self.write_yaml
125
- File.open(config_path, "w+") do |f|
126
- f.write(self.yaml.to_yaml)
127
- end
53
+ Utility.writer
54
+ return stale.size
128
55
  end
129
56
 
130
57
  end
@@ -0,0 +1,26 @@
1
+ module StaleFish
2
+
3
+ class << self
4
+ attr_accessor :use_fakeweb
5
+ end
6
+
7
+ self.use_fakeweb = true
8
+
9
+ class FixtureDefinition
10
+ alias_method :update_without_fakeweb, :update!
11
+
12
+ def update!
13
+ FakeWeb.allow_net_connect = true
14
+ update_without_fakeweb
15
+ FakeWeb.allow_net_connect = false
16
+ end
17
+
18
+ def register_uri
19
+ if StaleFish.use_fakeweb && !FakeWeb.registered_uri?(source_url)
20
+ FakeWeb.register_uri(:any, source_url, :body => file_path)
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,34 @@
1
+ module StaleFish
2
+
3
+ class FixtureDefinition
4
+ attr_accessor :tag, :file_path, :source_url, :last_updated_at, :response
5
+ attr_accessor :update_frequency
6
+
7
+ def is_stale?
8
+ StaleFish.force_flag || last_updated_at.nil? || ((DateTime.now + eval(update_frequency)) < last_updated_at)
9
+ end
10
+
11
+ def update!
12
+ begin
13
+ self.response = StaleFish.http.resource(source_url).get
14
+ File.open(file_path, 'w') { |file| file.write(response.body.to_s) }
15
+ self.last_updated_at = DateTime.now
16
+ rescue Resourceful::UnsuccessfulHttpRequestError
17
+ raise StaleFish::FixtureUpdateFailure, "#{key}'s source: #{value['source']} returned unsuccessfully."
18
+ rescue ArgumentError
19
+ raise StaleFish::MalformedSourceURL, "#{key}'s source: #{value['source']} is not a valid URL path. Most likely it's missing a trailing slash."
20
+ end
21
+ end
22
+
23
+ def output_hash
24
+ <<-EOF
25
+ #{tag}:
26
+ file_path: #{file_path}
27
+ source_url: #{source_url}
28
+ update_frequency: #{update_frequency}
29
+ last_updated_at: #{last_updated_at}
30
+ EOF
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,67 @@
1
+ require 'singleton'
2
+
3
+ module StaleFish
4
+ module Utility
5
+ class << self
6
+ attr_accessor :config_path
7
+ end
8
+
9
+ def self.valid_path?
10
+ !config_path.nil? ? File.exist?(config_path) : false
11
+ end
12
+
13
+ def self.loader
14
+ raise Errno::ENOENT, 'invalid config path: #{Utility.config_path}; ensure StaleFish::Utility.valid_path? is true' unless Utility.valid_path?
15
+
16
+ data = YAML.load_file(config_path)
17
+
18
+ config = data['stale'].delete('configuration')
19
+ StaleFish.use_fakeweb = (config['use_fakeweb'] || false) unless config.nil?
20
+
21
+ raise YAML::Error, 'missing root element' unless data['stale']
22
+
23
+ # Reset these
24
+ StaleFish.fixtures = []
25
+ StaleFish.force_flag = false
26
+
27
+ deprecated_var_names = {
28
+ 'file_path' => 'filepath',
29
+ 'update_frequency' => 'frequency',
30
+ 'source_url' => 'source',
31
+ 'last_updated_at' => 'updated'
32
+ }
33
+
34
+ data['stale'].each do |key, value|
35
+ definition = FixtureDefinition.new
36
+ definition.tag = key
37
+ %w{ file_path update_frequency source_url }.each do |field|
38
+ if value[deprecated_var_names[field]]
39
+ $stderr.puts "Deprecation warning: #{field} has been replaced with #{deprecated_var_names[field]}. Please update #{Utility.config_path}."
40
+ definition.send((field + '=').to_sym, value[deprecated_var_names[field]])
41
+ else
42
+ raise YAML::Error, "missing #{field} node for #{key} in #{Utility.config_path}" unless value[field]
43
+ definition.send((field + '=').to_sym, value[field])
44
+ end
45
+ end
46
+
47
+ $stderr.puts "Deprecation warning: updated has been changed to last_updated_at; this change is automatic." if value.has_key?('updated')
48
+ definition.last_updated_at = value['last_updated_at'] || value[deprecated_var_names['last_updated_at']] || nil
49
+
50
+ StaleFish.fixtures << definition
51
+ end
52
+ end
53
+
54
+ def self.writer
55
+ string = <<-EOF
56
+ stale:
57
+ configuration:
58
+ use_fakeweb: #{StaleFish.use_fakeweb}
59
+ EOF
60
+ StaleFish.fixtures.each do |fix|
61
+ string << fix.output_hash
62
+ end
63
+
64
+ File.open(config_path, "w+") { |f| f.write(string) }
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,20 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe StaleFish do
4
+
5
+ before do
6
+ @deprecated_yaml = File.dirname(__FILE__) + "/fixtures/deprecated_yaml.yml"
7
+ end
8
+
9
+ it "should assign the config file" do
10
+ StaleFish::Utility.config_path = @deprecated_yaml
11
+ StaleFish::Utility.config_path.should == @deprecated_yaml
12
+ StaleFish::Utility.valid_path?.should == true
13
+ end
14
+
15
+ it "should map to valid attributes" do
16
+ pending
17
+ StaleFish::Utility.loader
18
+ end
19
+
20
+ end
@@ -0,0 +1,12 @@
1
+ ---
2
+ stale:
3
+ yahoo:
4
+ filepath: ./tmp/yahoo.html
5
+ source: http://www.yahoo.com/
6
+ updated:
7
+ frequency: 1.day
8
+ google:
9
+ filepath: ./tmp/google.html
10
+ source: http://www.google.com/
11
+ updated:
12
+ frequency: 2.weeks
@@ -1,5 +1,5 @@
1
1
  stale:
2
2
  google:
3
- filepath: google.html
3
+ file_path: google.html
4
4
  yahoo:
5
- frequency: 2.weeks.ago
5
+ update_frequency: 2.weeks.ago
@@ -1,12 +1,11 @@
1
- ---
2
1
  stale:
3
2
  yahoo:
4
- filepath: ./tmp/yahoo.html
5
- source: http://www.yahoo.com/
6
- updated:
7
- frequency: 1.day
3
+ file_path: ./tmp/yahoo.html
4
+ source_url: http://www.yahoo.com/
5
+ last_updated_at:
6
+ update_frequency: 1.day
8
7
  google:
9
- filepath: ./tmp/google.html
10
- source: http://www.google.com/
11
- updated:
12
- frequency: 2.weeks
8
+ file_path: ./tmp/google.html
9
+ source_url: http://www.google.com/
10
+ last_updated_at:
11
+ update_frequency: 2.weeks
@@ -1,12 +1,13 @@
1
- ---
2
- stale:
3
- yahoo:
4
- filepath: ./tmp/yahoo.html
5
- frequency: 1.day
6
- updated: 2009-09-15T11:58:51-06:00
7
- source: http://www.yahoo.com/
8
- google:
9
- filepath: ./tmp/google.html
10
- frequency: 2.weeks
11
- updated: 2009-09-15T11:58:51-06:00
12
- source: http://www.google.com/
1
+ stale:
2
+ configuration:
3
+ use_fakeweb: true
4
+ yahoo:
5
+ file_path: ./tmp/yahoo.html
6
+ source_url: http://www.yahoo.com/
7
+ update_frequency: 1.day
8
+ last_updated_at: 2009-09-25T17:17:20-06:00
9
+ google:
10
+ file_path: ./tmp/google.html
11
+ source_url: http://www.google.com/
12
+ update_frequency: 2.weeks
13
+ last_updated_at: 2009-09-25T17:17:20-06:00
@@ -1,36 +1,44 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "StaleFish" do
4
+
4
5
  before do
5
6
  @valid_yaml = File.dirname(__FILE__) + "/fixtures/stale_fish.yml"
6
7
  end
7
8
 
8
9
  it "should assign the config file" do
9
- StaleFish.config_path = @valid_yaml
10
- StaleFish.config_path.should == @valid_yaml
11
- StaleFish.valid_path?.should == true
10
+ StaleFish::Utility.config_path = @valid_yaml
11
+ StaleFish::Utility.config_path.should == @valid_yaml
12
+ StaleFish::Utility.valid_path?.should == true
12
13
  end
13
14
 
14
15
  context "when loading the yaml file" do
16
+
15
17
  it "should raise errors on malformed yaml" do
16
- StaleFish.config_path = File.dirname(__FILE__) + "/fixtures/malformed_stale_fish.yml"
17
- lambda { StaleFish.load_yaml }.should_not raise_error(Errno::ENOENT)
18
- lambda { StaleFish.load_yaml }.should raise_error(YAML::Error)
18
+ StaleFish::Utility.config_path = File.dirname(__FILE__) + "/fixtures/malformed_stale_fish.yml"
19
+ lambda { StaleFish::Utility.loader }.should_not raise_error(Errno::ENOENT)
20
+ lambda { StaleFish::Utility.loader }.should raise_error(YAML::Error)
21
+ end
22
+
23
+ it "shouldn't fail on valid yaml" do
24
+ StaleFish::Utility.config_path = @valid_yaml
25
+ lambda { StaleFish::Utility.loader }.should_not raise_error(YAML::Error)
19
26
  end
20
27
 
21
- it "should pass on valid yaml" do
22
- StaleFish.config_path = @valid_yaml
23
- lambda { StaleFish.load_yaml }.should_not raise_error(YAML::Error)
28
+ it "should establish the fixture definitions" do
29
+ StaleFish::Utility.config_path = @valid_yaml
30
+ lambda { StaleFish::Utility.loader }
24
31
  end
25
32
 
26
33
  end
27
34
 
28
35
  context "after loading the config" do
36
+
29
37
  before(:each) do
30
- FileUtils.cp(@valid_yaml, File.dirname(__FILE__)+"/fixtures/stale_fish.orig.yml")
31
- StaleFish.config_path = @valid_yaml
32
- StaleFish.valid_path?.should == true
33
- StaleFish.load_yaml
38
+ FileUtils.cp(@valid_yaml, File.dirname(__FILE__) + "/fixtures/stale_fish.orig.yml")
39
+ StaleFish::Utility.config_path = @valid_yaml
40
+ StaleFish::Utility.valid_path?.should == true
41
+ StaleFish::Utility.loader
34
42
  end
35
43
 
36
44
  it "should update all stale fixtures" do
@@ -48,9 +56,9 @@ describe "StaleFish" do
48
56
 
49
57
  it "should not have any remaining fixtures to update" do
50
58
  StaleFish.update_stale.should == 2
51
- StaleFish.yaml = nil # this will force a reload of the YAML file.
52
- StaleFish.yaml.should == nil # ensure it was reset
53
- StaleFish.load_yaml
59
+ StaleFish.fixtures = [] # this will force a reload of the YAML file.
60
+ StaleFish.fixtures.should == [] # ensure it was reset
61
+ StaleFish::Utility.loader
54
62
  StaleFish.update_stale.should == 0
55
63
  end
56
64
 
@@ -59,23 +67,26 @@ describe "StaleFish" do
59
67
  after(:each) do
60
68
  FileUtils.mv(@valid_yaml, File.dirname(__FILE__)+"/../tmp/stale_fish.test.yml")
61
69
  FileUtils.mv(File.dirname(__FILE__)+"/fixtures/stale_fish.orig.yml", @valid_yaml)
62
- StaleFish.yaml = nil
70
+ StaleFish.fixtures = []
63
71
  end
72
+
64
73
  end
65
74
 
66
75
  context "with FakeWeb" do
76
+
67
77
  before(:each) do
68
78
  @fakeweb_yaml = File.dirname(__FILE__) + '/fixtures/stale_fish_fakeweb.yml'
69
- StaleFish.config_path = @fakeweb_yaml
70
- StaleFish.valid_path?.should == true
71
- StaleFish.load_yaml
79
+ StaleFish::Utility.config_path = @fakeweb_yaml
80
+ StaleFish::Utility.valid_path?.should == true
81
+ StaleFish::Utility.loader
72
82
  StaleFish.use_fakeweb = true
73
83
  StaleFish.use_fakeweb.should == true
74
84
  end
75
85
 
76
86
  it "should register any FakeWeb URI's" do
77
- StaleFish.register_uri("http://www.google.com", "some shit")
78
- FakeWeb.registered_uri?("http://www.google.com").should == true
87
+ definition = StaleFish.fixtures.first
88
+ definition.register_uri
89
+ FakeWeb.registered_uri?(definition.source_url).should == true
79
90
  end
80
91
 
81
92
  it "should turn off FakeWeb.allow_net_connect to process stale fixtures" do
@@ -85,8 +96,9 @@ describe "StaleFish" do
85
96
  end
86
97
 
87
98
  after(:each) do
88
- StaleFish.yaml = nil
99
+ StaleFish.fixtures = []
89
100
  end
101
+
90
102
  end
91
103
 
92
104
  end
data/stale_fish.gemspec CHANGED
@@ -5,24 +5,28 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{stale_fish}
8
- s.version = "1.1.3"
8
+ s.version = "1.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Justin Smestad"]
12
- s.date = %q{2009-09-18}
12
+ s.date = %q{2009-09-25}
13
13
  s.email = %q{justin.smestad@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
16
16
  "README.rdoc"
17
17
  ]
18
18
  s.files = [
19
- ".document",
20
- ".gitignore",
19
+ ".gitignore",
21
20
  "LICENSE",
22
21
  "README.rdoc",
23
22
  "Rakefile",
24
23
  "VERSION",
25
24
  "lib/stale_fish.rb",
25
+ "lib/stale_fish/fakeweb.rb",
26
+ "lib/stale_fish/fixture_definition.rb",
27
+ "lib/stale_fish/utility.rb",
28
+ "spec/deprecation_spec.rb",
29
+ "spec/fixtures/deprecated_yaml.yml",
26
30
  "spec/fixtures/google.html",
27
31
  "spec/fixtures/malformed_stale_fish.yml",
28
32
  "spec/fixtures/stale_fish.yml",
@@ -39,7 +43,8 @@ Gem::Specification.new do |s|
39
43
  s.rubygems_version = %q{1.3.5}
40
44
  s.summary = %q{keeps fixtures synchronized with sources to prevent outdated fixtures going undetected.}
41
45
  s.test_files = [
42
- "spec/spec_helper.rb",
46
+ "spec/deprecation_spec.rb",
47
+ "spec/spec_helper.rb",
43
48
  "spec/stale_fish_spec.rb"
44
49
  ]
45
50
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsmestad-stale_fish
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Smestad
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-18 00:00:00 -07:00
12
+ date: 2009-09-25 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -52,13 +52,17 @@ extra_rdoc_files:
52
52
  - LICENSE
53
53
  - README.rdoc
54
54
  files:
55
- - .document
56
55
  - .gitignore
57
56
  - LICENSE
58
57
  - README.rdoc
59
58
  - Rakefile
60
59
  - VERSION
61
60
  - lib/stale_fish.rb
61
+ - lib/stale_fish/fakeweb.rb
62
+ - lib/stale_fish/fixture_definition.rb
63
+ - lib/stale_fish/utility.rb
64
+ - spec/deprecation_spec.rb
65
+ - spec/fixtures/deprecated_yaml.yml
62
66
  - spec/fixtures/google.html
63
67
  - spec/fixtures/malformed_stale_fish.yml
64
68
  - spec/fixtures/stale_fish.yml
@@ -96,5 +100,6 @@ signing_key:
96
100
  specification_version: 3
97
101
  summary: keeps fixtures synchronized with sources to prevent outdated fixtures going undetected.
98
102
  test_files:
103
+ - spec/deprecation_spec.rb
99
104
  - spec/spec_helper.rb
100
105
  - spec/stale_fish_spec.rb
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE