vcr-archive 0.2.0 → 0.3.0.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de71bed807fa08fe527f584320a64e8d2cbd8865
4
- data.tar.gz: e54747a4c1de2a6c430cb65d908a297b964283f9
3
+ metadata.gz: 5228f750fa913d827a1bd296a0a4f6cc90d79609
4
+ data.tar.gz: dc82326c218702636850f6f5e034a94be9e8c199
5
5
  SHA512:
6
- metadata.gz: 58211dfb81169d016e7e33321d0805112b0c1aee48bd89481372a355bc52b62ef30fa5147c0e3b3315002ce507e132a24236c3a8bdb028907baf99b90a276841
7
- data.tar.gz: 28ff7a48392ca176bf533ab625a643d6f3123fa8fca758ca566b35a6265d7fcfe52ebf7b58773c79189d48e767c31f36b98fb16f24cc120af6779c01eda42839
6
+ metadata.gz: 0fd3b2e7b7d8f3819a9e4831dec43e5fb6f79be3a1b8d2a0990f0016f5ec5caaff6f941b041c2cf5585f4096d2c1af303df6df866e08779f1a9129d884abaaa9
7
+ data.tar.gz: a07c32c19df1ac6f150b274c67cb7f3a36f4a46354b3c375a95ef2e383900c5f64bc2383cf383afe4debe544c7de57f5d0afb679b5aac63dadd5eebda7efc285
data/.travis.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.0
4
+ - 2.1.10
5
+ - 2.2.5
6
+ - 2.3.1
5
7
  before_install: gem install bundler -v 1.12.5
8
+ cache: bundler
data/README.md CHANGED
@@ -22,7 +22,8 @@ Or install it yourself as:
22
22
 
