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, params = nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
# File 'lib/synowebapi/client.rb', line 10

def initialize(url, params = nil)
  @url = url
  @conn = Faraday.new(:url => @url) do |f|
    f.adapter(Faraday.default_adapter)
    f.use(FaradayMiddleware::ParseJson)
  end
  connect(params) if params
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

#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



46
47
48
49
# File 'lib/synowebapi/client.rb', line 46

def [](api_name)
  @api ||= query_api
  @api[api_name] ? @api[api_name] : (raise ArgumentError.new("#{api_name} isn't found"))
end

#connect(params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/synowebapi/client.rb', line 19

def connect(params)
  resp = self['SYNO.API.Auth'].request(
    :method => 'login',
    :account => params[:username],
    :passwd => params[:password],
    :session => params[:session_id],
    :format => 'sid',
  )

  @session_id = resp['sid']
end

#disconnectObject



31
32
33
# File 'lib/synowebapi/client.rb', line 31

def disconnect
  self['SYNO.API.Auth'].request(:method => 'logout', :session => @session_id)
end

#send(api, params = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/synowebapi/client.rb', line 35

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