box_active_storage 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: 56950dc6a327d09c817d1db342c167313140bdef613fb5d79ddeb2a30148ff43
4
+ data.tar.gz: f93aebbe4a1fc38c920afa097551fbc4bf10533a471d4cba6421e0edec8c4f0b
5
+ SHA512:
6
+ metadata.gz: 35a1eeee26b0d34ee6c18d5fdfd141b9ab28ce0551ae369a80d70b47524de9c88de633487b824b46f76bf75aabf61395585fddc9de51e217c81b75bd7742c9fd
7
+ data.tar.gz: 4d7f6442c833b63d8a509d54cc8f7645f0e35846cc0b7e861eee7ef26655f4e25a4aa9e235a5a6a3437f1da4cb1626169dffa615b1008759649e11e1a65fa285
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,55 @@
1
+ # require:
2
+ # - rubocop-rails
3
+
4
+ AllCops:
5
+ NewCops: enable
6
+
7
+ Style/TrailingCommaInArrayLiteral:
8
+ Enabled: false
9
+
10
+ Style/TrailingCommaInHashLiteral:
11
+ Enabled: false
12
+
13
+ Style/TrailingCommaInArguments:
14
+ Enabled: false
15
+
16
+ Style/FormatStringToken:
17
+ Enabled: false
18
+
19
+ Metrics/BlockLength:
20
+ Max: 30
21
+ Exclude:
22
+ - spec/**/*.rb
23
+ - Guardfile
24
+
25
+ Style/Documentation:
26
+ Enabled: false
27
+
28
+ Layout/LineLength:
29
+ Max: 200
30
+
31
+ Metrics/MethodLength:
32
+ Max: 20
33
+
34
+ Rails/FilePath:
35
+ EnforcedStyle: arguments
36
+
37
+ Metrics/AbcSize:
38
+ Enabled: false
39
+
40
+ Metrics/ClassLength:
41
+ Max: 140
42
+
43
+ Layout/EndAlignment:
44
+ EnforcedStyleAlignWith: variable
45
+ Enabled: true
46
+
47
+ Layout/MultilineMethodCallIndentation:
48
+ EnforcedStyle: indented
49
+ Enabled: true
50
+
51
+ Rails/SkipsModelValidations:
52
+ Enabled: false
53
+
54
+ Metrics/ParameterLists:
55
+ Max: 12
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # BoxActiveStorage
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/box_active_storage`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ 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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/box_active_storage.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/box_active_storage/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "box_active_storage"
7
+ spec.version = BoxActiveStorage::VERSION
8
+ spec.authors = ["JoelGamer"]
9
+ spec.email = ["gtheodoro@rescocompany.com"]
10
+
11
+ spec.summary = "This Gem is for integrating Box.com with ActiveStorage."
12
+ # spec.description = "TODO: Write a longer description or delete this line."
13
+ spec.homepage = "https://github.com/RESCO-Company/box_active_storage"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/RESCO-Company/box_active_storage"
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(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) ||
28
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ # Uncomment to register a new dependency of your gem
36
+ spec.add_dependency "httparty", '~> 0.22'
37
+
38
+ # For more information and examples about making a new gem, check out our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ end
@@ -0,0 +1,33 @@
1
+ module ActiveStorage
2
+ class Service::BoxService < Service
3
+ def initialize(folder:, client_id:, client_secret:, grant_type:, box_subject_type:, box_subject_id:)
4
+ @multipart_upload_threshold = 50.megabytes
5
+
6
+ @folder = folder
7
+ @client_id = client_id
8
+ @client_secret = client_secret
9
+ @grant_type = grant_type
10
+ @box_subject_type = box_subject_type
11
+ @box_subject_id = box_subject_id
12
+ end
13
+
14
+ def upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}, **)
15
+ token = BoxActiveStorage::Token.token(client_id: @client_id, client_secret: @client_secret, grant_type: @grant_type, box_subject_type: @box_subject_type, box_subject_id: @box_subject_id)
16
+
17
+ instrument :upload, key: key, checksum: checksum, filename: filename do
18
+ response = BoxActiveStorage::File.upload(token: token, folder: @folder, io: io, filename: filename&.sanitized || File.basename(io))
19
+ ActiveStorage::Blob.find_by(key: key).update!(key: response['entries'][0]['id'])
20
+
21
+ response
22
+ end
23
+ end
24
+
25
+ def download(key, &block)
26
+ token = BoxActiveStorage::Token.token(client_id: @client_id, client_secret: @client_secret, grant_type: @grant_type, box_subject_type: @box_subject_type, box_subject_id: @box_subject_id)
27
+
28
+ instrument :download, key: key do
29
+ BoxActiveStorage::File.download(token: token, id: key)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,44 @@
1
+ module BoxActiveStorage
2
+ class File
3
+ class NotReadyError < StandardError; end
4
+ class ClientError < StandardError; end
5
+ class NotFoundError < StandardError; end
6
+ class DiskSpaceFullError < StandardError; end
7
+
8
+ def download(token:, id:)
9
+ response = ::HTTParty.get("https://api.box.com/2.0/files/#{id}/content",
10
+ headers: { 'Authorization': token },
11
+ )
12
+
13
+ if response.code != 200
14
+ raise NotReadyError.new(response.body) if response.code == 202
15
+ raise StandardError.new(response.body)
16
+ end
17
+
18
+ response.body.force_encoding(Encoding::BINARY)
19
+ end
20
+
21
+ def upload(token:, folder:, io:, filename:)
22
+ response = ::HTTParty.post('https://upload.box.com/api/2.0/files/content',
23
+ multipart: true,
24
+ headers: { 'Authorization': token },
25
+ body: {
26
+ attributes: JSON.generate({ name: filename, parent: { id: folder } }),
27
+ file: io
28
+ }
29
+ )
30
+
31
+ if response.code != 201
32
+ raise ClientError.new(response.body) if response.code == 400
33
+ raise NotFoundError.new(response.body) if response.code == 404
34
+ raise DiskSpaceFullError.new(response.body) if response.code == 409
35
+ raise StandardError.new(response.body)
36
+ end
37
+
38
+ JSON.parse!(response.body)
39
+ end
40
+
41
+ def self.download(...) = new.download(...)
42
+ def self.upload(...) = new.upload(...)
43
+ end
44
+ end
@@ -0,0 +1,68 @@
1
+ class BoxActiveStorage::Folder
2
+ class ClientError < StandardError; end
3
+ class UnauthorizedError < StandardError; end
4
+ class NotFoundError < StandardError; end
5
+ class UnrecognizedError < StandardError; end
6
+ class BlockedError < StandardError; end
7
+
8
+ def information(id:)
9
+ response = ::HTTParty.get("https://api.box.com/2.0/folders/#{id}",
10
+ headers: { 'Authorization': @token },
11
+ )
12
+
13
+ if response.code != 200
14
+ raise UnauthorizedError.new(response.body) if response.code == 403
15
+ raise NotFoundError.new(response.body) if response.code == 404
16
+ raise UnrecognizedError.new(response.body) if response.code == 405
17
+ raise StandardError.new(response.body)
18
+ end
19
+
20
+ JSON.parse!(response.body)
21
+ end
22
+
23
+ def create(name:, parent: '0')
24
+ response = ::HTTParty.post("https://api.box.com/2.0/folders",
25
+ headers: { 'Authorization': @token },
26
+ body: {
27
+ name: name,
28
+ parent: { id: parent }
29
+ }
30
+ )
31
+
32
+ if response.code != 201
33
+ raise ClientError.new(response.body) if response.code == 400
34
+ raise UnauthorizedError.new(response.body) if response.code == 403
35
+ raise NotFoundError.new(response.body) if response.code == 404
36
+ raise BlockedError.new(response.body) if response.code == 409
37
+ raise StandardError.new(response.body)
38
+ end
39
+
40
+ JSON.parse!(response.body)
41
+ end
42
+
43
+ def update(id:, name: nil, parent: nil)
44
+ body = {}
45
+
46
+ body = body.merge({ name: name }) if name.present?
47
+ body = body.merge({ attributes: JSON.generate({ parent: { id: parent } }) }) if parent.present?
48
+
49
+ response = ::HTTParty.put("https://api.box.com/2.0/folders",
50
+ headers: { 'Authorization': @token },
51
+ body: body
52
+ )
53
+
54
+ if response.code != 201
55
+ raise ClientError.new(response.body) if response.code == 400
56
+ raise UnauthorizedError.new(response.body) if response.code == 403
57
+ raise NotFoundError.new(response.body) if response.code == 404
58
+ raise BlockedError.new(response.body) if response.code == 409
59
+ raise StandardError.new(response.body)
60
+ end
61
+
62
+ JSON.parse!(response.body)
63
+ end
64
+
65
+ def self.information(...) = new.information(...)
66
+ def self.create(...) = new.create(...)
67
+ def self.update(...) = new.update(...)
68
+ end
@@ -0,0 +1,40 @@
1
+ module BoxActiveStorage::Token
2
+ def self.token(client_id:, client_secret:, grant_type:, box_subject_type:, box_subject_id:)
3
+ @client_id = client_id
4
+ @client_secret = client_secret
5
+ @grant_type = grant_type
6
+ @box_subject_type = box_subject_type
7
+ @box_subject_id = box_subject_id
8
+
9
+ self.reauth_token
10
+ @token
11
+ end
12
+
13
+ private
14
+
15
+ def self.oauth_token
16
+ response = ::HTTParty.post('https://api.box.com/oauth2/token',
17
+ headers: { 'Content-Type' => 'application/x-www-form-urlencoded' },
18
+ body: URI.encode_www_form({
19
+ client_id: @client_id,
20
+ client_secret: @client_secret,
21
+ grant_type: @grant_type,
22
+ box_subject_type: @box_subject_type,
23
+ box_subject_id: @box_subject_id
24
+ })
25
+ )
26
+
27
+ raise StandardError(response) unless response.code == 200
28
+
29
+ parsed_response = JSON.parse!(response.body)
30
+ refresh_in = Time.now + parsed_response['expires_in'].seconds
31
+
32
+ ["Bearer #{parsed_response['access_token']}", refresh_in]
33
+ end
34
+
35
+ def self.reauth_token
36
+ @token, @refresh_in = oauth_token if @refresh_in.blank? || @refresh_in < Time.now
37
+ end
38
+
39
+ attr_reader :token, :refresh_in
40
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BoxActiveStorage
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "box_active_storage/file"
4
+ require_relative "box_active_storage/folder"
5
+ require_relative "box_active_storage/token"
6
+ require_relative "box_active_storage/version"
7
+
8
+ module BoxActiveStorage
9
+ end
@@ -0,0 +1,4 @@
1
+ module BoxActiveStorage
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: box_active_storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - JoelGamer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.22'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.22'
27
+ description:
28
+ email:
29
+ - gtheodoro@rescocompany.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - README.md
37
+ - Rakefile
38
+ - box_active_storage.gemspec
39
+ - lib/active_storage/service/box_service.rb
40
+ - lib/box_active_storage.rb
41
+ - lib/box_active_storage/file.rb
42
+ - lib/box_active_storage/folder.rb
43
+ - lib/box_active_storage/token.rb
44
+ - lib/box_active_storage/version.rb
45
+ - sig/box_active_storage.rbs
46
+ homepage: https://github.com/RESCO-Company/box_active_storage
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/RESCO-Company/box_active_storage
51
+ source_code_uri: https://github.com/RESCO-Company/box_active_storage
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.6.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.5.3
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: This Gem is for integrating Box.com with ActiveStorage.
71
+ test_files: []