Module: BtCommunicator

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

Instance Method Summary collapse

Instance Method Details

#cookiesObject



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

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

#generate_secretObject



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

def generate_secret
  res = self.class.get(path('generatesecret'), :headers => {"Cookie" => cookies })
  res.parsed_response["secret"]
end

#path(action_name) ⇒ Object



38
39
40
# File 'lib/btsync/communicator.rb', line 38

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

#portObject



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

def port
  @port
end

#request_token(force = false) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/btsync/communicator.rb', line 30

def request_token force = false
  if @request_token.nil? || force
    @request_token = self.class.get("#{root_url}gui/token.html", :query => {:output => :text})
  else
    @request_token
  end
end

#root_urlObject



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

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

#token(force = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/btsync/communicator.rb', line 14

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

#uriObject



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

def uri
  @uri
end