synowebapi 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95ed9aa63ce6edb70a0cd2fdf0affa15e3f7eb85
4
+ data.tar.gz: 2ac9ba4a1b31f3963977b81b70c1584de9f58098
5
+ SHA512:
6
+ metadata.gz: e37b1cd570ae8ef30ae1cbbecfd54f1eef9a9268672592403287137ac72de4f0baaed7ce92f00b5bc89ed204a48842fea273f05a962747934c63f981d74ec29d
7
+ data.tar.gz: e31c3ecec2f461d85d378984a12f78a02c1c04d045b1f40d9c9f2586ce7270401e043027c68f40f88617f17c61100cc2d304dcb8f978e9fabb6be6c08fac40f2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # SYNOWebAPI
2
+
3
+ Ruby gem for [Synology](http://www.synology.com//) Web API
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ $ gem install synowebapi
9
+ ```
10
+
11
+ ## Synopsis
12
+
13
+ ```ruby
14
+ require 'synowebapi'
15
+
16
+ client = SYNOWebAPI::Client.new('http://192.168.10.10:5000')
17
+ client.connect(username: 'admin', password: 'password')
18
+
19
+ client['SYNO.Core.Terminal'].request(method: 'set', enable_telnet: true)
20
+ client['SYNO.Core.Terminal'].set(enable_ssh: true)
21
+
22
+ client.disconnect
23
+ ```
24
+
25
+ ## Requirements
26
+
27
+ * faraday
28
+ * faraday-middleware
29
+
30
+ ## License
31
+
32
+ http://cccheng.mit-license.org/
33
+
34
+ Copyright © 2015 Chung-Chiang Cheng <shepjeng@gmail.com>
35
+
36
+ Permission is hereby granted, free of charge, to any person obtaining a copy
37
+ of this software and associated documentation files (the "Software"), to deal
38
+ in the Software without restriction, including without limitation the rights
39
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
40
+ copies of the Software, and to permit persons to whom the Software is
41
+ furnished to do so, subject to the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be included in
44
+ all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
51
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
52
+ THE SOFTWARE.
53
+
data/lib/synowebapi.rb ADDED
@@ -0,0 +1,13 @@
1
+
2
+ module SYNOWebAPI
3
+ ROOT = File.expand_path(File.dirname(__FILE__))
4
+
5
+ unless $LOAD_PATH.any? { |lp| File.expand_path(lp) == ROOT }
6
+ $LOAD_PATH.unshift(ROOT)
7
+ end
8
+
9
+ require 'synowebapi/version'
10
+ require 'synowebapi/client'
11
+ require 'synowebapi/api'
12
+
13
+ end
@@ -0,0 +1,32 @@
1
+
2
+ require 'synowebapi/error'
3
+
4
+ module SYNOWebAPI
5
+ class API
6
+ include SYNOWebAPI::ErrorHandler
7
+ attr_reader :api_name, :path, :min_version, :max_version, :request_format, :methods
8
+
9
+ def initialize(api_name, client, params)
10
+ @api_name = api_name
11
+ @client = client
12
+ @path = params['path']
13
+ @min_version = params['minVersion']
14
+ @max_version = params['maxVersion']
15
+ @request_format = params['requestFormat']
16
+ end
17
+
18
+ def request(params)
19
+ resp = @client.send(self, params)
20
+
21
+ if resp['success']
22
+ resp['data']
23
+ else
24
+ error_handling(resp['error'])
25
+ end
26
+ end
27
+
28
+ def method_missing(method_name, **args, &block)
29
+ request({:method => method_name}.merge(args))
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,68 @@
1
+
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require 'synowebapi/api'
5
+
6
+ module SYNOWebAPI
7
+ class Client
8
+ attr_reader :url, :api, :session_id
9
+
10
+ def initialize(url, params = nil)
11
+ @url = url
12
+ @conn = Faraday.new(:url => @url) do |f|
13
+ f.adapter(Faraday.default_adapter)
14
+ f.use(FaradayMiddleware::ParseJson)
15
+ end
16
+ connect(params) if params
17
+ end
18
+
19
+ def connect(params)
20
+ resp = self['SYNO.API.Auth'].request(
21
+ :method => 'login',
22
+ :account => params[:username],
23
+ :passwd => params[:password],
24
+ :session => params[:session_id],
25
+ :format => 'sid',
26
+ )
27
+
28
+ @session_id = resp['sid']
29
+ end
30
+
31
+ def disconnect
32
+ self['SYNO.API.Auth'].request(:method => 'logout', :session => @session_id)
33
+ end
34
+
35
+ def send(api, params = {})
36
+ @conn.get("/webapi/#{api.path}", {
37
+ :api => api.api_name,
38
+ :version => api.max_version,
39
+ :_sid => @session_id,
40
+ }.merge(params)).body
41
+
42
+ rescue Faraday::ParsingError
43
+ raise StandardError.new("Failed to parse response")
44
+ end
45
+
46
+ def [](api_name)
47
+ @api ||= query_api
48
+ @api[api_name] ? @api[api_name] : (raise ArgumentError.new("#{api_name} isn't found"))
49
+ end
50
+
51
+ private
52
+ def query_api
53
+ resp = @conn.get('webapi/query.cgi', {
54
+ :api => 'SYNO.API.Info',
55
+ :version => 1,
56
+ :method => 'Query',
57
+ }).body
58
+
59
+ {}.tap do |api|
60
+ resp['data'].each do |api_name, params|
61
+ api[api_name] = API.new(api_name, self, params)
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+
@@ -0,0 +1,53 @@
1
+
2
+ module SYNOWebAPI
3
+
4
+ ERRNO = {
5
+ 100 => 'unknown error',
6
+ 101 => 'bad request',
7
+ 102 => 'API not found',
8
+ 103 => 'method not found',
9
+ 104 => 'unsupported version',
10
+ 105 => 'no permission',
11
+ 106 => 'session timeout',
12
+ 107 => 'session interrupted',
13
+ 108 => 'handling upload',
14
+ 109 => 'process relay',
15
+ 110 => 'process entry',
16
+ 111 => 'process lib',
17
+ 112 => 'compound stop',
18
+ 113 => 'compound reject',
19
+ 114 => 'no required parameter',
20
+ 115 => 'denied in upload mode',
21
+ 116 => 'denied in demo mode',
22
+ 117 => 'internal error',
23
+ 118 => 'process name error',
24
+ 119 => 'session not found',
25
+ 120 => 'invalid request parameter',
26
+ 121 => 'mismatched lib entry',
27
+ 122 => 'sharing session not found',
28
+ 123 => 'sharing error token',
29
+ 124 => 'sharing invalid entry',
30
+ 125 => 'sharing timeout',
31
+ 126 => 'sharing no permission',
32
+ 127 => 'sharing no app permission',
33
+ 400 => 'authentication failed',
34
+ 401 => 'account disabled',
35
+ 402 => 'permission denied',
36
+ 403 => 'OTP code required',
37
+ 404 => 'OTP code incorrect',
38
+ 405 => 'App portal incorrect',
39
+ 406 => 'OTP code enforced',
40
+ 407 => 'max tries by auto blocking',
41
+ }
42
+
43
+ module ErrorHandler
44
+ def error_handling(error)
45
+ code = error['code']
46
+ if ERRNO.has_key?(code)
47
+ raise StandardError.new(ERRNO[code])
48
+ else
49
+ raise StandardError.new(code)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+
2
+ module SYNOWebAPI
3
+ VERSION = '0.0.1'
4
+ end
5
+
@@ -0,0 +1,20 @@
1
+
2
+ require File.expand_path('../lib/synowebapi/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'synowebapi'
6
+ s.version = SYNOWebAPI::VERSION
7
+ s.date = Time.now.strftime('%Y-%m-%d')
8
+ s.authors = ['Chung-Chiang Cheng']
9
+ s.email = ['shepjeng@gmail.com', 'cccheng@synology.com']
10
+ s.summary = 'Ruby gem for Synology Web API'
11
+ s.description = s.summary
12
+ s.homepage = 'http://github.com/shepjeng/ruby-synowebapi'
13
+ s.license = 'MIT'
14
+
15
+ s.files = %x[git ls-files].split($/).sort
16
+ s.require_paths = ['lib']
17
+
18
+ s.add_dependency 'faraday', '~> 0'
19
+ s.add_dependency 'faraday_middleware', '~> 0'
20
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: synowebapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chung-Chiang Cheng
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Ruby gem for Synology Web API
42
+ email:
43
+ - shepjeng@gmail.com
44
+ - cccheng@synology.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - Gemfile
50
+ - README.md
51
+ - lib/synowebapi.rb
52
+ - lib/synowebapi/api.rb
53
+ - lib/synowebapi/client.rb
54
+ - lib/synowebapi/error.rb
55
+ - lib/synowebapi/version.rb
56
+ - synowebapi.gemspec
57
+ homepage: http://github.com/shepjeng/ruby-synowebapi
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.4.5
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Ruby gem for Synology Web API
81
+ test_files: []