boxr 0.17.0 → 0.18.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: 8ae5afb3d84f050d0294084395417af4b38e3036
4
- data.tar.gz: 9e2e2a339d4839e4ce2f7a8d1a2fd191ef64a834
3
+ metadata.gz: 9c4c27adff340e29abba4bcf0faad07015ac528c
4
+ data.tar.gz: 683c1ef609710dc2bb9c661562c94e461c4f2766
5
5
  SHA512:
6
- metadata.gz: f3bdf146ed55c4da53f519a6df8b1cda0c979be4c05b4b4232bc8852aca4435473953a7346f8455062fd571aac301409f751fc6ab6a92878ee500f8b34bec91a
7
- data.tar.gz: 1462e2544c327bc4fc4ced82426ee8be6ceb853dbcb84b530a7b07456e6a67bb8a6f5d75e71fb918e8d0fe6ae022a352a2ae68d9f59d384dda4f7a5ea629765c
6
+ metadata.gz: e315eb590f313753ba1c01f171ab9fda6f463f98372e16d9c4bf27a3c3a34bdaf0e04797e215cdbaafef4b6992417cd7b62281a547eeab156929cc87e0387bdb
7
+ data.tar.gz: 4da05a5a7890a733219eddf0e8e1e5ae3d36391825f5bfcc616e1ebbc09dbe22254bf8836ab6773d291a13748281661d88e33863962c2fd8e2ac1bf05e2e3a51
@@ -10,19 +10,24 @@ BOX_TRIGGER_EVENT = 'UPLOAD'
10
10
  @box_client = Boxr::Client.new(ENV['BOX_DEVELOPER_TOKEN'])
11
11
 
12
12
  def send_sms_to_box_user(recipient, message)
13
- phone = @box_client.user(recipient, fields: [:phone]).phone
14
- unless phone.nil? || phone.empty?
15
- begin
16
- full_phone = "+1#{phone}"
17
- @twilio_client.account.messages.create(
18
- from: ENV['TWILIO_PHONE_NUMBER'],
19
- to: full_phone,
20
- body: message
21
- )
22
- puts "Sent SMS to user #{recipient.name} at #{full_phone}"
23
- rescue Twilio::REST::RequestError => e
24
- puts e.message
13
+ begin
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
26
+ end
25
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
26
31
  end
27
32
  end
28
33
 
@@ -33,7 +38,7 @@ start_date = now - (60*60*24) #one day ago
33
38
  result = @box_client.enterprise_events(created_after: start_date, created_before: now)
34
39
 
35
40
  #now that we have the latest stream position let's start monitoring in real-time
36
- @box_client.enterprise_events_stream(result.next_stream_position, event_type: BOX_TRIGGER_EVENT) do |result|
41
+ @box_client.enterprise_events_stream(result.next_stream_position, event_type: BOX_TRIGGER_EVENT, refresh_period: 5) do |result|
37
42
  if result.events.count==0
38
43
  puts "no new #{BOX_TRIGGER_EVENT} events..."
39
44
  else
