Module: BtCommunicator

Includes:
HTTParty
Included in:
BtSync, BtSync::Directory
Defined in:
lib/btsync/communicator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



4
5
6
# File 'lib/btsync/communicator.rb', line 4

def opts
  @opts
end

Instance Method Details

#authObject



32
33
34
# File 'lib/btsync/communicator.rb', line 32

def auth
  { username: user, password: password }
end

#cookiesObject



49
50
51
# File 'lib/btsync/communicator.rb', line 49

def cookies
  @cookies ||= request_token.headers['set-cookie'].split('; ')[0]
end

#generate_secretObject



7
8
9
10
# File 'lib/btsync/communicator.rb', line 7

def generate_secret
  res = get(path('generatesecret'))
  res.parsed_response['secret']
end

#get(path, opts = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/btsync/communicator.rb', line 57

def get(path, opts = {})
  opts = {
    headers: {
      'Cookie' => cookies
    }, query: {

    }, basic_auth: auth }.merge(opts)
  self.class.get(path, opts)
end

#passwordObject



28
29
30
# File 'lib/btsync/communicator.rb', line 28

def password
  @opts[:password]
end

#path(action_name) ⇒ Object



86
87
88
# File 'lib/btsync/communicator.rb', line 86

def path(action_name)
  "#{root_url}gui/?token=#{token}&action=#{action_name}"
end

#portObject



12
13
14
# File 'lib/btsync/communicator.rb', line 12

def port
  @opts[:port]
end

#protocolObject



20
21
22
# File 'lib/btsync/communicator.rb', line 20

def protocol
  @opts[:protocol]
end

#request_token(force = false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/btsync/communicator.rb', line 67

def request_token(force = false)
  @last_request ||= 0
  t = DateTime.now.strftime('%s').to_i
  if @request_token.nil? || force || (@last_request + 600) < t
    @last_request = t
    @request_token = self.class.get(token_path, basic_auth: auth)
  else
    @request_token
  end
end

#root_urlObject



53
54
55
# File 'lib/btsync/communicator.rb', line 53

def root_url
  "#{starter}#{uri}:#{port}/"
end

#starterObject



82
83
84
# File 'lib/btsync/communicator.rb', line 82

def starter
  "#{protocol}://"
end

#token(force = false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/btsync/communicator.rb', line 36

def token(force = false)
  @token_cache ||= 0
  time = DateTime.now.strftime('%s').to_i
  if time > @token_cache + 600 || force
    @token = request_token(force).
      gsub('</div></html>', '').
      gsub("<html><div id='token' style='display:none;'>", '')
    @token_cache = time
  end
  @cookies = nil if force
  @token
end

#token_pathObject



78
79
80
# File 'lib/btsync/communicator.rb', line 78

def token_path
  "#{root_url}gui/token.html"
end

#uriObject



16
17
18
# File 'lib/btsync/communicator.rb', line 16

def uri
  @opts[:uri]
end

#userObject



24
25
26
# File 'lib/btsync/communicator.rb', line 24

def user
  @opts[:user]
end