Class: Divshare::Client
- Inherits:
-
Object
- Object
- Divshare::Client
- Defined in:
- lib/divshare/client.rb
Overview
Constant Summary collapse
- SUCCESS =
'1'
- FAILURE =
'0'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
readonly
Returns the value of attribute api_secret.
-
#api_session_key ⇒ Object
readonly
Returns the value of attribute api_session_key.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#post_url ⇒ Object
readonly
Returns the value of attribute post_url.
Instance Method Summary collapse
-
#get_file(file_id) ⇒ Object
A convenience method for finding only one file.
-
#get_files(file_ids) ⇒ Object
This method replaces the real get_files until the API is cleared up and working properly.
-
#get_folder_files(folder_id, limit = nil, offset = nil) ⇒ Object
Returns an array of Divshare::DivshareFile objects in the specified folder.
-
#get_upload_ticket ⇒ Object
Returns an upload ticket string for use in uploading files.
-
#get_user_files(limit = nil, offset = nil) ⇒ Object
Returns an array of Divshare::DivshareFile objects belonging to the logged-in user.
-
#get_user_info ⇒ Object
Returns information about the logged-in user.
-
#initialize(api_key, api_secret, email = nil, password = nil) ⇒ Client
constructor
Creates a Divshare::Client.
-
#login(email = nil, password = nil) ⇒ Object
Login to the Divshare service.
-
#logout ⇒ Object
Returns true if logout is successful.
-
#sign(method, args) ⇒ Object
Generates the required MD5 signature as described in www.divshare.com/integrate/api#sig.
Constructor Details
#initialize(api_key, api_secret, email = nil, password = nil) ⇒ Client
Creates a Divshare::Client. The api_key
and api_secret
are required, but email
and password
are optional. If you omit email
and password
here, you must send them with login when you call it.
30 31 32 33 34 |
# File 'lib/divshare/client.rb', line 30 def initialize(api_key, api_secret, email=nil, password=nil) @api_key, @api_secret, @email, @password = api_key, api_secret, email, password @api_session_key = nil @post_url = "http://www.divshare.com/api/" end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
24 25 26 |
# File 'lib/divshare/client.rb', line 24 def api_key @api_key end |
#api_secret ⇒ Object (readonly)
Returns the value of attribute api_secret.
24 25 26 |
# File 'lib/divshare/client.rb', line 24 def api_secret @api_secret end |
#api_session_key ⇒ Object (readonly)
Returns the value of attribute api_session_key.
24 25 26 |
# File 'lib/divshare/client.rb', line 24 def api_session_key @api_session_key end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
24 25 26 |
# File 'lib/divshare/client.rb', line 24 def email @email end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
24 25 26 |
# File 'lib/divshare/client.rb', line 24 def password @password end |
#post_url ⇒ Object (readonly)
Returns the value of attribute post_url.
24 25 26 |
# File 'lib/divshare/client.rb', line 24 def post_url @post_url end |
Instance Method Details
#get_file(file_id) ⇒ Object
A convenience method for finding only one file. Returns a single DivshareFile instead of an array.
55 56 57 58 |
# File 'lib/divshare/client.rb', line 55 def get_file(file_id) raise ArgumentError, "Only one file id allowed for this method" if file_id.is_a?(Array) get_files(file_id).first end |
#get_files(file_ids) ⇒ Object
This method replaces the real get_files until the API is cleared up and working properly. Limitation: it can only retrieve files owned by the logged-in user.
46 47 48 49 50 51 |
# File 'lib/divshare/client.rb', line 46 def get_files(file_ids) file_ids = [file_ids] unless file_ids.is_a? Array files = get_user_files puts file_ids.class files.delete_if {|f| file_ids.include?(f.file_id) == false} end |
#get_folder_files(folder_id, limit = nil, offset = nil) ⇒ Object
Returns an array of Divshare::DivshareFile objects in the specified folder. Use limit
and offset
to narrow things down.
72 73 74 75 76 77 78 79 |
# File 'lib/divshare/client.rb', line 72 def get_folder_files(folder_id, limit=nil, offset=nil) args = {} args['limit'] = limit unless limit.nil? args['offset'] = offset unless offset.nil? args['folder_id'] = folder_id response = send_method(:get_folder_files, args) files_from response end |
#get_upload_ticket ⇒ Object
Returns an upload ticket string for use in uploading files. See www.divshare.com/integrate/api#uploading for more information on how to use the upload ticket once you’ve got it.
90 91 92 93 |
# File 'lib/divshare/client.rb', line 90 def get_upload_ticket response = send_method :get_upload_ticket upload_ticket_from response end |
#get_user_files(limit = nil, offset = nil) ⇒ Object
Returns an array of Divshare::DivshareFile objects belonging to the logged-in user. Use limit
and offset
to narrow things down.
62 63 64 65 66 67 68 |
# File 'lib/divshare/client.rb', line 62 def get_user_files(limit=nil, offset=nil) args = {} args['limit'] = limit unless limit.nil? args['offset'] = offset unless offset.nil? response = send_method(:get_user_files, args) files_from response end |
#get_user_info ⇒ Object
Returns information about the logged-in user
82 83 84 85 |
# File 'lib/divshare/client.rb', line 82 def get_user_info response = send_method :get_user_info user_from response end |
#login(email = nil, password = nil) ⇒ Object
Login to the Divshare service. Raises Divshare::APIError if login is unsuccessful.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/divshare/client.rb', line 97 def login(email=nil, password=nil) logout if @api_session_key email ||= @email password ||= @password response = send_method(:login, {'user_email' => email, 'user_password' => password}) if response.at(:api_session_key) @api_session_key = response.at(:api_session_key).inner_html else raise Divshare::APIError, "Couldn't log in. Received: \n" + response.to_s end end |
#logout ⇒ Object
Returns true if logout is successful. Raises Divshare::APIError if logout is unsuccessful.
111 112 113 114 115 116 117 118 119 |
# File 'lib/divshare/client.rb', line 111 def logout response = send_method(:logout) if response.at(:logged_out) && (%w(true 1).include? response.at(:logged_out).inner_html) @api_session_key = nil else raise Divshare::APIError, "Couldn't log out. Received: \n" + response.to_s end true end |
#sign(method, args) ⇒ Object
Generates the required MD5 signature as described in www.divshare.com/integrate/api#sig
123 124 125 |
# File 'lib/divshare/client.rb', line 123 def sign(method, args) Digest::MD5.hexdigest(string_to_sign(args)) end |