boxr 1.22.0 → 1.23.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 +4 -4
- data/.github/workflows/ruby-tests.yml +52 -0
- data/.gitignore +1 -0
- data/.rspec +0 -1
- data/.rubocop.yml +22 -0
- data/README.md +34 -5
- data/Rakefile +3 -4
- data/bin/boxr +5 -2
- data/boxr.gemspec +30 -25
- data/examples/enterprise_events.rb +15 -11
- data/examples/jwt_auth.rb +6 -5
- data/examples/metadata_search.rb +12 -11
- data/examples/oauth.rb +8 -11
- data/examples/remove_collaborators.rb +6 -5
- data/examples/use_events_to_send_sms.rb +35 -34
- data/examples/use_events_to_warn_about_sharing.rb +35 -31
- data/examples/user_events.rb +8 -7
- data/lib/boxr/auth.rb +41 -36
- data/lib/boxr/chunked_uploads.rb +56 -32
- data/lib/boxr/client.rb +117 -102
- data/lib/boxr/collaborations.rb +21 -14
- data/lib/boxr/collections.rb +5 -5
- data/lib/boxr/comments.rb +13 -13
- data/lib/boxr/errors.rb +25 -21
- data/lib/boxr/files.rb +87 -73
- data/lib/boxr/folders.rb +32 -29
- data/lib/boxr/groups.rb +22 -22
- data/lib/boxr/metadata.rb +23 -22
- data/lib/boxr/search.rb +34 -28
- data/lib/boxr/shared_items.rb +7 -5
- data/lib/boxr/tasks.rb +18 -18
- data/lib/boxr/users.rb +63 -38
- data/lib/boxr/version.rb +3 -1
- data/lib/boxr/watermarking.rb +10 -17
- data/lib/boxr/web_links.rb +14 -17
- data/lib/boxr/webhooks.rb +8 -7
- data/lib/boxr.rb +13 -11
- metadata +86 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6fe53be6f1ffc2011088a31f8c40e6ed6e574f948ea6d08f634742b65633fcb1
|
4
|
+
data.tar.gz: aee3efc2e1b573c15de8af52d410f3c93f39d5bf897692c3992a502c9ea15c3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 623fff67ad55230bf68b62a24e318a7ca9464f43a9f5745cce620f6d47ecf7c873909eb5970bc8c47fedfe6ecaa963138d0176159294c5acf9b5afe18dc6345d
|
7
|
+
data.tar.gz: 905a779b88f4fc7aee66026497e0afc2d7f0530925a614473459b06cc2d62d713ab6b50e9d1cdf168a73f6fe6e0dd8e026137a6bea0082d82af2952724113041
|
@@ -0,0 +1,52 @@
|
|
1
|
+
name: Ruby Tests
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ main, master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ main, master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
|
13
|
+
strategy:
|
14
|
+
matrix:
|
15
|
+
ruby-version: ['2.7', '3.0', '3.1', '3.2']
|
16
|
+
|
17
|
+
steps:
|
18
|
+
- uses: actions/checkout@v4
|
19
|
+
|
20
|
+
- name: Set up Ruby
|
21
|
+
uses: ruby/setup-ruby@v1
|
22
|
+
with:
|
23
|
+
ruby-version: ${{ matrix.ruby-version }}
|
24
|
+
bundler-cache: true
|
25
|
+
bundler: '2.4.22'
|
26
|
+
|
27
|
+
- name: Run unit tests
|
28
|
+
run: bundle exec rspec spec/unit/
|
29
|
+
env:
|
30
|
+
BOX_DEVELOPER_TOKEN: test_token
|
31
|
+
|
32
|
+
test_bundler_2_5:
|
33
|
+
runs-on: ubuntu-latest
|
34
|
+
|
35
|
+
strategy:
|
36
|
+
matrix:
|
37
|
+
ruby-version: [ '3.3', '3.4']
|
38
|
+
|
39
|
+
steps:
|
40
|
+
- uses: actions/checkout@v4
|
41
|
+
|
42
|
+
- name: Set up Ruby
|
43
|
+
uses: ruby/setup-ruby@v1
|
44
|
+
with:
|
45
|
+
ruby-version: ${{ matrix.ruby-version }}
|
46
|
+
bundler-cache: true
|
47
|
+
bundler: '2.5.23'
|
48
|
+
|
49
|
+
- name: Run unit tests
|
50
|
+
run: bundle exec rspec spec/unit/
|
51
|
+
env:
|
52
|
+
BOX_DEVELOPER_TOKEN: test_token
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.7
|
3
|
+
|
4
|
+
plugins:
|
5
|
+
- rubocop-rspec
|
6
|
+
- rubocop-rake
|
7
|
+
|
8
|
+
Layout/LineLength:
|
9
|
+
Max: 100
|
10
|
+
|
11
|
+
# Due to legacy test format
|
12
|
+
RSpec/SpecFilePathFormat:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
RSpec/MultipleMemoizedHelpers:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
RSpec/MultipleExpectations:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Style/Documentation:
|
22
|
+
Enabled: false
|
data/README.md
CHANGED
@@ -13,7 +13,12 @@ The full RubyDocs for Boxr can be found [here](http://www.rubydoc.info/gems/boxr
|
|
13
13
|
Boxr follows Semantic Versioning since version 1.5.0
|
14
14
|
|
15
15
|
## Requirements
|
16
|
-
This gem requires Ruby
|
16
|
+
This gem requires a Ruby version depending on the version of the gem you are using. We aim to deprecate support for EOL Ruby versions.
|
17
|
+
|
18
|
+
- For version 1.23.0 and higher, this gem requires Ruby 2.7 or higher.
|
19
|
+
- For version 1.22.0 and lower, this gem requires Ruby 2.0.0 or higher.
|
20
|
+
|
21
|
+
|
17
22
|
|
18
23
|
## Installation
|
19
24
|
Add this line to your application's Gemfile:
|
@@ -99,7 +104,7 @@ client = Boxr::Client.new('zX3UjFwNerOy5PSWc2WI8aJgMHtAjs8T',
|
|
99
104
|
|
100
105
|
# You can provide another parameter called as_user. Read about what that means here: https://developer.box.com/reference#as-user-1
|
101
106
|
|
102
|
-
# You can provide
|
107
|
+
# You can provide another parameter called identifier. This can be used, for example, to
|
103
108
|
# hold the id of the user associated with this Boxr client. When the callback is invoked this value
|
104
109
|
# will be provided.
|
105
110
|
```
|
@@ -115,6 +120,7 @@ initialize( access_token=ENV['BOX_DEVELOPER_TOKEN'],
|
|
115
120
|
jwt_public_key_id: ENV['JWT_PUBLIC_KEY_ID'],
|
116
121
|
identifier: nil,
|
117
122
|
as_user: nil,
|
123
|
+
proxy: nil,
|
118
124
|
&token_refresh_listener)
|
119
125
|
```
|
120
126
|
|
@@ -132,6 +138,14 @@ updated_file = client.create_shared_link_for_file(file, access: :open)
|
|
132
138
|
puts "Shared Link: #{updated_file.shared_link.url}"
|
133
139
|
```
|
134
140
|
|
141
|
+
### Using a proxy
|
142
|
+
You can provide a proxy to Boxr::Client.new by passing the proxy parameter.
|
143
|
+
```ruby
|
144
|
+
client = Boxr::Client.new(proxy: 'http://username:password@proxy.example.com:8080')
|
145
|
+
```
|
146
|
+
Boxr uses HTTPClient under the hood, so you can use all the features of HTTPClient's proxy support.
|
147
|
+
See https://www.rubydoc.info/gems/httpclient/HTTPClient#proxy=-instance_method for more details.
|
148
|
+
|
135
149
|
### NOTE: Using HTTP mocking libraries for testing
|
136
150
|
When using HTTP mocking libraries for testing, you may need to set Boxr::BOX_CLIENT to a fresh instance of HTTPClient in your test setup after loading the HTTP mocking library. For example, when using WebMock with RSpec you might could add the following to your RSpec configuration:
|
137
151
|
``` ruby
|
@@ -240,7 +254,7 @@ promote_old_version_of_file(file, file_version)
|
|
240
254
|
|
241
255
|
delete_old_version_of_file(file, file_version, if_match: nil)
|
242
256
|
|
243
|
-
copy_file(file, parent, name: nil)
|
257
|
+
copy_file(file, parent, name: nil, version: nil)
|
244
258
|
|
245
259
|
thumbnail(file, min_height: nil, min_width: nil, max_height: nil, max_width: nil)
|
246
260
|
|
@@ -319,7 +333,9 @@ delete_comment(comment)
|
|
319
333
|
```
|
320
334
|
#### [Collaborations](https://developer.box.com/en/reference/resources/collaboration/)
|
321
335
|
```ruby
|
322
|
-
folder_collaborations(folder)
|
336
|
+
folder_collaborations(folder, fields: [], limit: DEFAULT_LIMIT, marker: nil)
|
337
|
+
|
338
|
+
file_collaborations(file, fields: [], limit: DEFAULT_LIMIT, marker: nil)
|
323
339
|
|
324
340
|
add_collaboration(folder, accessible_by, role, fields: [], notify: nil)
|
325
341
|
|
@@ -346,7 +362,7 @@ shared_item(shared_link, shared_link_password: nil)
|
|
346
362
|
```
|
347
363
|
#### [Search](https://developer.box.com/en/reference/get-search/)
|
348
364
|
```ruby
|
349
|
-
search( query=nil, scope: nil, file_extensions: [],
|
365
|
+
search( query=nil, scope: nil, file_extensions: [], fields: [],
|
350
366
|
created_at_range_from_date: nil, created_at_range_to_date: nil,
|
351
367
|
updated_at_range_from_date: nil, updated_at_range_to_date: nil,
|
352
368
|
size_range_lower_bound_bytes: nil, size_range_upper_bound_bytes: nil,
|
@@ -515,3 +531,16 @@ create_zip_download(items, download_file_name: nil)
|
|
515
531
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
516
532
|
4. Push to the branch (`git push origin my-new-feature`)
|
517
533
|
5. Create a new Pull Request
|
534
|
+
|
535
|
+
## Test Commands
|
536
|
+
|
537
|
+
```
|
538
|
+
rspec spec/files_spec.rb
|
539
|
+
rspec spec/folders_spec.rb
|
540
|
+
rspec spec/groups_spec.rb
|
541
|
+
rspec spec/tasks_spec.rb
|
542
|
+
rspec spec/users_spec.rb
|
543
|
+
rspec spec/watermarking_spec.rb
|
544
|
+
rspec spec/webhooks_spec.rb
|
545
|
+
rspec spec/zip_downloads_spec.rb
|
546
|
+
```
|
data/Rakefile
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
require 'rspec/core/rake_task'
|
2
|
-
require
|
2
|
+
require 'bundler/gem_tasks'
|
3
3
|
|
4
|
-
# Default directory to look in is `/specs`
|
5
4
|
# Run with `rake spec`
|
6
5
|
RSpec::Core::RakeTask.new(:spec) do |task|
|
7
|
-
task.rspec_opts = ['--
|
6
|
+
task.rspec_opts = ['--format', 'documentation']
|
8
7
|
end
|
9
8
|
|
10
|
-
task :
|
9
|
+
task default: :spec
|
data/bin/boxr
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require 'boxr'
|
4
|
-
require 'dotenv'
|
4
|
+
require 'dotenv'
|
5
|
+
Dotenv.load
|
6
|
+
|
5
7
|
require 'awesome_print'
|
6
8
|
require 'irb'
|
7
9
|
|
8
|
-
$boxr = Boxr::Client.new #uses ENV['BOX_DEVELOPER_TOKEN']
|
10
|
+
$boxr = Boxr::Client.new # uses ENV['BOX_DEVELOPER_TOKEN']
|
11
|
+
|
9
12
|
IRB.start
|
data/boxr.gemspec
CHANGED
@@ -1,37 +1,42 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require 'boxr/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
6
|
+
spec.name = 'boxr'
|
8
7
|
spec.version = Boxr::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
8
|
+
spec.authors = ['Chad Burnette', 'Xavier Hocquet']
|
9
|
+
spec.email = ['chadburnette@me.com', 'xhocquet@gmail.com']
|
10
|
+
spec.summary = 'A Ruby client library for the Box V2 Content API.'
|
11
|
+
spec.description = ''
|
12
|
+
spec.homepage = 'https://github.com/cburnette/boxr'
|
13
|
+
spec.license = 'MIT'
|
15
14
|
|
16
|
-
spec.files = `git ls-files -z`.split("\x0").reject
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
17
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
20
|
+
spec.require_paths = ['lib']
|
20
21
|
|
21
|
-
spec.required_ruby_version = '>= 2.
|
22
|
+
spec.required_ruby_version = '>= 2.7'
|
22
23
|
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
27
|
-
spec.add_development_dependency
|
28
|
-
spec.add_development_dependency
|
29
|
-
spec.add_development_dependency
|
30
|
-
spec.add_development_dependency
|
31
|
-
spec.add_development_dependency
|
24
|
+
spec.add_development_dependency 'awesome_print', '~> 1.8'
|
25
|
+
spec.add_development_dependency 'bundler', '>= 2.4' # Keep this locked for Ruby 2.7 compatibility
|
26
|
+
spec.add_development_dependency 'dotenv', '~> 2.8'
|
27
|
+
spec.add_development_dependency 'parallel', '~> 1.0'
|
28
|
+
spec.add_development_dependency 'rake', '~> 13.1'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.13.1'
|
30
|
+
spec.add_development_dependency 'rubocop', '~> 1.0'
|
31
|
+
spec.add_development_dependency 'rubocop-rake', '~> 0.7.1'
|
32
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 3.7.0'
|
33
|
+
spec.add_development_dependency 'rubyzip', '~> 2.3'
|
34
|
+
spec.add_development_dependency 'simplecov', '~> 0.22'
|
35
|
+
spec.add_development_dependency 'sin_lru_redux', '~> 2.5'
|
36
|
+
spec.add_development_dependency 'webmock', '~> 3.19'
|
32
37
|
|
33
|
-
spec.add_runtime_dependency
|
34
|
-
spec.add_runtime_dependency
|
35
|
-
spec.add_runtime_dependency
|
36
|
-
spec.add_runtime_dependency
|
38
|
+
spec.add_runtime_dependency 'addressable', '~> 2.8'
|
39
|
+
spec.add_runtime_dependency 'hashie', '>= 3.5', '< 6'
|
40
|
+
spec.add_runtime_dependency 'httpclient', '~> 2.8'
|
41
|
+
spec.add_runtime_dependency 'jwt', '>= 1.4', '< 3'
|
37
42
|
end
|
@@ -1,24 +1,28 @@
|
|
1
|
-
require 'dotenv'
|
1
|
+
require 'dotenv'
|
2
|
+
Dotenv.load('../.env')
|
2
3
|
require 'boxr'
|
3
4
|
require 'awesome_print'
|
4
5
|
|
5
6
|
client = Boxr::Client.new(ENV['BOX_DEVELOPER_TOKEN'])
|
6
7
|
|
7
8
|
now = Time.now.utc
|
8
|
-
start_date = now - (60*60*24) #one day ago
|
9
|
+
start_date = now - (60 * 60 * 24) # one day ago
|
9
10
|
|
10
|
-
puts
|
11
|
+
puts 'fetching historic enterprise events...'
|
11
12
|
result = client.enterprise_events(created_after: start_date, created_before: now)
|
12
13
|
|
13
|
-
ap
|
14
|
-
|
14
|
+
ap(result.events.each do |event|
|
15
|
+
ap event
|
16
|
+
puts
|
17
|
+
end)
|
18
|
+
output = { count: result.events.count, next_stream_position: result.next_stream_position }
|
15
19
|
ap output
|
16
20
|
|
17
|
-
#now that we have the latest stream position let's start monitoring in real-time
|
18
|
-
puts
|
21
|
+
# now that we have the latest stream position let's start monitoring in real-time
|
22
|
+
puts 'listening for new enterprise events...'
|
19
23
|
client.enterprise_events_stream(result.next_stream_position, refresh_period: 5) do |result|
|
20
|
-
result.events.each{|e| ap e}
|
21
|
-
output={count: result.events.count, next_stream_position: result.next_stream_position}
|
24
|
+
result.events.each { |e| ap e }
|
25
|
+
output = { count: result.events.count, next_stream_position: result.next_stream_position }
|
22
26
|
ap output
|
23
|
-
puts
|
24
|
-
end
|
27
|
+
puts 'waiting...'
|
28
|
+
end
|
data/examples/jwt_auth.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
require 'dotenv'
|
1
|
+
require 'dotenv'
|
2
|
+
Dotenv.load('../.env')
|
2
3
|
require 'boxr'
|
3
4
|
require 'awesome_print'
|
4
5
|
require 'openssl'
|
5
6
|
|
7
|
+
private_key = OpenSSL::PKey::RSA.new(File.read(ENV['JWT_PRIVATE_KEY_PATH']),
|
8
|
+
ENV['JWT_PRIVATE_KEY_PASSWORD'])
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
#make sure ENV['BOX_ENTERPRISE_ID'], ENV['BOX_CLIENT_ID'] and ENV['BOX_CLIENT_SECRET'] are set
|
10
|
-
response = Boxr::get_enterprise_token(private_key: private_key)
|
10
|
+
# make sure ENV['BOX_ENTERPRISE_ID'], ENV['BOX_CLIENT_ID'] and ENV['BOX_CLIENT_SECRET'] are set
|
11
|
+
response = Boxr.get_enterprise_token(private_key: private_key)
|
11
12
|
|
12
13
|
ap response
|
data/examples/metadata_search.rb
CHANGED
@@ -1,19 +1,20 @@
|
|
1
|
-
require 'dotenv'
|
1
|
+
require 'dotenv'
|
2
|
+
Dotenv.load('../.env')
|
2
3
|
require 'awesome_print'
|
3
4
|
require 'boxr'
|
4
5
|
|
5
6
|
box_client = Boxr::Client.new(ENV['BOX_DEVELOPER_TOKEN'])
|
6
7
|
|
7
8
|
mdfilters = [
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
9
|
+
{
|
10
|
+
'templateKey' => 'test1',
|
11
|
+
'scope' => 'enterprise',
|
12
|
+
'filters' => {
|
13
|
+
'attrone' => 'blah',
|
14
|
+
'attrtwo' => { 'gt' => '4', 'lt' => '7' }
|
15
|
+
}
|
16
|
+
}
|
17
|
+
]
|
17
18
|
|
18
19
|
results = box_client.search(mdfilters: mdfilters)
|
19
|
-
ap results
|
20
|
+
ap results
|
data/examples/oauth.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
require 'dotenv'
|
1
|
+
require 'dotenv'
|
2
|
+
Dotenv.load('../.env')
|
2
3
|
require 'boxr'
|
3
4
|
require 'uri'
|
4
5
|
require 'awesome_print'
|
5
6
|
|
6
|
-
#make sure you have BOX_CLIENT_ID and BOX_CLIENT_SECRET set in your .env file
|
7
|
-
#make sure you have the redirect_uri for your application set to something like http://localhost:1234 in the developer portal
|
7
|
+
# make sure you have BOX_CLIENT_ID and BOX_CLIENT_SECRET set in your .env file
|
8
|
+
# make sure you have the redirect_uri for your application set to something like http://localhost:1234 in the developer portal
|
8
9
|
|
9
|
-
oauth_url = Boxr
|
10
|
+
oauth_url = Boxr.oauth_url(URI.encode_www_form_component('your-anti-forgery-token'))
|
10
11
|
|
11
12
|
puts "Copy the URL below and paste into a browser. Go through the OAuth flow using the desired Box account. \
|
12
13
|
When you are finished your browser will redirect to a 404 error page. This is expected behavior. Look at the URL in the address \
|
@@ -17,11 +18,7 @@ puts
|
|
17
18
|
puts "URL: #{oauth_url}"
|
18
19
|
puts
|
19
20
|
|
20
|
-
print
|
21
|
-
code =
|
22
|
-
|
23
|
-
ap Boxr::get_tokens(code)
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
print 'Enter the code: '
|
22
|
+
code = $stdin.gets.chomp.split('=').last
|
27
23
|
|
24
|
+
ap Boxr.get_tokens(code)
|
@@ -1,4 +1,5 @@
|
|
1
|
-
require 'dotenv'
|
1
|
+
require 'dotenv'
|
2
|
+
Dotenv.load('../.env')
|
2
3
|
require 'awesome_print'
|
3
4
|
require 'boxr'
|
4
5
|
|
@@ -6,13 +7,13 @@ box_client = Boxr::Client.new(ENV['BOX_DEVELOPER_TOKEN'])
|
|
6
7
|
|
7
8
|
current_user_id = box_client.me.id
|
8
9
|
|
9
|
-
folders = box_client.root_folder_items(fields:[
|
10
|
-
owned_folders = folders.select{|f| f.owned_by.id == current_user_id}
|
10
|
+
folders = box_client.root_folder_items(fields: %i[owned_by name]).folders
|
11
|
+
owned_folders = folders.select { |f| f.owned_by.id == current_user_id }
|
11
12
|
|
12
13
|
removed_count = 0
|
13
14
|
owned_folders.each do |f|
|
14
15
|
puts "Checking folder '#{f.name}'"
|
15
|
-
collabs = box_client.folder_collaborations(f, fields:[:accessible_by])
|
16
|
+
collabs = box_client.folder_collaborations(f, fields: [:accessible_by])
|
16
17
|
collabs.each do |c|
|
17
18
|
box_client.remove_collaboration(c)
|
18
19
|
removed_count += 1
|
@@ -21,4 +22,4 @@ owned_folders.each do |f|
|
|
21
22
|
end
|
22
23
|
|
23
24
|
puts
|
24
|
-
puts "Removed #{removed_count} collaborations from #{owned_folders.count} folders"
|
25
|
+
puts "Removed #{removed_count} collaborations from #{owned_folders.count} folders"
|
@@ -1,59 +1,60 @@
|
|
1
|
-
require 'dotenv'
|
1
|
+
require 'dotenv'
|
2
|
+
Dotenv.load('../.env')
|
2
3
|
require 'boxr'
|
3
|
-
require 'twilio-ruby' #make sure you 'gem install twilio-ruby'
|
4
|
-
|
4
|
+
require 'twilio-ruby' # make sure you 'gem install twilio-ruby'
|
5
|
+
|
5
6
|
# Get your Account Sid, Auth Token, and phone number from twilio.com/user/account
|
6
|
-
TWILIO_PHONE_NUMBER= ENV['TWILIO_PHONE_NUMBER']
|
7
|
-
BOX_TRIGGER_EVENT = 'UPLOAD'
|
7
|
+
TWILIO_PHONE_NUMBER = ENV['TWILIO_PHONE_NUMBER']
|
8
|
+
BOX_TRIGGER_EVENT = 'UPLOAD'.freeze
|
8
9
|
|
9
10
|
@twilio_client = Twilio::REST::Client.new(ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN'])
|
10
11
|
@box_client = Boxr::Client.new(ENV['BOX_DEVELOPER_TOKEN'])
|
11
12
|
|
12
13
|
def send_sms_to_box_user(recipient, message)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
puts e.message
|
26
|
-
end
|
14
|
+
phone = @box_client.user(recipient, fields: [:phone]).phone
|
15
|
+
unless phone.nil? || phone.empty?
|
16
|
+
begin
|
17
|
+
full_phone = "+1#{phone}"
|
18
|
+
@twilio_client.account.messages.create(
|
19
|
+
from: ENV['TWILIO_PHONE_NUMBER'],
|
20
|
+
to: full_phone,
|
21
|
+
body: message
|
22
|
+
)
|
23
|
+
puts "Sent SMS to user #{recipient.name} at #{full_phone}"
|
24
|
+
rescue Twilio::REST::RequestError => e
|
25
|
+
puts e.message
|
27
26
|
end
|
28
|
-
rescue Boxr::BoxrError => e
|
29
|
-
#most likely error is that a collaborator is an external user and Box threw a 404
|
30
|
-
puts e.message
|
31
27
|
end
|
28
|
+
rescue Boxr::BoxrError => e
|
29
|
+
# most likely error is that a collaborator is an external user and Box threw a 404
|
30
|
+
puts e.message
|
32
31
|
end
|
33
32
|
|
34
|
-
#need to look back in time to make sure we get a valid stream position;
|
35
|
-
#normally your app will be persisting the last known stream position and you wouldn't have to look this up
|
33
|
+
# need to look back in time to make sure we get a valid stream position;
|
34
|
+
# normally your app will be persisting the last known stream position and you wouldn't have to look this up
|
36
35
|
now = Time.now.utc
|
37
|
-
start_date = now - (60*60*24) #one day ago
|
36
|
+
start_date = now - (60 * 60 * 24) # one day ago
|
38
37
|
result = @box_client.enterprise_events(created_after: start_date, created_before: now)
|
39
38
|
|
40
|
-
#now that we have the latest stream position let's start monitoring in real-time
|
41
|
-
@box_client.enterprise_events_stream(result.next_stream_position, event_type: BOX_TRIGGER_EVENT,
|
42
|
-
|
39
|
+
# now that we have the latest stream position let's start monitoring in real-time
|
40
|
+
@box_client.enterprise_events_stream(result.next_stream_position, event_type: BOX_TRIGGER_EVENT,
|
41
|
+
refresh_period: 5) do |result|
|
42
|
+
if result.events.count.zero?
|
43
43
|
puts "no new #{BOX_TRIGGER_EVENT} events..."
|
44
44
|
else
|
45
45
|
puts "detected #{result.events.count} new #{BOX_TRIGGER_EVENT}"
|
46
46
|
result.events.each do |e|
|
47
47
|
folder = @box_client.folder(e.source.parent)
|
48
48
|
message = "Document '#{e.source.item_name}' uploaded to folder '#{folder.name}' by #{e.created_by.name}"
|
49
|
-
|
50
|
-
#first notify the folder owner
|
49
|
+
|
50
|
+
# first notify the folder owner
|
51
51
|
send_sms_to_box_user(folder.owned_by, message)
|
52
52
|
|
53
|
-
#now notify collaborators
|
54
|
-
user_collabs = @box_client.folder_collaborations(folder).select
|
55
|
-
|
53
|
+
# now notify collaborators
|
54
|
+
user_collabs = @box_client.folder_collaborations(folder).select do |c|
|
55
|
+
c.accessible_by.type == 'user'
|
56
|
+
end
|
57
|
+
user_collabs.each { |c| send_sms_to_box_user(c.accessible_by, message) }
|
56
58
|
end
|
57
59
|
end
|
58
60
|
end
|
59
|
-
|