rbox 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.
@@ -0,0 +1,19 @@
1
+ .DS_Store
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'faraday', '>= 0.7.0', :require => 'faraday'
4
+ gem 'faraday_middleware', '>= 0.7.0', :require => 'faraday_middleware'
5
+ gem 'multi_xml', '~> 0.4.1'
6
+
7
+ group :test do
8
+ gem 'minitest'
9
+ gem 'vcr'
10
+ gem 'rake'
11
+ end
@@ -0,0 +1,23 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ faraday (0.8.0)
5
+ multipart-post (~> 1.1)
6
+ faraday_middleware (0.8.7)
7
+ faraday (>= 0.7.4, < 0.9)
8
+ minitest (2.11.1)
9
+ multi_xml (0.4.1)
10
+ multipart-post (1.1.5)
11
+ rake (0.9.2.2)
12
+ vcr (2.1.1)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ faraday (>= 0.7.0)
19
+ faraday_middleware (>= 0.7.0)
20
+ minitest
21
+ multi_xml (~> 0.4.1)
22
+ rake
23
+ vcr
@@ -0,0 +1,33 @@
1
+ # Client modules
2
+ require 'rbox/connection'
3
+ require 'rbox/authentication'
4
+ require 'rbox/download'
5
+ require 'rbox/item'
6
+ # Client class
7
+ require 'rbox/client'
8
+
9
+ # Utility methods modules
10
+ require 'rbox/utils/collect_nested_key'
11
+
12
+ # Base response module
13
+ require 'rbox/response/base'
14
+ # Authentication response classes
15
+ require 'rbox/response/authentication/ticket'
16
+ require 'rbox/response/authentication/get_account_info'
17
+ require 'rbox/response/authentication/verify_registration_email'
18
+ require 'rbox/response/authentication/auth_token'
19
+ require 'rbox/response/authentication/logout'
20
+ require 'rbox/response/authentication/register_new_account'
21
+ # Items response classes
22
+ require 'rbox/response/items/account_tree'
23
+ require 'rbox/response/items/folder'
24
+ require 'rbox/response/items/file'
25
+
26
+
27
+ module Rbox
28
+ class << self
29
+ def new(*arg)
30
+ Rbox::Client.new(*arg)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,43 @@
1
+ module Rbox
2
+ module Authentication
3
+
4
+ def get_ticket
5
+ response = action(:get_ticket)
6
+ Response::Ticket.new(response, self)
7
+ end
8
+
9
+ def get_auth_token
10
+ raise 'Ticket is require for this operation' unless ticket
11
+
12
+ response = action(:get_auth_token) do |req|
13
+ req.params[:ticket] = ticket
14
+ end
15
+ Response::AuthToken.new(response, self)
16
+ end
17
+
18
+ def logout
19
+ response = action(:logout)
20
+ Response::Logout.new(response, self)
21
+ end
22
+
23
+ def register_new_user(login, password)
24
+ response = action(:register_new_user)
25
+ Response::RegisterNewUser.new(response, self)
26
+ end
27
+
28
+ def verify_registration_email
29
+ response = action(:verify_registration_email)
30
+ Response::VerifyRegistrationEmail.new(response, self)
31
+ end
32
+
33
+ def get_account_info
34
+ response = action(:get_account_info)
35
+ Response::GetAccountInfo.new(response, self)
36
+ end
37
+
38
+ def authorize_url
39
+ "https://www.box.net/api/1.0/auth/#{@ticket}"
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,20 @@
1
+ module Rbox
2
+ class Client
3
+ include Connection
4
+ include Authentication
5
+ include Download
6
+ include Item
7
+
8
+ attr_accessor :config, :api_token, :auth_token, :ticket
9
+ attr_reader :connection
10
+
11
+ def initialize(options = {})
12
+ @config = options
13
+ @api_token = @config[:api_token]
14
+ @auth_token = @config[:auth_token]
15
+ @ticket = @config[:ticket]
16
+ @connection = default_connection
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module Rbox
5
+ module Connection
6
+
7
+ private
8
+ def action(action, options = {}, &block)
9
+ params = { :action => action.to_s }
10
+ params[:api_key] = api_token if api_token
11
+ params[:auth_token] = auth_token if auth_token
12
+ connection.get('/api/1.0/rest') do |req|
13
+ req.params = req.params.merge(params)
14
+ block.call(req) if block_given?
15
+ end
16
+ end
17
+
18
+ def default_connection
19
+ Faraday.new(:url => 'https://www.box.net/') do |builder|
20
+ builder.use Faraday::Response::ParseXml
21
+ builder.use Faraday::Request::UrlEncoded
22
+ builder.use Faraday::Response::Logger if @config[:logger]
23
+ builder.use Faraday::Adapter::NetHttp
24
+ end
25
+ end
26
+
27
+ def set_connection(*arg, &block)
28
+ @connection = Faraday.send(:new, *args, &block)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ module Rbox
2
+ module Download
3
+
4
+ def download_url(file_id)
5
+ "https://www.box.net/api/1.0/download/#{auth_token}/#{file_id}"
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,43 @@
1
+ module Rbox
2
+ module Item
3
+
4
+ def get_account_tree(options = { :folder_id => 0 })
5
+ raise 'Auth token and api key is require for this operation' unless auth_token and api_token
6
+
7
+ response = action(:get_account_tree) do |req|
8
+ req.params['params[]'] = 'nozip'
9
+ req.params.merge!(options)
10
+ end
11
+ Response::AccountTree.new(response, self)
12
+ end
13
+
14
+ def get_file_info(file_id, options = {} )
15
+ raise 'Auth token and api key is require for this operation' unless auth_token and api_token
16
+
17
+ response = action(:get_file_info) do |req|
18
+ req.params.merge!(options)
19
+ req.params[:file_id] = file_id
20
+ end
21
+
22
+ response_file = response.body['response']['info']
23
+ response_file['id'] = response_file['file_id']
24
+
25
+ Response::File.new(response_file, self)
26
+ end
27
+
28
+ def public_share(target_id, target_type = 'file')
29
+ response = action(:public_share) do |req|
30
+ req.params[:target] = target_type
31
+ req.params[:target_id] = target_id
32
+
33
+ # Yes, we need those empty strings
34
+ %w(password message emails[]).each do |key|
35
+ req.params[key] = ''
36
+ end
37
+ end
38
+
39
+ response.body['response']['public_name']
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ module Rbox
2
+ module Response
3
+ class AuthToken
4
+ include Base
5
+
6
+ def initialize(*arg)
7
+ super(*arg)
8
+ @client.auth_token = auth_token
9
+ end
10
+
11
+ def auth_token
12
+ @body_response['auth_token']
13
+ end
14
+
15
+ def user
16
+ @body_response['user']
17
+ end
18
+
19
+ def attributes
20
+ { :user => user, :auth_token => auth_token }
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ module Rbox
2
+ module Response
3
+ class GetAccountInfo
4
+ include Base
5
+
6
+ def user
7
+ @body_response['user']
8
+ end
9
+
10
+ def attributes
11
+ { :user => user }
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module Rbox
2
+ module Response
3
+ class Logout
4
+ include Base
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ module Rbox
2
+ module Response
3
+ class RegisterNewAccount
4
+ include Base
5
+
6
+ def initialize(*arg)
7
+ super(*arg)
8
+ @client.auth_token = auth_token
9
+ end
10
+
11
+ def auth_token
12
+ @body_response['auth_token']
13
+ end
14
+
15
+ def user
16
+ @body_response['user']
17
+ end
18
+
19
+ def attributes
20
+ { :user => user, :auth_token => auth_token }
21
+ end
22
+
23
+ def success?
24
+ !!(status == 'successful_register')
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module Rbox
2
+ module Response
3
+ class Ticket
4
+ include Base
5
+
6
+ def initialize(*arg)
7
+ super(*arg)
8
+ @client.ticket = ticket
9
+ end
10
+
11
+ def ticket
12
+ @body_response['ticket']
13
+ end
14
+
15
+ def authentization_url
16
+ "https://www.box.net/api/1.0/auth/#{ticket}"
17
+ end
18
+
19
+ def auth_token
20
+ @client.auth_token(ticket)
21
+ end
22
+
23
+ def attributes
24
+ { :ticket => @body_response['ticket'] }
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ module Rbox
2
+ module Response
3
+ class VerifyRegistrationEmail
4
+ include Base
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ module Rbox
2
+ module Response
3
+ module Base
4
+ def initialize(response, client)
5
+ @response = response
6
+ @body_response = response.body['response']
7
+ @client = client
8
+ raise "Response failure with status code \"#{status}\"" unless success?
9
+ end
10
+
11
+ def success?
12
+ !!(status =~ /_ok\z/)
13
+ end
14
+
15
+ def status
16
+ @body_response['status']
17
+ end
18
+
19
+ def method_missing(name, *args, &block)
20
+ @client.send(name, *args, &block)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ module Rbox
2
+ module Response
3
+ class AccountTree
4
+ include Base
5
+ include CollectNestedKey
6
+
7
+ def root_folder
8
+ @root_folder ||= Folder.new(@body_response['tree']['folder'], @client)
9
+ end
10
+ alias :tree :root_folder
11
+
12
+ def files
13
+ @files ||= collect_nested_key(@body_response['tree'], 'file').map {|f| File.new(f, @client) }
14
+ end
15
+
16
+ def folders
17
+ @folder ||= collect_nested_key(@body_response['tree'], 'folder').map {|f| Folder.new(f, @client) }
18
+ end
19
+
20
+ def to_array
21
+ root_folder.folders.map {|f| f.to_hash } + root_folder.files.map {|f| f.to_hash }
22
+ end
23
+ alias :to_a :to_array
24
+
25
+ def to_hash
26
+ root_folder.to_hash
27
+ end
28
+ alias :to_h :to_hash
29
+ alias :attributes :to_hash
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,57 @@
1
+ module Rbox
2
+ module Response
3
+ class File
4
+
5
+ ATTRIBUTES = %w(id file_name shared sha1 size shared_link description user_id thumbnail small_thumbnail large_thumbnail larger_thumbnail preview_thumbnail permissions tags)
6
+
7
+ ATTRIBUTES.each do |attr_name|
8
+ define_method(attr_name) do
9
+ instance_variable_get(:@attributes)[attr_name]
10
+ end
11
+ end
12
+
13
+ %w(created updated).each do |attr_name|
14
+ define_method(attr_name) do
15
+ Time.at(instance_variable_get(:@attributes)[attr_name])
16
+ end
17
+ end
18
+
19
+ def initialize(attributes, client, parent_folder = nil)
20
+ @attributes = attributes.dup
21
+ @client = client
22
+ @parent_folder = parent_folder
23
+ end
24
+
25
+ def attributes
26
+ @attributes
27
+ end
28
+
29
+ def download_url
30
+ @client.download_url(id)
31
+ end
32
+
33
+ # public share
34
+ # https://www.box.net/api/1.0/rest?action=public_share&api_key=rrc1d3ntb53tt6b2vhail6rdtrsxov3v&auth_token=rpuis3lincpbyz60gyym8s3xhnc6gbcl&target=folder&target_id=709&password=&message=hey&emails[]=email@example.com&emails[]=email2@example.com
35
+ def public_share!
36
+ @public_name ||= @client.public_share(self.id, 'file')
37
+ true
38
+ end
39
+
40
+ def public_share_url
41
+ if @public_link.nil? || @public_link.empty?
42
+ public_share!
43
+ "https://www.box.net/s/#{@public_name}"
44
+ else
45
+ @public_link
46
+ end
47
+ end
48
+
49
+ def to_hash
50
+ hash = ATTRIBUTES.inject({}) {|h,k,v| h[k.to_sym]= @attributes[k]; h }
51
+ hash[:type] = 'file'
52
+ hash[:parent_folder_id] = @parent_folder.id if @parent_folder
53
+ hash
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,64 @@
1
+ module Rbox
2
+ module Response
3
+ class Folder
4
+
5
+ ATTRIBUTES = %w(id name description user_id shared shared_link permissions size file_count tags)
6
+
7
+ ATTRIBUTES.each do |attr_name|
8
+ define_method(attr_name) do
9
+ instance_variable_get(:@attributes)[attr_name]
10
+ end
11
+ end
12
+
13
+ %w(created updated).each do |attr_name|
14
+ define_method(attr_name) do
15
+ Time.at(instance_variable_get(:@attributes)[attr_name])
16
+ end
17
+ end
18
+
19
+ def initialize(attributes, client, parent_folder = nil)
20
+ @attributes = attributes.dup
21
+ @client = client
22
+
23
+ @parent_folder = parent_folder
24
+
25
+ @folders_attr = [@attributes['folders']['folder']].flatten rescue []
26
+ @attributes.delete('folders')
27
+
28
+ # lol, { files => { file => [file, ...] } }
29
+ @files_attr = [@attributes['files']['file']].flatten rescue []
30
+ @attributes.delete('files')
31
+ end
32
+
33
+ def folders
34
+ @folders ||= @folders_attr.map {|f| Folder.new(f, @client, self) }
35
+ end
36
+
37
+ def create_folder(name, options = {})
38
+ options[:parent_id] = id
39
+ @client.create_folder(name, options)
40
+ end
41
+
42
+ def files
43
+ @files ||= @files_attr.map {|f| File.new(f, @client, self) }
44
+ end
45
+
46
+ def attributes
47
+ @attributes
48
+ end
49
+
50
+ def root?
51
+ id == '0'
52
+ end
53
+
54
+ def to_hash
55
+ hash = ATTRIBUTES.inject({}) {|h,k,v| h[k.to_sym]= @attributes[k]; h }
56
+ hash[:files] = files.map {|f| f.to_hash }
57
+ hash[:folders] = folders.map {|f| f.to_hash }
58
+ hash[:type] = 'folder'
59
+ hash[:parent_folder_id] = @parent_folder.id if @parent_folder
60
+ hash
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,26 @@
1
+ module Rbox
2
+ module CollectNestedKey
3
+
4
+ def collect_nested_key(obj, key)
5
+ result = []
6
+
7
+ case obj
8
+ when Hash
9
+ if obj.key? key
10
+ result << obj[key]
11
+ else
12
+ obj.each do |k, child|
13
+ result += collect_nested_key(child, key)
14
+ end
15
+ end
16
+ when Array
17
+ obj.each do |child|
18
+ result += collect_nested_key(child, key)
19
+ end
20
+ end
21
+
22
+ result.flatten
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.add_dependency 'faraday', '>= 0.7'
5
+ gem.add_dependency 'multi_xml', '~> 0.4.1'
6
+
7
+ gem.authors = ["Charles Barbier"]
8
+ gem.description = %q{A Ruby wrapper for box.com}
9
+ gem.email = ['unixcharles@gmail.com']
10
+ gem.files = `git ls-files`.split("\n")
11
+ gem.homepage = 'https://github.com/unixcharles/rbox'
12
+ gem.name = 'rbox'
13
+ gem.require_paths = ['lib']
14
+ gem.summary = %q{box.com API wrapper}
15
+ gem.test_files = `git ls-files -- {spec}/*`.split("\n")
16
+ gem.version = '0.1.0'
17
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Charles Barbier
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: &70133377507860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0.7'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70133377507860
25
+ - !ruby/object:Gem::Dependency
26
+ name: multi_xml
27
+ requirement: &70133377507360 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.4.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70133377507360
36
+ description: A Ruby wrapper for box.com
37
+ email:
38
+ - unixcharles@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - Gemfile.lock
46
+ - lib/rbox.rb
47
+ - lib/rbox/authentication.rb
48
+ - lib/rbox/client.rb
49
+ - lib/rbox/connection.rb
50
+ - lib/rbox/download.rb
51
+ - lib/rbox/item.rb
52
+ - lib/rbox/response/authentication/auth_token.rb
53
+ - lib/rbox/response/authentication/get_account_info.rb
54
+ - lib/rbox/response/authentication/logout.rb
55
+ - lib/rbox/response/authentication/register_new_account.rb
56
+ - lib/rbox/response/authentication/ticket.rb
57
+ - lib/rbox/response/authentication/verify_registration_email.rb
58
+ - lib/rbox/response/base.rb
59
+ - lib/rbox/response/items/account_tree.rb
60
+ - lib/rbox/response/items/file.rb
61
+ - lib/rbox/response/items/folder.rb
62
+ - lib/rbox/utils/collect_nested_key.rb
63
+ - rbox.gemspec
64
+ homepage: https://github.com/unixcharles/rbox
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.10
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: box.com API wrapper
88
+ test_files: []