Class: MyBitcasa::Connection

Inherits:
Faraday::Connection
  • Object
show all
Defined in:
lib/my_bitcasa/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user: nil, password: nil, multipart: false) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(user: nil, password: nil, multipart: false)
  super(:url => 'https://my.bitcasa.com') do |conn|
    conn.use FaradayMiddleware::FollowRedirects
    if multipart
      conn.request :multipart
    else
      conn.request :url_encoded
    end
    #conn.response :logger
    conn.response :my_bitcasa
    conn.adapter Faraday.default_adapter
  end 
  @headers[:user_agent] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:10.0.2) Gecko/20100101 Firefox/10.0.2"

  yield self if block_given?

  (user, password) if user && password
ensure
  #ObjectSpace.define_finalizer(self) { logout! }
end

Instance Attribute Details

Returns the value of attribute cookie.



14
15
16
# File 'lib/my_bitcasa/connection.rb', line 14

def cookie
  @cookie
end

#login_engineObject



37
38
39
# File 'lib/my_bitcasa/connection.rb', line 37

def 
  @login_engine ||= LoginEngine.autodetect.new
end

Class Method Details

.uri_encode(path) ⇒ Object



97
98
99
# File 'lib/my_bitcasa/connection.rb', line 97

def uri_encode(path)
  URI.encode(path).gsub("[", "%5B").gsub("]", "%5D")
end

Instance Method Details

#loggedin?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/my_bitcasa/connection.rb', line 46

def loggedin?
  !!@cookie
end

#login(user, password) ⇒ Object



41
42
43
44
# File 'lib/my_bitcasa/connection.rb', line 41

def (user, password)
  .(user, password)
  @cookie = .cookie
end

#logout!Object



50
51
52
53
54
55
# File 'lib/my_bitcasa/connection.rb', line 50

def logout!
  if loggedin?
    self.get("/logout")
    @cookie = nil
  end
end

#multipartObject



78
79
80
81
82
# File 'lib/my_bitcasa/connection.rb', line 78

def multipart
  @multipart ||= self.class.new(multipart: true)
  @multipart.cookie = self.cookie
  @multipart
end