Skip to content

Commit

Permalink
Add get_account and get_current_account methods
Browse files Browse the repository at this point in the history
  • Loading branch information
waits committed Jun 19, 2016
1 parent a71c8e5 commit aacb916
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/dropbox.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative 'dropbox/client'
require_relative 'dropbox/errors'
require_relative 'dropbox/account'
require_relative 'dropbox/metadata'

module Dropbox
Expand Down
37 changes: 37 additions & 0 deletions lib/dropbox/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Dropbox
class Account
attr_reader :account_id, :display_name, :email, :email_verified, :disabled, :profile_photo_url

def initialize(attrs={})
@account_id = attrs['account_id']
@display_name = attrs['name']['display_name']
@email = attrs['email']
@email_verified = attrs['email_verified']
@disabled = attrs['disabled']
@profile_photo_url = attrs['profile_photo_url']
end
end

class BasicAccount < Account
attr_reader :is_teammate, :team_member_id

def initialize(attrs={})
@is_teammate = attrs.delete('is_teammate')
@team_member_id = attrs.delete('team_member_id')
super(attrs)
end
end

class FullAccount < Account
attr_reader :locale, :referral_link, :is_paired, :profile_photo_url, :country

def initialize(attrs={})
@locale = attrs.delete('locale')
@referral_link = attrs.delete('referral_link')
@is_paired = attrs.delete('is_paired')
@profile_photo_url = attrs.delete('profile_photo_url')
@country = attrs.delete('country')
super(attrs)
end
end
end
22 changes: 18 additions & 4 deletions lib/dropbox/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ def download(path)
return parse_tagged_response(resp, 'file'), body
end

def get_account(id)
resp = request('/users/get_account', account_id: id)
parse_tagged_response(resp, 'basic_account')
end

def get_current_account
resp = request('/users/get_current_account')
parse_tagged_response(resp, 'full_account')
end

def get_metadata(path)
resp = request('/files/get_metadata', path: path)
parse_tagged_response(resp)
Expand Down Expand Up @@ -115,22 +125,26 @@ def parse_tagged_response(resp, tag=resp['.tag'])
FolderMetadata.new(resp)
when 'deleted'
DeletedMetadata.new(resp)
when 'basic_account'
BasicAccount.new(resp)
when 'full_account'
FullAccount.new(resp)
else
raise ClientError.unknown_response_type(tag)
end
end

def request(action, data = {})
def request(action, data=nil)
url = API + action
resp = HTTP.auth('Bearer ' + @access_token)
.headers(content_type: 'application/json')
.headers(content_type: ('application/json' if data))
.post(url, json: data)

raise APIError.new(resp) if resp.code != 200
JSON.parse(resp.to_s)
end

def content_request(action, args = {})
def content_request(action, args={})
url = CONTENT_API + action
resp = HTTP.auth('Bearer ' + @access_token)
.headers('Dropbox-API-Arg' => args.to_json).get(url)
Expand All @@ -140,7 +154,7 @@ def content_request(action, args = {})
return file, resp.body
end

def upload_request(action, body, args = {})
def upload_request(action, body, args={})
resp = HTTP.auth('Bearer ' + @access_token).headers({
'Content-Type' => 'application/octet-stream',
'Dropbox-API-Arg' => args.to_json,
Expand Down
38 changes: 38 additions & 0 deletions test/test_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'minitest/autorun'
require 'dropbox'

class DropboxAccountTest < Minitest::Test
def test_basic_account_initialize
account = Dropbox::BasicAccount.new({
'account_id' => 'id:123',
'name' => {'display_name' => 'John Smith'},
'email' => 'email@example.com',
'email_verified' => true,
'disabled' => false,
'profile_photo_url' => 'http://example.com'
})

assert_equal 'id:123', account.account_id
assert_equal 'John Smith', account.display_name
assert_equal true, account.email_verified
assert_equal false, account.disabled
end

def test_full_account_initialize
account = Dropbox::FullAccount.new({
'account_id' => 'id:123',
'name' => {'display_name' => 'John Smith'},
'email' => 'email@example.com',
'email_verified' => true,
'is_paired' => true,
'disabled' => false,
'profile_photo_url' => 'http://example.com'
})

assert_equal 'id:123', account.account_id
assert_equal 'John Smith', account.display_name
assert_equal true, account.email_verified
assert_equal false, account.disabled
assert_equal true, account.is_paired
end
end
33 changes: 33 additions & 0 deletions test/test_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'minitest/autorun'
require 'dropbox'

class DropboxUsersTest < Minitest::Test
def setup
@client = Dropbox::Client.new(ENV['DROPBOX_SDK_ACCESS_TOKEN'])
end

def test_get_account
id = @client.get_current_account.account_id
account = @client.get_account(id)

assert account.is_a?(Dropbox::BasicAccount)
assert_equal id, account.account_id
end

def test_get_account_error
assert_raises(Dropbox::APIError) do
@client.get_account('invalid_id')
end
end

def test_get_current_account
account = @client.get_current_account

assert account.is_a?(Dropbox::FullAccount)
assert_match /^dbid:[a-z0-9_-]+$/i, account.account_id
assert_equal 'Dylan Waits', account.display_name
assert_equal true, account.email_verified
assert_equal false, account.disabled
end

end

0 comments on commit aacb916

Please sign in to comment.