vcr-archive 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/vcr/archive/version.rb +1 -1
- data/lib/vcr/archive.rb +74 -32
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de71bed807fa08fe527f584320a64e8d2cbd8865
|
4
|
+
data.tar.gz: e54747a4c1de2a6c430cb65d908a297b964283f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58211dfb81169d016e7e33321d0805112b0c1aee48bd89481372a355bc52b62ef30fa5147c0e3b3315002ce507e132a24236c3a8bdb028907baf99b90a276841
|
7
|
+
data.tar.gz: 28ff7a48392ca176bf533ab625a643d6f3123fa8fca758ca566b35a6265d7fcfe52ebf7b58773c79189d48e767c31f36b98fb16f24cc120af6779c01eda42839
|
data/lib/vcr/archive/version.rb
CHANGED
data/lib/vcr/archive.rb
CHANGED
@@ -4,9 +4,14 @@ require 'vcr/archive/version'
|
|
4
4
|
|
5
5
|
module VCR
|
6
6
|
module Archive
|
7
|
-
|
7
|
+
|
8
|
+
extend self
|
9
|
+
|
10
|
+
attr_accessor :git_repository_url
|
11
|
+
|
12
|
+
class Serializer
|
8
13
|
def self.file_extension
|
9
|
-
'
|
14
|
+
'git'
|
10
15
|
end
|
11
16
|
|
12
17
|
def self.serialize(hash)
|
@@ -18,44 +23,81 @@ module VCR
|
|
18
23
|
end
|
19
24
|
end
|
20
25
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
interactions = files.map do |f|
|
28
|
-
meta = YAML.load_file(f)
|
29
|
-
body = File.binread(f.sub(/\.yml$/, '.html'))
|
30
|
-
meta['response']['body']['string'] = body
|
31
|
-
meta
|
32
|
-
end
|
33
|
-
{
|
34
|
-
'http_interactions' => interactions,
|
35
|
-
'recorded_with' => File.binread(File.join(interactions_directory, 'recorded_with.txt')),
|
36
|
-
}
|
26
|
+
|
27
|
+
module Persister
|
28
|
+
extend self
|
29
|
+
|
30
|
+
def [](_)
|
31
|
+
nil
|
37
32
|
end
|
38
33
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
34
|
+
def []=(_, _)
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def absolute_path_to_file(storage_key)
|
39
|
+
storage_key
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module GitRepository
|
44
|
+
extend self
|
45
|
+
|
46
|
+
def tmpdir
|
47
|
+
@tmpdir ||= Dir.mktmpdir
|
48
|
+
end
|
49
|
+
|
50
|
+
def directory
|
51
|
+
@directory ||= File.join(tmpdir, git_repository_url.split('/').last)
|
52
|
+
end
|
53
|
+
|
54
|
+
def git_repository_url
|
55
|
+
VCR::Archive.git_repository_url
|
56
|
+
end
|
57
|
+
|
58
|
+
def branch_name
|
59
|
+
@branch_name ||= 'scraped-pages-archive'
|
60
|
+
end
|
61
|
+
|
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}")
|
49
79
|
end
|
50
|
-
File.binwrite(File.join(interactions_directory, 'recorded_with.txt'), meta['recorded_with'])
|
51
80
|
end
|
52
81
|
end
|
53
82
|
|
54
83
|
VCR.configure do |config|
|
55
84
|
config.hook_into :webmock
|
56
|
-
config.cassette_serializers[:
|
57
|
-
config.cassette_persisters[:
|
58
|
-
config.default_cassette_options = { serialize_with: :
|
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)
|
99
|
+
end
|
100
|
+
end
|
59
101
|
end
|
60
102
|
end
|
61
103
|
end
|