Class: Baidupan::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/baidupan.rb

Direct Known Subclasses

FsCmd

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, method = :get, params = {}, body = {}, opts = {}) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/baidupan.rb', line 17

def initialize(url, method=:get, params={}, body={}, opts={})
  @options = {
    method: method,
    headers: {"User-Agent"=>"Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"},
    params: params
  }
  @options.merge!(body: body) if body
  @options.merge!(opts)
  @request = Typhoeus::Request.new(url, @options)
  @request.on_complete do |response|
    @response = 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

#bodyObject (readonly)

Returns the value of attribute body.



15
16
17
# File 'lib/baidupan.rb', line 15

def body
  @body
end

#responseObject (readonly)

Returns the value of attribute response.



15
16
17
# File 'lib/baidupan.rb', line 15

def response
  @response
end

Class Method Details

.common_params(method, params = {}) ⇒ Object



52
53
54
55
# File 'lib/baidupan.rb', line 52

def common_params(method, params={})
  params = {access_token: Config.access_token}.merge(params)
  params.merge!(method: method)
end

.get(url, params = {}, opts = {}) ⇒ Object



44
45
46
# File 'lib/baidupan.rb', line 44

def get(url, params={}, opts={})
  new(url, :get, params, nil, opts).run!
end

.post(url, params = {}, body = {}, opts = {}) ⇒ Object



48
49
50
# File 'lib/baidupan.rb', line 48

def post(url, params={}, body={}, opts={})
  new(url, :post, params, body, opts).run!
end

Instance Method Details

#run!Object



38
39
40
41
# File 'lib/baidupan.rb', line 38

def run!
  @request.run
  self
end