@@ -0,0 +1,92 @@
1
+ require 'dotenv'; Dotenv.load("../.env")
2
+ require 'boxr'
3
+ require 'mailgun' #make sure you 'gem install mailgun-ruby'
4
+
5
+ BOX_TRIGGER_EVENT = 'ITEM_SHARED_UPDATE,COLLABORATION_INVITE'
6
+ FROM_EMAIL = "box-admin@#{ENV['MAILGUN_SENDING_DOMAIN']}"
7
+ VALID_COLLABORATION_DOMAIN=ENV['VALID_COLLABORATION_DOMAIN'] #i.e. 'your-company.com'
8
+
9
+ @mailgun_client = Mailgun::Client.new ENV['MAILGUN_API_KEY']
10
+ @box_client = Boxr::Client.new(ENV['BOX_DEVELOPER_TOKEN'])
11
+
12
+ def file_warning_text(event)
13
+ %(
14
+ Our records indicate that you just created an open shared link for the document '#{event.source.item_name}'
15
+ located in the folder '#{event.source.parent.name}'.
16
+
17
+ If you meant to do this, please ignore this email. Otherwise, please update this shared link
18
+ so that it is no longer open access.
19
+
20
+ Thanks,
21
+ Your Box Admin
22
+ )
23
+ end
24
+
25
+ def folder_warning_text(event)
26
+ %(
27
+ Our records indicate that you just created an open shared link for the folder '#{event.source.item_name}'.
28
+
29
+ If you meant to do this, please ignore this email. Otherwise, please update this shared link
30
+ so that it is no longer open access.
31
+
32
+ Thanks,
33
+ Your Box Admin
34
+ )
35
+ end
36
+
37
+ def collaboration_warning_text(event)
38
+ %(
39
+ Our records indicate that you just invited #{event.accessible_by.login} to be an external collaborator
40
+ with the role of #{event.additional_details.role} in the folder '#{event.source.folder_name}'.
41
+
42
+ If you meant to do this, please ignore this email. Otherwise, please remove this collaborator.
43
+
44
+ Thanks,
45
+ Your Box Admin
46
+ )
47
+ end
48
+
49
+ def send_email_to_box_user(from, to, subject, text)
50
+ #this example code uses Mailgun to send email, but you can use any standard SMTP server
51
+ message_params = {:from => from, :to => to, :subject => subject, :text => text}
52
+ @mailgun_client.send_message ENV['MAILGUN_SENDING_DOMAIN'], message_params
53
+
54
+ puts "Sent email to #{to} with subject '#{subject}'"
55
+ end
56
+
57
+ #need to look back in time to make sure we get a valid stream position;
58
+ #normally your app will be persisting the last known stream position and you wouldn't have to look this up
59
+ now = Time.now
60
+ start_date = now - (60*60*24) #one day ago
61
+ result = @box_client.enterprise_events(created_after: start_date, created_before: now)
62
+
63
+ #now that we have the latest stream position let's start monitoring in real-time
64
+ @box_client.enterprise_events_stream(result.next_stream_position, event_type: BOX_TRIGGER_EVENT, refresh_period: 60) do |result|
65
+ if result.events.count==0
66
+ puts "no new #{BOX_TRIGGER_EVENT} events..."
67
+ else
68
+ result.events.each do |e|
69
+ puts "detected new #{e.event_type}"
70
+ if e.event_type=='ITEM_SHARED_UPDATE'
71
+ if e.source.item_type=='file'
72
+ file = @box_client.file(e.source.item_id)
73
+ if file.shared_link.effective_access=='open'
74
+ send_email_to_box_user(FROM_EMAIL, e.created_by.login, "WARNING: Open Shared Link Detected", file_warning_text(e))
75
+ end
76
+ elsif e.source.item_type=='folder'
77
+ folder = @box_client.folder(e.source.item_id)
78
+ if folder.shared_link.effective_access=='open'
79
+ send_email_to_box_user(FROM_EMAIL, e.created_by.login, "WARNING: Open Shared Link Detected", folder_warning_text(e))
80
+ end
81
+ end
82
+ elsif e.event_type=='COLLABORATION_INVITE'
83
+ email = e.accessible_by.login
84
+ domain = email.split('@').last
85
+ unless domain.downcase==VALID_COLLABORATION_DOMAIN.downcase
86
+ send_email_to_box_user(FROM_EMAIL, e.created_by.login, "WARNING: External Collaborator Detected", collaboration_warning_text(e))
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+
@@ -3,7 +3,7 @@ require 'httpclient'
3
3
  require 'hashie'
4
4
 
5
5
  require 'boxr/version'
6
- require 'boxr/exceptions'
6
+ require 'boxr/errors'
7
7
  require 'boxr/client'
8
8
  require 'boxr/shared_items'
9
9
  require 'boxr/folders'
@@ -38,7 +38,7 @@ module Boxr
38
38
  body_json = Oj.load(res.body)
39
39
  return Hashie::Mash.new(body_json)
40
40
  else
41
- raise BoxrException.new(status: res.status, body: res.body, header: res.header)
41
+ raise BoxrError.new(status: res.status, body: res.body, header: res.header)
42
42
  end
43
43
  end
44
44
 
@@ -54,7 +54,7 @@ module Boxr
54
54
  def initialize(access_token=ENV['BOX_DEVELOPER_TOKEN'], refresh_token: nil, box_client_id: ENV['BOX_CLIENT_ID'], box_client_secret: ENV['BOX_CLIENT_SECRET'],
55
55
  identifier: nil, as_user: nil, &token_refresh_listener)
56
56
  @access_token = access_token
57
- raise BoxrException.new(boxr_message: "Access token cannot be nil") if @access_token.nil?
57
+ raise BoxrError.new(boxr_message: "Access token cannot be nil") if @access_token.nil?
58
58
 
59
59
  @refresh_token = refresh_token
60
60
  @box_client_id = box_client_id
@@ -103,7 +103,7 @@ module Boxr
103
103
 
104
104
  entries << body_json["entries"]
105
105
  else
106
- raise BoxrException.new(status: res.status, body: res.body, header: res.header)
106
+ raise BoxrError.new(status: res.status, body: res.body, header: res.header)
107
107
  end
108
108
  end until offset - total_count >= 0
109
109
 
@@ -190,7 +190,7 @@ module Boxr
190
190
  end
191
191
 
192
192
  def check_response_status(res, success_codes)
193
- raise BoxrException.new(status: res.status, body: res.body, header: res.header) unless success_codes.include?(res.status)
193
+ raise BoxrError.new(status: res.status, body: res.body, header: res.header) unless success_codes.include?(res.status)
194
194
  end
195
195
 
196
196
  def processed_response(res)
@@ -211,7 +211,7 @@ module Boxr
211
211
  def ensure_id(item)
212
212
  return item if item.class == String || item.class == Fixnum || item.nil?
213
213
  return item.id if item.respond_to?(:id)
214
- raise BoxrException.new(boxr_message: "Expecting an id of class String or Fixnum, or object that responds to :id")
214
+ raise BoxrError.new(boxr_message: "Expecting an id of class String or Fixnum, or object that responds to :id")
215
215
  end
