lib/cdnconnect_api.rb in cdnconnect-api-0.3.1 vs lib/cdnconnect_api.rb in cdnconnect-api-0.3.2
- old
+ new
@@ -11,11 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'faraday'
-require 'faraday/utils'
require 'signet/oauth_2/client'
require 'cdnconnect_api/response'
require 'logger'
@@ -24,11 +23,11 @@
##
# Used to easily interact with CDN Connect API.
class APIClient
@@application_name = 'cdnconnect-api-ruby'
- @@application_version = '0.3.1'
+ @@application_version = '0.3.2'
@@user_agent = @@application_name + ' v' + @@application_version
@@api_host = 'https://api.cdnconnect.com'
@@api_version = 'v1'
##
@@ -204,11 +203,12 @@
upload_url_response = self.get_upload_url(destination_path)
if upload_url_response.is_error
return upload_url_response
end
upload_url = upload_url_response.get_result('upload_url')
- @logger.debug("Received new upload url")
+ upload_id = upload_url_response.get_result('upload_id')
+ @logger.debug("Received new upload url, #{upload_id}")
else
@logger.debug("Use prefetched upload url")
end
# Build the data that gets sent in the POST request
@@ -260,12 +260,17 @@
@logger.info("Successful upload")
# an upload response also contains a new upload url.
# Save it for the next upload to the same destination.
- set_prefetched_upload_url(destination_path,
- upload_response.get_result('upload_url'))
+ new_upload_url = upload_response.get_result('upload_url')
+ new_upload_id = upload_response.get_result('upload_url_upload_id')
+ if new_upload_url != nil
+ @logger.debug("Received upload url in upload response, #{new_upload_id}")
+ set_prefetched_upload_url(destination_path,
+ new_upload_url)
+ end
end
end
return api_response
@@ -595,11 +600,11 @@
# @!visibility private
def get_upload_url(destination_path)
if destination_path == "/"
destination_path = ""
end
- upload_id = Random.new.rand(1_000_000..9_999_999)
+ upload_id = ((9_999_999 - 1_000_000) * rand + 1_000_000).to_i
api_path = "#{destination_path}/upload-#{upload_id}.json"
@logger.debug("get_upload_url: #{api_path}")
i = 1
@@ -888,13 +893,17 @@
def failed_uploads
@failed_uploads
end
def human_file_size(bytes)
- units = %w{B KB MB GB TB}
- e = (Math.log(bytes)/Math.log(1024)).floor
- s = "%.2f" % (bytes.to_f / 1024**e)
- s.sub(/\.?0*$/, units[e])
+ begin
+ units = %w{B KB MB GB TB}
+ e = (Math.log(bytes)/Math.log(1024)).floor
+ s = "%.2f" % (bytes.to_f / 1024**e)
+ s.sub(/\.?0*$/, units[e])
+ rescue
+ bytes
+ end
end
end
end