spn2 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6ec6088e94bed925212bc5aa5abf3211cae5fd0bf5402c49f316d7e4d65f051d
4
+ data.tar.gz: e00cad0b9921f36052ec81271e69f553979dec1e0b6cd540f1bee1c0f712f337
5
+ SHA512:
6
+ metadata.gz: e618df35304407811745df2e7c92106669a01564d4c61c1639fa06a109ffb995fb05e145e12d13efc7cf30fd4c899a959138e0640894bc68c3942efd1035e3d0
7
+ data.tar.gz: cc5616e4848cd9f5bfceb58e163095f9adfc86e584857d3c5c9674bfbbe8a182262d7c779c7d24a87134e7959e7f3a6fcbfc4588f039efc5184919e193d70ca2
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ ---
2
+ require:
3
+ - rubocop-minitest
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ TargetRubyVersion: 3.1
8
+
9
+ Style/HashSyntax:
10
+ Enabled: false # yuk Ruby 3.1
11
+
12
+ Style/NumericLiterals:
13
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-06-29
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :minitest do
4
+ watch(%r{^test/(.*)/?test_(.*)\.rb$})
5
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
6
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/lib/#{m[1]}test_#{m[2]}.rb" }
7
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 MatzFan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ [![Gem Version](https://badge.fury.io/rb/spn2.svg)](https://badge.fury.io/rb/spn2)
2
+
3
+ # Spn2
4
+
5
+ Spn2 is a gem for interacting with the [Wayback Machine](https://web.archive.org/)'s Save Page Now 2 (SPN2) REST API. The API (draft) specification is [here](https://docs.google.com/document/d/1Nsv52MvSjbLb2PCpHlat0gkzw0EvtSgpKHu4mk0MnrA/edit).
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add spn2
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install spn2
16
+
17
+ ## Usage
18
+
19
+ For the Spn2 namespace do:
20
+
21
+ ```rb
22
+ require 'spn2'
23
+ ```
24
+
25
+ ### Authentication
26
+
27
+ The API requires authentication, so you will need an account at [archive.org](https://archive.org). There are two methods of authentication; cookies and API key. Presently only the latter is implemented. API keys may be generated at https://archive.org/account/s3.php. Ensure your access key and secret key are set in environment variables SPN2_ACCESS_KEY and SPN2_SECRET_KEY respectively.
28
+
29
+ ### Save a page
30
+
31
+ Save a url in the Wayback Machine. This method returns the job_id in a hash.
32
+ ```rb
33
+ > Spn2.save(url: 'example.com') # returns a job_id
34
+
35
+ => {job_id: 'spn2-9c17e047f58f9220a7008d4f18152fee4d111d14'}
36
+ ```
37
+
38
+ ### View the status of a job
39
+
40
+ Use the job_id.
41
+ ```rb
42
+ > Spn2.status(job_id: 'spn2-9c17e047f58f9220a7008d4f18152fee4d111d14')
43
+
44
+ => {"counters"=>{"outlinks"=>1, "embeds"=>2}, "job_id"=>"spn2-9c17e047f58f9220a7008d4f18152fee4d111d14",
45
+ "original_url"=>"http://example.com/", "resources"=>["http://example.com/", "http://example.com/favicon.ico"],
46
+ "duration_sec"=>6.732, "outlinks"=>["https://www.iana.org/domains/example"], "http_status"=>200,
47
+ "timestamp"=>"20220622224107", "status"=>"success"}
48
+ ```
49
+ "status" => "success" is what you are looking for.
50
+
51
+ ### System status
52
+
53
+ The status of Wayback Machine itself is available.
54
+ ```rb
55
+ > Spn2.system_status
56
+ => {"status"=>"ok"}
57
+ ```
58
+
59
+ ## Development
60
+
61
+ ~~After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.~~
62
+
63
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitLab at https://gitlab.com/matzfan/spn2. Please run `rubocop` and correct all errors before submitting PR's.
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/test_*.rb']
10
+ end
11
+
12
+ require 'rubocop/rake_task'
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/lib/curlable.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'curb'
4
+
5
+ # namespace
6
+ module Curlable
7
+ def get(url:, headers: {})
8
+ Curl::Easy.http_get(url) do |http|
9
+ http.follow_location = true
10
+ headers.each { |k, v| http.headers[k] = v }
11
+ end.body_str
12
+ end
13
+
14
+ def post(url:, headers: {}, params: {})
15
+ Curl::Easy.http_post("#{url}?#{Curl.postalize(params)}", params) do |http|
16
+ http.follow_location = true
17
+ headers.each { |k, v| http.headers[k] = v }
18
+ end.body_str
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spn2
4
+ VERSION = '0.1.0'
5
+ end
data/lib/spn2.rb ADDED
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'nokogiri'
5
+
6
+ require_relative 'curlable'
7
+
8
+ # namespace
9
+ module Spn2
10
+ extend Curlable # for .system_status
11
+ include Curlable
12
+
13
+ class Spn2Error < StandardError; end
14
+
15
+ FIND_JOB_ID_REGEXP = /(spn2-([a-f]|\d){40})/
16
+ WEB_ARCHIVE = 'https://web.archive.org'
17
+
18
+ def self.access_key
19
+ ENV.fetch('SPN2_ACCESS_KEY', nil)
20
+ end
21
+
22
+ def self.secret_key
23
+ ENV.fetch('SPN2_SECRET_KEY', nil)
24
+ end
25
+
26
+ def self.system_status
27
+ JSON.parse get(url: "#{WEB_ARCHIVE}/save/status/system")
28
+ end
29
+
30
+ def self.save(url:)
31
+ job_id(json(auth_post(url: "#{WEB_ARCHIVE}/save/#{url}", params: "url=#{url}")))
32
+ end
33
+
34
+ def self.status(job_id:)
35
+ json auth_get(url: "#{WEB_ARCHIVE}/save/status/#{job_id}?_t=#{Time.now.to_i}")
36
+ end
37
+
38
+ def self.auth_get(url:)
39
+ get(url: url, headers: accept_header.merge(auth_header))
40
+ end
41
+
42
+ def self.auth_post(url:, params:)
43
+ post(url: url, headers: accept_header.merge(auth_header), params:)
44
+ end
45
+
46
+ def self.accept_header
47
+ { 'Accept' => 'application/json' }
48
+ end
49
+
50
+ def self.auth_header
51
+ { 'Authorization' => "LOW #{Spn2.access_key}:#{Spn2.secret_key}" }
52
+ end
53
+
54
+ def self.job_id(hash)
55
+ { job_id: hash['job_id'] }
56
+ end
57
+
58
+ def self.json(json_string)
59
+ JSON.parse Nokogiri::HTML(json_string)
60
+ end
61
+ end
data/sig/spn2.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Spn2
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spn2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - MatzFan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: curb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.18'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.18'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.16'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '13.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '13.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.30'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.30'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-minitest
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.20'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.20'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.6'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.6'
139
+ description: Atomate the process of saving web pages to archive.org
140
+ email:
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files: []
144
+ files:
145
+ - ".rubocop.yml"
146
+ - CHANGELOG.md
147
+ - Gemfile
148
+ - Guardfile
149
+ - LICENSE.txt
150
+ - README.md
151
+ - Rakefile
152
+ - lib/curlable.rb
153
+ - lib/spn2.rb
154
+ - lib/spn2/version.rb
155
+ - sig/spn2.rbs
156
+ homepage: https://gitlab.com/matxfan/spn2
157
+ licenses:
158
+ - MIT
159
+ metadata:
160
+ allowed_push_host: https://rubygems.org
161
+ homepage_uri: https://gitlab.com/matzfan/spn2
162
+ source_code_uri: https://gitlab.com/matzfan/spn2
163
+ changelog_uri: https://gitlab.com/matzfan/spn2/CHANGELOG.md
164
+ rubygems_mfa_required: 'true'
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '3.1'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubygems_version: 3.3.16
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Gem for the Save Page Now API of the Wayback Machine
184
+ test_files: []