Class: SYNOWebAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/synowebapi/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#apiObject (readonly)

Returns the value of attribute api.



8
9
10
# File 'lib/synowebapi/client.rb', line 8

def api
  @api
end

#session_idObject (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_nameObject (readonly)

Returns the value of attribute session_name.



8
9
10
# File 'lib/synowebapi/client.rb', line 8

def session_name
  @session_name
end

#urlObject (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'].(
    :account => params[:username],
    :passwd => params[:password],
    :session => @session_name,
    :format => 'sid',
  )
  @session_id = resp['sid']
end

#disconnectObject



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 = {}, options = {})
  require 'httpclient'
  http_client = HTTPClient.new
  if options[:receive_timeout]
    http_client.receive_timeout = options[: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