216
216
 
217
217
  def restore_trashed_item(uri, name, parent)
@@ -1,6 +1,6 @@
1
1
  module Boxr
2
2
 
3
- class BoxrException < Exception
3
+ class BoxrError < StandardError
4
4
 
5
5
  attr_reader :response_body, :type, :status, :code, :help_uri, :box_message, :boxr_message, :request_id
6
6
 
@@ -1,14 +1,14 @@
1
1
  module Boxr
2
2
  class Client
3
3
 
4
- def user_events(stream_position, stream_type: :all, limit: 100)
4
+ def user_events(stream_position, stream_type: :all, limit: 800)
5
5
  query = {stream_position: stream_position, stream_type: stream_type, limit: limit}
6
6
 
7
7
  events, response = get(EVENTS_URI, query: query)
8
8
  Hashie::Mash.new({events: events["entries"], chunk_size: events["chunk_size"], next_stream_position: events["next_stream_position"]})
9
9
  end
10
10
 
11
- def enterprise_events(created_after: nil, created_before: nil, stream_position: 0, event_type: nil, limit: 100)
11
+ def enterprise_events(created_after: nil, created_before: nil, stream_position: 0, event_type: nil, limit: 500)
12
12
  events = []
13
13
  loop do
14
14
  event_response = get_enterprise_events(created_after, created_before, stream_position, event_type, limit)
@@ -20,7 +20,7 @@ module Boxr
20
20
  Hashie::Mash.new({events: events, next_stream_position: stream_position})
21
21
  end
22
22
 
23
- def enterprise_events_stream(initial_stream_position, event_type: nil, limit: 100, refresh_period: 5)
23
+ def enterprise_events_stream(initial_stream_position, event_type: nil, limit: 500, refresh_period: 300)
24
24
  stream_position = initial_stream_position
25
25
  loop do
26
26
  response = enterprise_events(stream_position: stream_position, event_type: event_type, limit: limit)
@@ -13,7 +13,7 @@ module Boxr
13
13
 
14
14
  files = folder_items(folder, fields: [:id, :name]).files
15
15
  file = files.select{|f| f.name == file_name}.first
16
- raise BoxrException.new(boxr_message: "File not found: '#{file_name}'") if file.nil?
16
+ raise BoxrError.new(boxr_message: "File not found: '#{file_name}'") if file.nil?
17
17
  file
18
18
  end
19
19
 
@@ -86,7 +86,7 @@ module Boxr
86
86
 
87
87
  File.open(path_to_file) do |file|
88
88
  content_md5 = send_content_md5 ? Digest::SHA1.file(file).hexdigest : nil
89
-
89
+
90
90
  attributes = {filename: file, parent_id: parent_id}
91
91
  attributes[:content_created_at] = content_created_at.to_datetime.rfc3339 unless content_created_at.nil?
92
92
  attributes[:content_modified_at] = content_modified_at.to_datetime.rfc3339 unless content_modified_at.nil?
@@ -11,7 +11,7 @@ module Boxr
11
11
  folder = path_folders.inject(Boxr::ROOT) do |parent_folder, folder_name|
12
12
  folders = folder_items(parent_folder, fields: [:id, :name]).folders
13
13
  folder = folders.select{|f| f.name == folder_name}.first
14
- raise BoxrException.new(boxr_message: "Folder not found: '#{folder_name}'") if folder.nil?
14
+ raise BoxrError.new(boxr_message: "Folder not found: '#{folder_name}'") if folder.nil?
15
15
  folder
16
16
  end
17
17
  end
@@ -1,3 +1,3 @@
1
1
  module Boxr
2
- VERSION = "0.17.0"
2
+ VERSION = "0.18.0"
3
3
  end
@@ -449,7 +449,7 @@ describe Boxr::Client do
449
449
  expect(results).to eq([])
450
450
  end
451
451
 
452
- it "invokes a Boxr exception" do
452
+ it "invokes a Boxr error" do
453
453
  expect { BOX_CLIENT.folder(1)}.to raise_error
454
454
  end
455
455
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boxr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chad Burnette
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-09 00:00:00.000000000 Z
11
+ date: 2015-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -167,6 +167,7 @@ files:
167
167
  - examples/enterprise_events.rb
168
168
  - examples/oauth.rb
169
169
  - examples/use_events_to_send_sms.rb
170
+ - examples/use_events_to_warn_about_sharing.rb
170
171
  - examples/user_events.rb
171
172
  - lib/boxr.rb
172
173
  - lib/boxr/auth.rb
@@ -174,8 +175,8 @@ files:
174
175
  - lib/boxr/collaborations.rb
175
176
  - lib/boxr/collections.rb
176
177
  - lib/boxr/comments.rb
178
+ - lib/boxr/errors.rb
177
179
  - lib/boxr/events.rb
178
- - lib/boxr/exceptions.rb
179
180
  - lib/boxr/files.rb
180
181
  - lib/boxr/folders.rb
181
182
  - lib/boxr/groups.rb