From 6a468c7f1162e9861b8fe38f248e0996c1e589dc Mon Sep 17 00:00:00 2001 From: jsvisa Date: Mon, 17 Nov 2014 22:40:40 +0800 Subject: [PATCH] DRY: move the repeated code to a Utils module --- .rspec | 2 + lib/upyun.rb | 1 + lib/upyun/form.rb | 15 ++----- lib/upyun/rest.rb | 12 +----- lib/upyun/utils.rb | 19 ++++++++ spec/upyun_spec.rb | 105 ++++++++++++++++++++++++++++++++------------- 6 files changed, 102 insertions(+), 52 deletions(-) create mode 100644 .rspec create mode 100644 lib/upyun/utils.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..9d52c56 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format doc diff --git a/lib/upyun.rb b/lib/upyun.rb index 2862f38..b900b9f 100644 --- a/lib/upyun.rb +++ b/lib/upyun.rb @@ -1,4 +1,5 @@ require 'upyun/version' +require 'upyun/utils' require 'upyun/rest' require 'upyun/form' diff --git a/lib/upyun/form.rb b/lib/upyun/form.rb index 4ac23ea..2b72634 100644 --- a/lib/upyun/form.rb +++ b/lib/upyun/form.rb @@ -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 @@ -31,7 +32,7 @@ class Form ext-param ) - attr_accessor :endpoint, :bucket, :password + attr_accessor :bucket, :password def initialize(password, bucket) @password = password @@ -39,14 +40,6 @@ def initialize(password, 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, @@ -81,7 +74,7 @@ def policy(opts) end def signature - Digest::MD5.hexdigest("#{@_policy}&#{@password}") + md5("#{@_policy}&#{@password}") end def policy_json(opts) diff --git a/lib/upyun/rest.rb b/lib/upyun/rest.rb index ae2fdef..0163997 100644 --- a/lib/upyun/rest.rb +++ b/lib/upyun/rest.rb @@ -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 @@ -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") @@ -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 diff --git a/lib/upyun/utils.rb b/lib/upyun/utils.rb new file mode 100644 index 0000000..35e3496 --- /dev/null +++ b/lib/upyun/utils.rb @@ -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 diff --git a/spec/upyun_spec.rb b/spec/upyun_spec.rb index cfc365a..8401ebe 100644 --- a/spec/upyun_spec.rb +++ b/spec/upyun_spec.rb @@ -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 @@ -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 @@ -31,9 +46,9 @@ 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 @@ -41,23 +56,43 @@ 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 @@ -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 @@ -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 @@ -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'