mechanize_rspec 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # MechanizeRspec
2
+
3
+ This gem should be used when you test Mechanize offline
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mechanize_rspec'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mechanize_rspec
18
+
19
+ ## Usage
20
+
21
+ ### Set up and Example
22
+
23
+ In your application you should set up
24
+
25
+ > require 'mechanize_rspec'
26
+ > Mechanize::Config.dirname = 'dir_name'
27
+
28
+ Where in dir_name you should have filese:
29
+
30
+ location.yml
31
+ file_1.html
32
+ file_2.html
33
+ ...
34
+ file_n.html
35
+
36
+ Where in file 'location.yml' should be:
37
+
38
+ > ---
39
+ > 'http://HOST/index.html': '/file_1.html'
40
+ > 'http://HOST/pl/index.html': '/file_2.html'
41
+ > ...
42
+ > 'http://HOST/index.html': '/file_n.html'
43
+
44
+ ### Methods
45
+
46
+ Show all used urls (is useful when we want compare with file location.yml)
47
+
48
+ > Mechanize.get("http://www.test.pl")
49
+ > Mechanize::Config.uris.inspect #=> ["http://www.test.pl"]
50
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,58 @@
1
+ require 'yaml'
2
+
3
+ class Mechanize
4
+ module Config
5
+
6
+ def self.location
7
+ @location ||= get_location
8
+ end
9
+
10
+ # pobranie hasha lokalizacji ze wskazanego pliku o ile istnieje
11
+ def self.get_location
12
+ file_source = ::File.join("#{dirname}", "location.yml")
13
+ return {} unless ::File.exist?(file_source)
14
+ result = YAML::load(::File.read(file_source))
15
+ result.class == Hash ? result : {}
16
+ end
17
+
18
+ def self.dirname=(value)
19
+ return unless ::Dir.exist?(value)
20
+ @dirname, @location = value, nil
21
+ end
22
+
23
+ def self.dirname
24
+ @dirname || nil
25
+ end
26
+
27
+ def self.error_uris
28
+ @error_uris
29
+ end
30
+
31
+ def self.push_error_uri(uri)
32
+ @error_uris ||= []
33
+ @error_uris << uri
34
+ end
35
+
36
+ def self.uris
37
+ @uris
38
+ end
39
+
40
+ def self.push_uri(uri)
41
+ @uris ||= []
42
+ @uris << uri
43
+ end
44
+
45
+ # Sprawdzenie czy wszystkie wskazane ścieżki do plików lokalnych istnieją
46
+ def self.check_location
47
+ location.each do |uri, path|
48
+ path = ::File.join("#{dirname}", path)
49
+ if ::File.exist?(path)
50
+ puts "file #{path} exist."
51
+ else
52
+ puts "file #{path} don't exist."
53
+ end
54
+ end
55
+ end
56
+
57
+ end # module Config
58
+ end # module Mechanize
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require 'mechanize'
4
+ require File.expand_path('../config', __FILE__)
5
+
6
+ class Mechanize
7
+ alias :old_get :get
8
+
9
+ # Modyfikacja dotychczasowej metody get
10
+ # tak aby w tej chwili jeżeli jakiś link
11
+ # znajduje się na liście nastąpiło przekierowanie do pliku lokalnego
12
+ def get(uri, parameters = [], referer = nil, headers = {})
13
+ return old_get(uri) if Config.dirname.nil?
14
+ Config.push_uri(uri)
15
+ return old_get(uri) if uri =~ /^(file:)/
16
+ path = Config.location.select{|url, path| url =~ /#{Regexp.quote(uri)}?$/}.first.last
17
+ path.nil? ? (raise "Not correct uri") : old_get(::File.join("file://#{Config.dirname}", path))
18
+ rescue
19
+ Config.push_error_uri(uri)
20
+ old_get("*#{uri}")
21
+ end
22
+ end # class Mechanize
@@ -0,0 +1,3 @@
1
+ module MechanizeRspec
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "mechanize_rspec/version"
2
+ require "mechanize_rspec/mechanize"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path("../lib/mechanize_rspec/version", __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+
7
+ gem.name = "mechanize_rspec"
8
+ gem.version = MechanizeRspec::VERSION
9
+ gem.platform = Gem::Platform::RUBY
10
+ gem.authors = ["Michał Szyma"]
11
+ gem.date = "2012-03-05"
12
+ gem.email = ["raglub.ruby@gmail.com"]
13
+ gem.summary = %q{This gem should be used when you test Mechanize offline}
14
+ gem.description = %q{This gem should be used when you test Mechanize offline}
15
+ gem.homepage = "http://github.com/raglub/mechanize_rspec"
16
+
17
+ gem.files = `git ls-files`.split("\n")
18
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ gem.require_path = "lib"
21
+
22
+ gem.add_dependency "mechanize", ">= 2.1.1"
23
+ gem.add_development_dependency "rspec", ">= 2.10.0"
24
+ end
@@ -0,0 +1,30 @@
1
+ # coding:utf-8
2
+ require 'mechanize_rspec'
3
+
4
+ describe Mechanize do
5
+ before(:each) do
6
+ @tmp_dir = File.expand_path('../pages', __FILE__)
7
+ Mechanize::Config.dirname = @tmp_dir
8
+ #puts Mechanize::Config.location
9
+ @agent = Mechanize.new
10
+ end
11
+
12
+ it "should correct push uris 1" do
13
+ @agent.get('http://www.mechanize/#pl/home.html')
14
+ Mechanize::Config.uris.should include('http://www.mechanize/#pl/home.html')
15
+ end
16
+
17
+ it "should correct push uris 2" do
18
+ @agent.get('/#pl/home.html')
19
+ Mechanize::Config.uris.should include('http://www.mechanize/#pl/home.html')
20
+ end
21
+
22
+ it "should correct push uris 3" do
23
+ begin
24
+ @agent.get('http://www.mechanize/#en/home.html')
25
+ rescue
26
+ end
27
+ Mechanize::Config.uris.should include('http://www.mechanize/#en/home.html')
28
+ end
29
+
30
+ end
@@ -0,0 +1,2 @@
1
+ ---
2
+ 'http://www.mechanize/#pl/home.html': '/test.html'
File without changes
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mechanize_rspec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michał Szyma
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.1.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.1.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.10.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.10.0
46
+ description: This gem should be used when you test Mechanize offline
47
+ email:
48
+ - raglub.ruby@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - README.md
56
+ - Rakefile
57
+ - lib/mechanize_rspec.rb
58
+ - lib/mechanize_rspec/config.rb
59
+ - lib/mechanize_rspec/mechanize.rb
60
+ - lib/mechanize_rspec/version.rb
61
+ - mechanize_rspec.gemspec
62
+ - spec/mechanize_spec.rb
63
+ - spec/pages/location.yml
64
+ - spec/pages/test.html
65
+ homepage: http://github.com/raglub/mechanize_rspec
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.24
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: This gem should be used when you test Mechanize offline
89
+ test_files: []