vcr-archive 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df4561f7124c25def97db1155a3e0b2a83b00eaf
4
- data.tar.gz: 3f20ddca7ef92064fbcb908483b5828366482af7
3
+ metadata.gz: de71bed807fa08fe527f584320a64e8d2cbd8865
4
+ data.tar.gz: e54747a4c1de2a6c430cb65d908a297b964283f9
5
5
  SHA512:
6
- metadata.gz: e0eb49a6ff7a09f0bbd11ee563596e7433acfbfd529ff714e91897f1222b3502f7e4b15cfbb98eadd191d9e3d1967d69ce294a208b0b1d810e7dc950ea006604
7
- data.tar.gz: 03f220e9803e54fada95b198e2a5ad7e075cf84778303eb251571a3d5a3a0ecbd6ed536a48ea5d32af4b1b0c026c52791566bc7c49c5060b7db742c0888d3e17
6
+ metadata.gz: 58211dfb81169d016e7e33321d0805112b0c1aee48bd89481372a355bc52b62ef30fa5147c0e3b3315002ce507e132a24236c3a8bdb028907baf99b90a276841
7
+ data.tar.gz: 28ff7a48392ca176bf533ab625a643d6f3123fa8fca758ca566b35a6265d7fcfe52ebf7b58773c79189d48e767c31f36b98fb16f24cc120af6779c01eda42839
@@ -1,5 +1,5 @@
1
1
  module VCR
2
2
  module Archive
3
- VERSION = '0.1.0'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
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
- class YamlSeparateHtmlSerializer
7
+
8
+ extend self
9
+
10
+ attr_accessor :git_repository_url
11
+
12
+ class Serializer
8
13
  def self.file_extension
9
- 'archive'
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
- class YamlSeparateHtmlPersister
22
- def self.[](interactions_directory)
23
- interactions_directory.sub!(/\.archive$/, '')
24
- return nil unless File.directory?(interactions_directory)
25
- files = Dir.glob("#{interactions_directory}/**/*.yml")
26
- return nil unless files.any?
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 self.[]=(interactions_directory, meta)
40
- interactions_directory.sub!(/\.archive$/, '')
41
- meta['http_interactions'].each do |interaction|
42
- uri = URI.parse(interaction['request']['uri'])
43
- path = File.join(interactions_directory, uri.host, Digest::SHA1.hexdigest(uri.to_s))
44
- directory = File.dirname(path)
45
- FileUtils.mkdir_p(directory) unless File.exist?(directory)
46
- body = interaction['response']['body'].delete('string')
47
- File.binwrite("#{path}.yml", YAML.dump(interaction))
48
- File.binwrite("#{path}.html", body)
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[:yaml_separate_html] = YamlSeparateHtmlSerializer
57
- config.cassette_persisters[:yaml_separate_html] = YamlSeparateHtmlPersister
58
- config.default_cassette_options = { serialize_with: :yaml_separate_html, persist_with: :yaml_separate_html }
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
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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Mytton