Class: BaiduPcs::Base
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#options ⇒ Object
Returns the value of attribute options.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
Class Method Summary collapse
-
.atoken_params ⇒ Object
common.
- .delete(url, params = {}, opts = {}) ⇒ Object
- .get(url, params = {}, opts = {}) ⇒ Object
- .method_params(buz_method = nil, params = {}) ⇒ Object
- .post(url, params = {}, body = {}, opts = {}) ⇒ Object
- .put(url, params = {}, body = {}, opts = {}) ⇒ Object
- .quota ⇒ Object
Instance Method Summary collapse
- #http_code ⇒ Object
-
#initialize(url, method = :get, params = {}, body = {}, opts = {}) ⇒ Base
constructor
A new instance of Base.
- #ok? ⇒ Boolean
- #run! ⇒ Object
Constructor Details
#initialize(url, method = :get, params = {}, body = {}, opts = {}) ⇒ Base
Returns a new instance of Base.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/baidu_pcs.rb', line 33 def initialize(url, method=:get, params={}, body={}, opts={}) [:noprogress, :verbose].each do |k| opts[k] = params.delete(k) if params.key?(k) and !opts.key?(k) end @options = { method: method||:get, headers: {"User-Agent"=>"Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"}, params: params } if headers = opts.delete(:headers) @options[:headers].merge!(headers) end @options.merge!(body: body) unless body.blank? @options.merge!(opts) @request = Typhoeus::Request.new(url, @options) if @options[:verbose] puts "#### request options: " puts @options end @request.on_complete do |response| @response = response if response.success? #(mock || return_code == :ok) && response_code && response_code >= 200 && response_code < 300 if response.headers["Content-Disposition"] =~ /attachment;file/ or response.headers["Content-Type"] =~ /image\// @body = response.body else #default as json @body = MultiJson.load(response.body, symbolize_keys: true) end elsif response.timed_out? raise "Timeout with options: #{}" elsif response.code == 0 raise "Could not get an http response, something's wrong: #{response.} with options: #{}" else raise "Http request failed with code: #{response.code}, msg: #{response.body}" end end self end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
31 32 33 |
# File 'lib/baidu_pcs.rb', line 31 def body @body end |
#options ⇒ Object
Returns the value of attribute options.
31 32 33 |
# File 'lib/baidu_pcs.rb', line 31 def @options end |
#request ⇒ Object
Returns the value of attribute request.
31 32 33 |
# File 'lib/baidu_pcs.rb', line 31 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
31 32 33 |
# File 'lib/baidu_pcs.rb', line 31 def response @response end |
Class Method Details
.atoken_params ⇒ Object
common
97 98 99 |
# File 'lib/baidu_pcs.rb', line 97 def atoken_params {access_token: Config.access_token} end |
.delete(url, params = {}, opts = {}) ⇒ Object
92 93 94 |
# File 'lib/baidu_pcs.rb', line 92 def delete(url, params={}, opts={}) new(url, :delete, params, opts).run! end |
.get(url, params = {}, opts = {}) ⇒ Object
83 84 85 |
# File 'lib/baidu_pcs.rb', line 83 def get(url, params={}, opts={}) new(url, :get, params, nil, opts).run! end |
.method_params(buz_method = nil, params = {}) ⇒ Object
100 101 102 103 104 |
# File 'lib/baidu_pcs.rb', line 100 def method_params(buz_method=nil, params={}) opts = atoken_params.merge(params) opts.merge!(method: buz_method) if buz_method opts end |
.post(url, params = {}, body = {}, opts = {}) ⇒ Object
86 87 88 |
# File 'lib/baidu_pcs.rb', line 86 def post(url, params={}, body={}, opts={}) new(url, :post, params, body, opts).run! end |
.put(url, params = {}, body = {}, opts = {}) ⇒ Object
89 90 91 |
# File 'lib/baidu_pcs.rb', line 89 def put(url, params={}, body={}, opts={}) new(url, :put, params, body, opts).run! end |
.quota ⇒ Object
105 106 107 |
# File 'lib/baidu_pcs.rb', line 105 def quota get("#{PCS_BASE_URL}/quota", method_params(:info)) end |
Instance Method Details
#http_code ⇒ Object
78 79 80 |
# File 'lib/baidu_pcs.rb', line 78 def http_code response.code end |
#ok? ⇒ Boolean
75 76 77 |
# File 'lib/baidu_pcs.rb', line 75 def ok? response.success? end |
#run! ⇒ Object
71 72 73 74 |
# File 'lib/baidu_pcs.rb', line 71 def run! @request.run self end |