Module: BtCommunicator
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #auth ⇒ Object
- #cookies ⇒ Object
- #generate_secret ⇒ Object
- #get(path, opts = {}) ⇒ Object
- #password ⇒ Object
- #path(action_name) ⇒ Object
- #port ⇒ Object
- #protocol ⇒ Object
- #request_token(force = false) ⇒ Object
- #root_url ⇒ Object
- #starter ⇒ Object
- #token(force = false) ⇒ Object
- #token_path ⇒ Object
- #uri ⇒ Object
- #user ⇒ Object
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
4 5 6 |
# File 'lib/btsync/communicator.rb', line 4 def opts @opts end |
Instance Method Details
#auth ⇒ Object
32 33 34 |
# File 'lib/btsync/communicator.rb', line 32 def auth { username: user, password: password } end |
#cookies ⇒ Object
49 50 51 |
# File 'lib/btsync/communicator.rb', line 49 def ||= request_token.headers['set-cookie'].split('; ')[0] end |
#generate_secret ⇒ Object
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' => }, query: { }, basic_auth: auth }.merge(opts) self.class.get(path, opts) end |
#password ⇒ Object
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 |
#port ⇒ Object
12 13 14 |
# File 'lib/btsync/communicator.rb', line 12 def port @opts[:port] end |
#protocol ⇒ Object
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_url ⇒ Object
53 54 55 |
# File 'lib/btsync/communicator.rb', line 53 def root_url "#{starter}#{uri}:#{port}/" end |
#starter ⇒ Object
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 = nil if force @token end |
#token_path ⇒ Object
78 79 80 |
# File 'lib/btsync/communicator.rb', line 78 def token_path "#{root_url}gui/token.html" end |
#uri ⇒ Object
16 17 18 |
# File 'lib/btsync/communicator.rb', line 16 def uri @opts[:uri] end |
#user ⇒ Object
24 25 26 |
# File 'lib/btsync/communicator.rb', line 24 def user @opts[:user] end |