Class: SYNOWebAPI::Client
- Inherits:
-
Object
- Object
- SYNOWebAPI::Client
- Defined in:
- lib/synowebapi/client.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#session_name ⇒ Object
readonly
Returns the value of attribute session_name.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #[](api_name) ⇒ Object
- #connect(params) ⇒ Object
- #disconnect ⇒ Object
- #download(api, output_path, params = {}, options = {}) ⇒ Object
-
#initialize(url) ⇒ Client
constructor
A new instance of Client.
- #post(api, params = {}) ⇒ Object
- #send(api, params = {}) ⇒ Object
Constructor Details
#initialize(url) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 |
# File 'lib/synowebapi/client.rb', line 10 def initialize(url) @url = url @session_id = @session_name = '' @conn = Faraday.new(:url => @url) do |f| f.request(:url_encoded) f.response(:json) f.adapter(Faraday.default_adapter) end end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
8 9 10 |
# File 'lib/synowebapi/client.rb', line 8 def api @api end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
8 9 10 |
# File 'lib/synowebapi/client.rb', line 8 def session_id @session_id end |
#session_name ⇒ Object (readonly)
Returns the value of attribute session_name.
8 9 10 |
# File 'lib/synowebapi/client.rb', line 8 def session_name @session_name end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
8 9 10 |
# File 'lib/synowebapi/client.rb', line 8 def url @url end |
Instance Method Details
#[](api_name) ⇒ Object
77 78 79 80 |
# File 'lib/synowebapi/client.rb', line 77 def [](api_name) @api ||= query_api @api[api_name] || (raise ArgumentError.new("WebAPI '#{api_name}' isn't found")) end |
#connect(params) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/synowebapi/client.rb', line 20 def connect(params) @session_name = params[:session_name] || @session_name resp = self['SYNO.API.Auth'].login( :account => params[:username], :passwd => params[:password], :session => @session_name, :format => 'sid', ) @session_id = resp['sid'] end |
#disconnect ⇒ Object
31 32 33 |
# File 'lib/synowebapi/client.rb', line 31 def disconnect self['SYNO.API.Auth'].logout(:session => @session_id) end |
#download(api, output_path, params = {}, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/synowebapi/client.rb', line 35 def download(api, output_path, params = {}, = {}) require 'httpclient' http_client = HTTPClient.new if [:receive_timeout] http_client.receive_timeout = [:receive_timeout] end open(output_path, 'wb') do |file| http_client.get_content( URI.parse(URI.encode("#{@url}/webapi/#{api.path}")), { :api => api.api_name, :version => api.max_version, :_sid => @session_id, }.merge(params) ) do |chunk| file.write chunk end end end |
#post(api, params = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/synowebapi/client.rb', line 55 def post(api, params = {}) @conn.post("/webapi/#{api.path}", { :api => api.api_name, :version => api.max_version, :_sid => @session_id, }.merge(params)).body rescue Faraday::ParsingError raise StandardError.new("Failed to parse response") end |
#send(api, params = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/synowebapi/client.rb', line 66 def send(api, params = {}) @conn.get("/webapi/#{api.path}", { :api => api.api_name, :version => api.max_version, :_sid => @session_id, }.merge(params)).body rescue Faraday::ParsingError raise StandardError.new("Failed to parse response") end |