Class: Baidupan::Base
- Inherits:
-
Object
- Object
- Baidupan::Base
- Defined in:
- lib/baidupan.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
Class Method Summary collapse
- .common_params(method, params = {}) ⇒ Object
- .get(url, params = {}, opts = {}) ⇒ Object
- .post(url, params = {}, body = {}, opts = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(url, method = :get, params = {}, body = {}, opts = {}) ⇒ Base
constructor
A new instance of Base.
- #run! ⇒ Object
Constructor Details
#initialize(url, method = :get, params = {}, body = {}, opts = {}) ⇒ Base
Returns a new instance of Base.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/baidupan.rb', line 15 def initialize(url, method=:get, params={}, body={}, opts={}) = { method: method, headers: {"User-Agent"=>"Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"}, params: params } .merge!(body: body) if body .merge!(opts) @request = Typhoeus::Request.new(url, ) @request.on_complete do |response| if response.success? if response.headers["Content-Disposition"] =~ /attachment;file/ or response.headers["Content-Type"] =~ /image\// @body = response.body else @body = MultiJson.load(response.body, symbolize_keys: true) end end end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
13 14 15 |
# File 'lib/baidupan.rb', line 13 def body @body end |
Class Method Details
.common_params(method, params = {}) ⇒ Object
49 50 51 52 |
# File 'lib/baidupan.rb', line 49 def common_params(method, params={}) params = {access_token: Config.access_token}.merge(params) params.merge!(method: method) end |
.get(url, params = {}, opts = {}) ⇒ Object
41 42 43 |
# File 'lib/baidupan.rb', line 41 def get(url, params={}, opts={}) new(url, :get, params, nil, opts).run! end |
.post(url, params = {}, body = {}, opts = {}) ⇒ Object
45 46 47 |
# File 'lib/baidupan.rb', line 45 def post(url, params={}, body={}, opts={}) new(url, :post, params, body, opts).run! end |
Instance Method Details
#run! ⇒ Object
35 36 37 38 |
# File 'lib/baidupan.rb', line 35 def run! @request.run self end |