Skip to content

Commit

Permalink
DRY: move the repeated code to a Utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvisa committed Nov 17, 2014
1 parent 5822b87 commit 6a468c7
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format doc
1 change: 1 addition & 0 deletions lib/upyun.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'upyun/version'
require 'upyun/utils'
require 'upyun/rest'
require 'upyun/form'

Expand Down
15 changes: 4 additions & 11 deletions lib/upyun/form.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# encoding: utf-8
require 'restclient'
require 'digest/md5'
require 'base64'
require 'json'
require 'active_support/hash_with_indifferent_access'

module Upyun
class Form
include Utils

VALID_PARAMS = %w(
bucket
save-key
Expand All @@ -31,22 +32,14 @@ class Form
ext-param
)

attr_accessor :endpoint, :bucket, :password
attr_accessor :bucket, :password

def initialize(password, bucket)
@password = password
@bucket = bucket
@endpoint = ED_AUTO
end

def endpoint=(ep)
unless Upyun::ED_LIST.member?(ep)
raise ArgumentError, "Valid endpoint are #{Upyun::ED_LIST}"
end

@endpoint = ep
end

def upload(file, opts={})
base_opts = HashWithIndifferentAccess.new({
'bucket' => @bucket,
Expand Down Expand Up @@ -81,7 +74,7 @@ def policy(opts)
end

def signature
Digest::MD5.hexdigest("#{@_policy}&#{@password}")
md5("#{@_policy}&#{@password}")
end

def policy_json(opts)
Expand Down
12 changes: 1 addition & 11 deletions lib/upyun/rest.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# encoding: utf-8
require 'restclient'
require 'digest/md5'
require 'uri'

module Upyun
class Rest
attr_accessor :endpoint
include Utils

def initialize(bucket, operator, password, endpoint=Upyun::ED_AUTO)
@bucket = bucket
Expand All @@ -14,11 +13,6 @@ def initialize(bucket, operator, password, endpoint=Upyun::ED_AUTO)
@endpoint = endpoint
end

def endpoint=(ep)
raise ArgumentError, "Valid endpoint are #{Upyun::ED_LIST}" unless Upyun::ED_LIST.member?(ep)
@endpoint = ep
end

def put(path, file, headers={})
raise ArgumentError, "'file' is not an instance of String" unless file.is_a?(String)
headers = headers.merge({"mkdir" => true}) unless headers.key?("mkdir")
Expand Down Expand Up @@ -134,9 +128,5 @@ def sign(method, date, path, length)
sign = "#{method.to_s.upcase}&#{path}&#{date}&#{length}&#{@password}"
"UpYun #{@operator}:#{md5(sign)}"
end

def md5(str)
Digest::MD5.hexdigest(str)
end
end
end
19 changes: 19 additions & 0 deletions lib/upyun/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'digest/md5'

module Upyun
module Utils
def md5(str)
Digest::MD5.hexdigest(str)
end

def self.included(receiver)
receiver.send(:define_method, :endpoint) { @endpoint }
receiver.send(:define_method, :endpoint=) do |ep|
unless Upyun::ED_LIST.member?(ep)
raise ArgumentError, "Valid endpoints are: #{Upyun::ED_LIST}"
end
@endpoint = ep
end
end
end
end
105 changes: 75 additions & 30 deletions spec/upyun_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
require File.dirname(__FILE__) + "/spec_helper"
require File.dirname(__FILE__) + '/spec_helper'

describe "Upyun Restful API Basic testing" do
before :all do
@upyun = Upyun::Rest.new("sdkfile", "tester", "grjxv2mxELR3")
@file = File.expand_path("../upyun.jpg", __FILE__)
@str = "This is a binary string, not a file"
@upyun = Upyun::Rest.new('sdkfile', 'tester', 'grjxv2mxELR3')
@file = File.expand_path('../upyun.jpg', __FILE__)
@str = 'This is a binary string, not a file'
end

describe ".endpoint=" do
it "known ENDPOINT, should return ok" do
@upyun.endpoint = Upyun::ED_CMCC
expect(@upyun.endpoint).to eq 'v3.api.upyun.com'
end

it "unknown ENDPOINT, should raise ArgumentError" do
expect {@upyun.endpoint = 'v5.api.upyun.com'}.
to raise_error(ArgumentError, /Valid endpoints/)
end

after { @upyun.endpoint = Upyun::ED_AUTO }
end

describe ".put" do
before { @path = "/ruby-sdk/foo/test.jpg" }
before { @path = '/ruby-sdk/foo/test.jpg' }

it "PUT a file" do
expect(@upyun.put(@path, @file)).to be true
end
Expand All @@ -19,10 +34,10 @@

it "PUT with some extra process headers" do
headers = {
"Contetn-type" => "image/jpeg",
"x-gmkerl-type" => "fix_width",
"x-gmkerl-value" => 42,
"x-gmkerl-unsharp" => true
'Contetn-type' => 'image/jpeg',
'x-gmkerl-type' => 'fix_width',
'x-gmkerl-value' => 42,
'x-gmkerl-unsharp' => true
}
expect(@upyun.put(@path, @file, headers)).to be true
end
Expand All @@ -31,33 +46,53 @@
end

describe ".get" do
before do
@path = "/ruby-sdk/foo/test.jpg"
@upyun.put(@path, @str, {"Content-Type" => "text/plain"})
before :all do
@path = '/ruby-sdk/foo/test.jpg'
@upyun.put(@path, @str, {'Content-Type' => 'text/plain'})
end

it "GET a file" do
expect(@upyun.get(@path)).to eq(@str)
end

it "GET a file and save" do
expect(@upyun.get(@path, "./save.jpg")).not_to eq(404)
expect(File.exists?("./save.jpg")).to be true
expect(File.read("./save.jpg")).to eq(@str)
File.delete("./save.jpg")
expect(@upyun.get(@path, './save.jpg')).not_to eq(404)
expect(File.exists?('./save.jpg')).to be true
expect(File.read('./save.jpg')).to eq(@str)
File.delete('./save.jpg')
end

it "GET a not-exist file" do
res = @upyun.get("/ruby-sdk/foo/test-not-exist.jpg")
res = @upyun.get('/ruby-sdk/foo/test-not-exist.jpg')
expect(res.is_a?(Hash) && res[:error][:code] == 404)
end

after { @upyun.delete(@path) }
after(:all) { @upyun.delete(@path) }
end

describe ".getinfo" do
before :all do
@dir = '/ruby-sdk/foo'
@path = "#{@dir}/test.jpg"
@upyun.put(@path, @str, {'Content-Type' => 'text/plain'})
end

it "of file should success" do
res = @upyun.getinfo(@path)
expect(res[:file_type]).to eq('file')
end

it "of folder should success" do
res = @upyun.getinfo(@dir)
expect(res[:file_type]).to eq('folder')
end

after(:all) { @upyun.delete(@path) }
end

describe ".delete" do
before do
@path = "/ruby-sdk/foo/test.jpg"
@path = '/ruby-sdk/foo/test.jpg'
@upyun.put(@path, @file)
end

Expand All @@ -67,27 +102,23 @@
end

describe ".mkdir" do
before :all do
@path = "/ruby-skd/foo/dir"
end
before(:all) { @path = '/ruby-skd/foo/dir' }

it "should success" do
expect(@upyun.mkdir(@path)).to be true
end

after :all do
@upyun.delete(@path)
end
after(:all) { @upyun.delete(@path) }
end

describe ".getlist" do
it "should ok" do
it "should get a list of file record" do
expect(@upyun.getlist("/")).to be_instance_of(Array)
end
end

describe ".usage" do
it "get space" do
it "should be an Fixnum" do
expect(@upyun.usage).to be_instance_of(Fixnum)
end
end
Expand All @@ -96,7 +127,21 @@
describe "Form Upload" do
before :all do
@form = Upyun::Form.new('ESxWIoMmF39nSDY7CSFUsC7s50U=', 'sdkfile')
@file = File.expand_path("../upyun.jpg", __FILE__)
@file = File.expand_path('../upyun.jpg', __FILE__)
end

describe ".endpoint=" do
it "known ENDPOINT, should return ok" do
@form.endpoint = Upyun::ED_CMCC
expect(@form.endpoint).to eq 'v3.api.upyun.com'
end

it "unknown ENDPOINT, should raise ArgumentError" do
expect {@form.endpoint = 'v5.api.upyun.com'}.
to raise_error(ArgumentError, /Valid endpoints/)
end

after { @form.endpoint = Upyun::ED_AUTO }
end

describe ".upload" do
Expand Down Expand Up @@ -137,13 +182,13 @@
expect(res.headers.key?(:location)).to be true
end

it "set 'notify-url' should return 200 success", current: true do
it "set 'notify-url' should return 200 success" do
res = @form.upload(@file, {'notify-url' => 'http://www.example.com'})
expect(res).to be_instance_of(Hash)
expect(res[:code]).to eq(200)
end

it "set 'notify-url' and handle failed, should return 403 failed", current: true do
it "set 'notify-url' and handle failed, should return 403 failed" do
opts = {
'image-width-range' => '0,10',
'notify-url' => 'http://www.example.com'
Expand Down

0 comments on commit 6a468c7

Please sign in to comment.