23
23
  ```ruby
24
24
  require 'vcr/archive'
25
- VCR.use_cassette('path/to/git/repo/archive') do
25
+
26
+ VCR.use_cassette('https://github.com/everypolitician/vcr-archive') do
26
27
  response = open('http://example.org/').read
27
28
  # ...
28
29
  end
@@ -1,5 +1,5 @@
1
1
  module VCR
2
2
  module Archive
3
- VERSION = '0.2.0'.freeze
3
+ VERSION = '0.3.0.pre'.freeze
4
4
  end
5
5
  end
data/lib/vcr/archive.rb CHANGED
@@ -1,102 +1,83 @@
1
- require 'vcr'
1
+ require 'yaml'
2
2
 
3
3
  require 'vcr/archive/version'
4
4
 
5
5
  module VCR
6
6
  module Archive
7
+ module Serializer
8
+ extend self
7
9
 
8
- extend self
9
-
10
- attr_accessor :git_repository_url
11
-
12
- class Serializer
13
- def self.file_extension
14
- 'git'
10
+ def file_extension
11
+ 'archive'
15
12
  end
16
13
 
17
- def self.serialize(hash)
14
+ def serialize(hash)
18
15
  hash
19
16
  end
20
17
 
21
- def self.deserialize(hash)
18
+ def deserialize(hash)
22
19
  hash
23
20
  end
24
21
  end
25
22
 
26
-
27
23
  module Persister
28
24
  extend self
29
25
 
30
- def [](_)
31
- nil
32
- end
26
+ attr_reader :storage_location
33
27
 
34
- def []=(_, _)
35
- nil
28
+ def storage_location=(dir)
29
+ FileUtils.mkdir_p(dir) if dir
30
+ @storage_location = dir ? absolute_path_for(dir) : nil
36
31
  end
37
32
 
38
- def absolute_path_to_file(storage_key)
39
- storage_key
33
+ def [](file_name)
34
+ path = absolute_path_to_file(file_name)
35
+ files = Dir.glob("#{path}/**/*.yml")
36
+ return nil unless files.any?
37
+ interactions = files.map do |f|
38
+ meta = YAML.load_file(f)
39
+ body = File.binread(f.sub(/\.yml$/, '.html'))
40
+ meta['response']['body']['string'] = body
41
+ meta
42
+ end
43
+ {
44
+ 'http_interactions' => interactions,
45
+ }
40
46
  end
41
- end
42
-
43
- module GitRepository
44
- extend self
45
47
 
46
- def tmpdir
47
- @tmpdir ||= Dir.mktmpdir
48
+ def []=(file_name, meta)
49
+ path = absolute_path_to_file(file_name)
50
+ meta['http_interactions'].each do |interaction|
51
+ uri = URI.parse(interaction['request']['uri'])
52
+ path = File.join(path, uri.host, Digest::SHA1.hexdigest(uri.to_s))
53
+ directory = File.dirname(path)
54
+ FileUtils.mkdir_p(directory) unless File.exist?(directory)
55
+ body = interaction['response']['body'].delete('string')
56
+ File.binwrite("#{path}.yml", YAML.dump(interaction))
57
+ File.binwrite("#{path}.html", body)
58
+ end
48
59
  end
49
60
 
50
- def directory
51
- @directory ||= File.join(tmpdir, git_repository_url.split('/').last)
61
+ def absolute_path_to_file(file_name)
62
+ return nil unless storage_location
63
+ File.join(storage_location, sanitized_file_name_from(file_name))
52
64
  end
53
65
 
54
- def git_repository_url
55
- VCR::Archive.git_repository_url
56
- end
66
+ private
57
67
 
58
- def branch_name
59
- @branch_name ||= 'scraped-pages-archive'
68
+ def absolute_path_for(path)
69
+ Dir.chdir(path) { Dir.pwd }
60
70
  end
61
71
 
62
- def commit_all(message, &block)
63
- unless File.directory?(File.join(directory, '.git'))
64
- system("git clone --quiet #{git_repository_url} #{directory}")
65
- end
66
- Dir.chdir(directory) do
67
- if system("git rev-parse --verify origin/#{branch_name} > /dev/null 2>&1")
68
- system("git checkout --quiet #{branch_name}")
69
- else
70
- system("git checkout --orphan #{branch_name}")
71
- system("git rm --quiet -rf .")
72
- end
73
-
74
- yield(directory)
75
-
76
- system("git add .")
77
- system("git commit --quiet --allow-empty --message='#{message}'")
78
- system("git push --quiet origin #{branch_name}")
79
- end
80
- end
81
- end
72
+ def sanitized_file_name_from(file_name)
73
+ parts = file_name.to_s.split('.')
82
74
 
83
- VCR.configure do |config|
84
- config.hook_into :webmock
85
- config.cassette_serializers[:vcr_archive] = Serializer
86
- config.cassette_persisters[:vcr_archive] = Persister
87
- config.default_cassette_options = { serialize_with: :vcr_archive, persist_with: :vcr_archive, record: :all }
88
- config.before_record do |interaction, cassette|
89
- uri = URI.parse(interaction.request.uri)
90
- message = "#{interaction.response.status.to_hash.values_at('code', 'message').join(' ')} #{uri}"
91
- GitRepository.commit_all(message) do |directory|
92
- path = File.join(directory, uri.host, Digest::SHA1.hexdigest(uri.to_s))
93
- directory = File.dirname(path)
94
- FileUtils.mkdir_p(directory) unless File.exist?(directory)
95
- meta = interaction.to_hash
96
- body = meta['response']['body'].delete('string')
97
- File.binwrite("#{path}.yml", YAML.dump(meta))
98
- File.binwrite("#{path}.html", body)
75
+ # Get rid of the unneeded extension on the file_name
76
+ if parts.size > 1 && parts.last == Serializer.file_extension
77
+ parts.pop
99
78
  end
79
+
80
+ parts.join('.').gsub(/[^\w\-\/]+/, '_')
100
81
  end
101
82
  end
102
83
  end
data/vcr-archive.gemspec CHANGED
@@ -24,4 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'bundler', '~> 1.12'
25
25
  spec.add_development_dependency 'rake', '~> 10.0'
26
26
  spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'pry', '~> 0.10.4'
28
+ spec.add_development_dependency 'simplecov', '~> 0.12.0'
27
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vcr-archive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Mytton
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.4
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.4
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.12.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.12.0
83
111
  description:
84
112
  email:
85
113
  - chrismytton@gmail.com
@@ -113,9 +141,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
141
  version: '0'
114
142
  required_rubygems_version: !ruby/object:Gem::Requirement
115
143
  requirements:
116
- - - ">="
144
+ - - ">"
117
145
  - !ruby/object:Gem::Version
118
- version: '0'
146
+ version: 1.3.1
119
147
  requirements: []
120
148
  rubyforge_project:
121
149
  rubygems_version: 2.5.1