scholarsphere-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 683f69f3cc0b3710f0b00fd18fbafc05efe11c54f7399a277665403d6b8fb10a
4
+ data.tar.gz: cccc924aa267339669331de7d92fb45fb1bc446fb29740ebc87c298041ba3b19
5
+ SHA512:
6
+ metadata.gz: dc35ef489babe5c2d303325b4ae9f7d818a3394eaaffb1574433db957834b88db149ca19c627d666a567f9752c2822d29b67be9a00db680b339cca97262d1b32
7
+ data.tar.gz: 22d55ab65a3a090b927163fdaa4ad22e4334fcf720ce6a4cd86b02e52088bea64c5c6702d34f6c8ca443164ba2fe5ae5a788dba1d26565c26fd95bc1c78da7bf
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # Ignore application configuration
14
+ /config/scholarsphere-client.yml
15
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,9 @@
1
+ inherit_gem:
2
+ niftany:
3
+ - niftany_rubocop_rspec.yml
4
+ - niftany_rubocop_ruby.yml
5
+
6
+ Metrics/BlockLength:
7
+ Exclude:
8
+ - 'spec/**/*'
9
+ - 'scholarsphere-client.gemspec'
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in scholarsphere-client.gemspec
6
+ gemspec
@@ -0,0 +1,23 @@
1
+ # Scholarsphere::Client
2
+
3
+ Ruby client to update and create content in the Scholarsphere repository.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'scholarsphere-client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install scholarsphere-client
20
+
21
+ ## Usage
22
+
23
+ This client is in development and currently not supporting any features.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'scholarsphere/client'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
File without changes
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'psych'
4
+ require 'marcel'
5
+ require 'faraday'
6
+ require 'scholarsphere/s3'
7
+ require 'scholarsphere/client/config'
8
+ require 'scholarsphere/client/ingest'
9
+ require 'scholarsphere/client/collection'
10
+ require 'scholarsphere/client/version'
11
+
12
+ module Scholarsphere
13
+ module Client
14
+ class Error < StandardError; end
15
+
16
+ Config.load_defaults
17
+
18
+ class << self
19
+ def connection
20
+ @connection ||= Faraday::Connection.new(
21
+ url: ENV['SS4_ENDPOINT'],
22
+ headers: {
23
+ 'Content-Type' => 'application/json',
24
+ 'X-API-KEY' => api_key
25
+ },
26
+ ssl: { verify: verify_ssl? }
27
+ )
28
+ end
29
+
30
+ def verify_ssl?
31
+ ENV['SS_CLIENT_SSL'] != 'false'
32
+ end
33
+
34
+ def api_key
35
+ ENV['SS_CLIENT_KEY']
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scholarsphere
4
+ module Client
5
+ class Collection
6
+ attr_reader :metadata, :depositor, :permissions, :work_noids
7
+
8
+ # @param [Hash] metadata
9
+ # @param [String] depositor
10
+ # @param optional [Hash] permissions
11
+ # @param optional [Array] work_noids
12
+ def initialize(metadata:, depositor:, permissions: {}, work_noids: [])
13
+ @metadata = metadata
14
+ @depositor = depositor
15
+ @permissions = permissions
16
+ @work_noids = work_noids
17
+ end
18
+
19
+ def create
20
+ connection.post do |req|
21
+ req.url 'collections'
22
+ req.body = {
23
+ metadata: metadata,
24
+ depositor: depositor,
25
+ permissions: permissions,
26
+ work_noids: work_noids
27
+ }.to_json
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def connection
34
+ Scholarsphere::Client.connection
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scholarsphere
4
+ module Client
5
+ class Config
6
+ def self.load_defaults
7
+ new.load_config
8
+ end
9
+
10
+ attr_reader :file
11
+
12
+ def initialize(file = Pathname.pwd.join('config', 'scholarsphere-client.yml'))
13
+ @file = file
14
+ load_config
15
+ end
16
+
17
+ def load_config
18
+ return unless file.exist?
19
+
20
+ Psych.safe_load(file.read).each do |key, value|
21
+ ENV[key] ||= value
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scholarsphere
4
+ module Client
5
+ class Ingest
6
+ attr_reader :content, :metadata, :depositor, :permissions
7
+
8
+ # @param [Hash] metadata
9
+ # @param [Array<File,IO,Pathnme,Hash>] files as an array of File or IO, or a hash with a :file param
10
+ # @param [String] depositor
11
+ # @param optional [Hash] permissions
12
+ def initialize(metadata:, files:, depositor:, permissions: {})
13
+ @content = build_content_hash(files)
14
+ @metadata = metadata
15
+ @depositor = depositor
16
+ @permissions = permissions
17
+ end
18
+
19
+ def publish
20
+ upload_files
21
+ connection.post do |req|
22
+ req.url 'ingest'
23
+ req.body = { metadata: metadata, content: content, depositor: depositor, permissions: permissions }.to_json
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def build_content_hash(files)
30
+ files.map do |file|
31
+ if file.is_a?(Hash)
32
+ file.merge(file: S3::UploadedFile.new(file.fetch(:file)))
33
+ else
34
+ { file: S3::UploadedFile.new(file) }
35
+ end
36
+ end
37
+ end
38
+
39
+ def upload_files
40
+ content.map do |file_parameters|
41
+ uploader.upload(file_parameters.fetch(:file))
42
+ file_parameters[:file] = file_parameters[:file].to_shrine.to_json
43
+ end
44
+ end
45
+
46
+ def uploader
47
+ @uploader ||= S3::Uploader.new
48
+ end
49
+
50
+ def connection
51
+ Scholarsphere::Client.connection
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scholarsphere
4
+ module Client
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-s3'
4
+
5
+ module Scholarsphere
6
+ module S3
7
+ require_relative 's3/uploader'
8
+ require_relative 's3/uploaded_file'
9
+ end
10
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scholarsphere
4
+ module S3
5
+ class UploadedFile
6
+ attr_reader :source, :checksum
7
+
8
+ # @param [Pathname]
9
+ # @option option [String] :checksum in md5 format
10
+ def initialize(source, options = {})
11
+ @source = source
12
+ @checksum = options[:checksum]
13
+ end
14
+
15
+ # @return [Hash]
16
+ # @note this can be passed to a controller for uploading the file to Shrine
17
+ def to_shrine
18
+ {
19
+ id: id,
20
+ storage: 'cache',
21
+ metadata: metadata
22
+ }
23
+ end
24
+
25
+ # @return [String]
26
+ # @note This is the name of the file that will be stored in S3. It's using the same procedure that the
27
+ # Uppy::S3Multipart gem is using.
28
+ def id
29
+ @id ||= "#{SecureRandom.uuid}#{source.extname}"
30
+ end
31
+
32
+ # @return [String]
33
+ # @note Path of the file relative to the bucket
34
+ def key
35
+ "#{prefix}/#{id}"
36
+ end
37
+
38
+ # @return [String]
39
+ # @note When sending the md5 checksum to verify the file's integrity, Amazon requires that the value of the
40
+ # checksum be base64 encoded.
41
+ # @example The equivalent operation in bash would be:
42
+ # openssl dgst -md5 -binary file.jpg | openssl enc -base64
43
+ def content_md5
44
+ @content_md5 ||= if checksum
45
+ Base64.encode64([checksum].pack('H*')).strip
46
+ else
47
+ Digest::MD5.base64digest(source.read)
48
+ end
49
+ end
50
+
51
+ def size
52
+ source.size
53
+ end
54
+
55
+ private
56
+
57
+ def metadata
58
+ {
59
+ size: source.size,
60
+ filename: source.basename.to_s,
61
+ mime_type: Marcel::MimeType.for(source)
62
+ }
63
+ end
64
+
65
+ def prefix
66
+ ENV['SHRINE_CACHE_PREFIX'] || 'cache'
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scholarsphere
4
+ module S3
5
+ class Uploader < ::Aws::S3::FileUploader
6
+ FIFTEEN_MEGABYTES = 15 * 1024 * 1024
7
+
8
+ # @param [Hash] options
9
+ # @option options [Client] :client
10
+ # @option options [Integer] :multipart_threshold (15728640)
11
+ def initialize(options = {})
12
+ @options = options
13
+ @options[:client] ||= ::Aws::S3::Client.new(client_defaults)
14
+ @client = options[:client]
15
+ @multipart_threshold = options[:multipart_threshold] || FIFTEEN_MEGABYTES
16
+ end
17
+
18
+ # @param [UploadedFile]
19
+ # @param [Hash] of additional options
20
+ # @options options [String] content_md5 a base64-encoded string representating the md5 checksum
21
+ # @return [void]
22
+ # @note The content_md5 hash cannot be used when doing a multipart upload.
23
+ def upload(uploaded_file, options = {})
24
+ options[:bucket] = ENV['AWS_BUCKET']
25
+ options[:key] = uploaded_file.key
26
+ if uploaded_file.size < multipart_threshold
27
+ options[:content_md5] = uploaded_file.content_md5
28
+ end
29
+ super(uploaded_file.source, options)
30
+ end
31
+
32
+ private
33
+
34
+ def client_defaults
35
+ {
36
+ endpoint: ENV['S3_ENDPOINT'],
37
+ access_key_id: ENV['AWS_ACCESS_KEY_ID'],
38
+ secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
39
+ force_path_style: true,
40
+ region: ENV['AWS_REGION']
41
+ }
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'scholarsphere/client/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'scholarsphere-client'
9
+ spec.version = Scholarsphere::Client::VERSION
10
+ spec.authors = ['Adam Wead']
11
+ spec.email = ['amsterdamos@gmail.com']
12
+
13
+ spec.summary = 'Client to connect to Scholarpshere'
14
+ spec.description = 'Client software to create new content for the Scholarsphere repository at Penn State.'
15
+ spec.homepage = 'https://github.com/psu-stewardship/scholarsphere-client'
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/psu-stewardship/scholarsphere-client'
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_dependency 'aws-sdk-s3', '~> 1.49'
33
+ spec.add_dependency 'faraday', '> 0.12'
34
+ spec.add_dependency 'marcel', '~> 0.3'
35
+
36
+ spec.add_development_dependency 'bundler', '~> 2.0'
37
+ spec.add_development_dependency 'niftany', '~> 0.6'
38
+ spec.add_development_dependency 'pry', '~> 0.12'
39
+ spec.add_development_dependency 'pry-byebug', '~> 3.9'
40
+ spec.add_development_dependency 'rake', '>= 12.3.3'
41
+ spec.add_development_dependency 'rspec', '~> 3.0'
42
+ spec.add_development_dependency 'rspec-its', '~> 1.3'
43
+ spec.add_development_dependency 'simplecov', '~> 0.18'
44
+ end
metadata ADDED
@@ -0,0 +1,218 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scholarsphere-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Wead
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-s3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.49'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.49'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.12'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: marcel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: niftany
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.6'
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.12'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.12'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.9'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 12.3.3
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 12.3.3
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec-its
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.3'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.3'
153
+ - !ruby/object:Gem::Dependency
154
+ name: simplecov
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.18'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.18'
167
+ description: Client software to create new content for the Scholarsphere repository
168
+ at Penn State.
169
+ email:
170
+ - amsterdamos@gmail.com
171
+ executables: []
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - ".rubocop.yml"
178
+ - Gemfile
179
+ - README.md
180
+ - Rakefile
181
+ - bin/console
182
+ - bin/setup
183
+ - config/.gitkeep
184
+ - lib/scholarsphere/client.rb
185
+ - lib/scholarsphere/client/collection.rb
186
+ - lib/scholarsphere/client/config.rb
187
+ - lib/scholarsphere/client/ingest.rb
188
+ - lib/scholarsphere/client/version.rb
189
+ - lib/scholarsphere/s3.rb
190
+ - lib/scholarsphere/s3/uploaded_file.rb
191
+ - lib/scholarsphere/s3/uploader.rb
192
+ - scholarsphere-client.gemspec
193
+ homepage: https://github.com/psu-stewardship/scholarsphere-client
194
+ licenses: []
195
+ metadata:
196
+ allowed_push_host: https://rubygems.org
197
+ homepage_uri: https://github.com/psu-stewardship/scholarsphere-client
198
+ source_code_uri: https://github.com/psu-stewardship/scholarsphere-client
199
+ post_install_message:
200
+ rdoc_options: []
201
+ require_paths:
202
+ - lib
203
+ required_ruby_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ required_rubygems_version: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '0'
213
+ requirements: []
214
+ rubygems_version: 3.1.2
215
+ signing_key:
216
+ specification_version: 4
217
+ summary: Client to connect to Scholarpshere
218
+ test_files: []