class Qnap::FileStation
Constants
- API_METHODS
- DEBUG
- DISCARD_EXTRANEOUS
- PROTOCOL
Public Class Methods
new(host, username = ENV['QNAP_USERNAME'], password = ENV['QNAP_PASSWORD'])
click to toggle source
# File lib/qnap/file_station.rb, line 106 def initialize(host, username = ENV['QNAP_USERNAME'], password = ENV['QNAP_PASSWORD']) raise ArgumentError.new("No username defined") if username.nil? raise ArgumentError.new("No password defined") if password.nil? @host = host @username = username @password = password @sid = nil @base_uri = URI "#{PROTOCOL}://#{@host}" @path = "/cgi-bin/filemanager/utilRequest.cgi" @agent = Net::HTTP.new @base_uri.host, @base_uri.port @agent.use_ssl = PROTOCOL == 'https', @agent.verify_mode = OpenSSL::SSL::VERIFY_NONE @agent.keep_alive_timeout = 10 @agent.set_debug_output $stdout if DEBUG end
session(*args) { |ds| ... }
click to toggle source
# File lib/qnap/file_station.rb, line 51 def self.session(*args) ds = self.new *args begin yield ds ensure ds.logout end end
Public Instance Methods
get_sid()
click to toggle source
# File lib/qnap/file_station.rb, line 102 def get_sid @sid ||= login(user: @username, pwd: Base64.encode64(@password).strip)[:sid] end
login(params={})
click to toggle source
# File lib/qnap/file_station.rb, line 81 def login(params={}) despatch_query( "/cgi-bin/filemanager/wfm2Login.cgi", params ) end
logout(params={})
click to toggle source
# File lib/qnap/file_station.rb, line 88 def logout(params={}) return unless @sid despatch_query( "/cgi-bin/filemanager/wfm2Logout.cgi", params.merge(sid: @sid) ) @sid = nil if @agent.started? @agent.finish puts "\e[1;32mClosing connection\e[0m" if DEBUG end end
Private Instance Methods
despatch_query(path, params)
click to toggle source
# File lib/qnap/file_station.rb, line 127 def despatch_query(path, params) uri = @base_uri.clone uri.path = path uri.query = URI.encode_www_form(params).gsub('+', '%20') if params.keys.length > 0 req = Net::HTTP::Get.new uri puts "\n\n\e[1;32mDespatching request to #{params[:func]}\e[0m" if DEBUG @session = @agent.start unless @agent.started? response = @session.request req puts "\e[1;32mResponse received\e[0m" if DEBUG unless (200..299).include? response.code.to_i raise "Error response from #{uri} -> #{response.read_body}" end case response['content-type'] when /application\/json/ data = JSON.parse response.read_body, symbolize_names: true when /application\/force-download/ data = response.read_body.force_encoding('UTF-8') else raise "Don't know how to parse #{response['content-type']}" end if data.respond_to?(:key?) and data.key?(:status) and data[:status] != 1 raise Qnap::ApiError.new data[:status], uri, response end data end