Class: Divshare::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/divshare/client.rb

Overview

This is the main class for interacting with the Divshare API. Use it like this:

client = Divshare::Client.new(api_key, api_secret)
client.(email, password)
files = client.get_files ['abcdefg-123', 'abcdefg-456']
upload_ticket = client.get_upload_ticket
client.logout

Constant Summary collapse

API_URL =
'http://www.divshare.com/api/'
UPLOAD_URL =
'http://upload.divshare.com'
UPLOAD_PATH =
'/api/upload'
SUCCESS =
'1'
FAILURE =
'0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret) ⇒ Client

Returns a new instance of Client.



31
32
33
34
# File 'lib/divshare/client.rb', line 31

def initialize(key, secret)
  @encoder = Encoder.new(key, secret)
  # @debug = true
end

Instance Attribute Details

#debug=(value) ⇒ Object

If true, extended debugging information is printed



29
30
31
# File 'lib/divshare/client.rb', line 29

def debug=(value)
  @debug = value
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.

Raises:

  • (ArgumentError)


85
86
87
88
# File 'lib/divshare/client.rb', line 85

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.



76
77
78
79
80
81
# File 'lib/divshare/client.rb', line 76

def get_files(file_ids)
  file_ids = [file_ids] unless file_ids.is_a? Array
  debug "DivShare.get_files(): #{file_ids.class}"
  files = get_user_files
  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.



103
104
105
106
107
108
109
110
# File 'lib/divshare/client.rb', line 103

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_ticketObject

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.



121
122
123
# File 'lib/divshare/client.rb', line 121

def get_upload_ticket
  send_method(:get_upload_ticket).at(:upload_ticket).inner_html
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.



93
94
95
96
97
98
99
# File 'lib/divshare/client.rb', line 93

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_infoObject

Returns information about the logged-in user



113
114
115
116
# File 'lib/divshare/client.rb', line 113

def 
  response = send_method(:get_user_info)
  user_from(response)
end

#keyObject



36
37
38
# File 'lib/divshare/client.rb', line 36

def key
  @encoder.key
end

#login(email, password) ⇒ Object



48
49
50
51
52
# File 'lib/divshare/client.rb', line 48

def (email, password)
  logout if @encoder.session_key
  response = send_method(:login, {'user_email' => email, 'user_password' => password})
  @encoder.session_key = response.at(:api_session_key).inner_html
end

#logoutObject

Returns true if logout is successful.



55
56
57
58
59
60
61
62
63
64
# File 'lib/divshare/client.rb', line 55

def logout
  response = send_method(:logout)
  debug response.to_html
  if response[:status] == SUCCESS
    @encoder.session_key = nil
    true
  else
    false
  end
end

#secretObject



40
41
42
# File 'lib/divshare/client.rb', line 40

def secret
  @encoder.secret
end

#session_keyObject



44
45
46
# File 'lib/divshare/client.rb', line 44

def session_key
  @encoder.session_key
end

#upload(ticket, file_path, response_url = 'www.divshare.com/upload_result') ⇒ Object

Uploads a file or files to the user’s DivShare account, and returns the file id(s).

The DivShare API is written for use with actual HTML forms, so the API method requires a ‘response_url’, and makes a GET request to that url, sending the file id(s) as query parameters.

Here, we’re simulating the form, so we parse DivShare’s GET request and simply return the file id(s). In this case, response_url is just a filler so that the server doesn’t complain.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/divshare/client.rb', line 135

def upload(ticket, file_path, response_url='www.divshare.com/upload_result')      
  location = nil
  File.open(file_path, 'r') { |file|
    uri = URI.parse(UPLOAD_URL)
    http = Net::HTTP.new(uri.host, uri.port)
    # API methods can be SLOW. Timeout interval should be long.
    http.read_timeout = 15*60
    request = Net::HTTP::Post.new(UPLOAD_PATH)
    fields = Hash.new
    fields['upload_ticket'] = ticket
    # API doesn't allow blank response_url. This is just filler.
    fields['response_url'] = response_url

    fields['file1'] = file
    request.multipart_params = fields
    # Until DivShare supports direct upload API, we deal with its response location field
    location = http.request(request)['location']
  }
  
  # if error, throw, otherwise return file ID for caller to do whatever they like
  resp = {}
  location.split('?')[1].split('&').each { |param| 
    k, v = param.split('=', 2)  # some params could contain two '=' for some reason
    resp[k]=CGI.unescape(v)
  }
  if resp['error']
    raise Divshare::APIError, resp['description']
  else
    resp['file1']   # return the file ID
  end
end