wialon_api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +1 -0
- data/.travis.yml +4 -0
- data/.yardopts +3 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +4 -0
- data/Guardfile +22 -0
- data/LICENSE.txt +22 -0
- data/README.md +170 -0
- data/Rakefile +13 -0
- data/bin/rspec +16 -0
- data/lib/generators/wialon_api/install/USAGE +0 -0
- data/lib/generators/wialon_api/install/install_generator.rb +10 -0
- data/lib/generators/wialon_api/install/templates/initializer.rb +30 -0
- data/lib/wialon_api/api.rb +24 -0
- data/lib/wialon_api/authorization.rb +9 -0
- data/lib/wialon_api/client.rb +29 -0
- data/lib/wialon_api/configuration.rb +54 -0
- data/lib/wialon_api/error.rb +36 -0
- data/lib/wialon_api/logger.rb +36 -0
- data/lib/wialon_api/method.rb +23 -0
- data/lib/wialon_api/namespace.rb +36 -0
- data/lib/wialon_api/namespaces.yml +14 -0
- data/lib/wialon_api/resolvable.rb +20 -0
- data/lib/wialon_api/resolver.rb +34 -0
- data/lib/wialon_api/result.rb +20 -0
- data/lib/wialon_api/version.rb +3 -0
- data/lib/wialon_api.rb +23 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/wialon_api/api_spec.rb +41 -0
- data/spec/wialon_api/authorization_spec.rb +20 -0
- data/spec/wialon_api/client_spec.rb +23 -0
- data/spec/wialon_api/configuration_spec.rb +35 -0
- data/spec/wialon_api/error_spec.rb +12 -0
- data/spec/wialon_api/logger_spec.rb +103 -0
- data/spec/wialon_api/method_spec.rb +44 -0
- data/spec/wialon_api/namespace_spec.rb +51 -0
- data/spec/wialon_api/resolvable_spec.rb +19 -0
- data/spec/wialon_api/resolver_spec.rb +50 -0
- data/spec/wialon_api/result_spec.rb +92 -0
- data/spec/wialon_api_spec.rb +7 -0
- data/wialon_api.gemspec +35 -0
- metadata +269 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cd519972faecf419d0180f12009d82e1ba6e5ed3
|
4
|
+
data.tar.gz: ab634f19f8dadb0f25c3095eeec8b4b1e72a5830
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d1e5a3a53d4b092302901232efdcb1150aea0b24f856703dee84927af98343d3daa45ce11d7822eb995976d4954e3e1f12f12ada7acac3320e01249d242ce060
|
7
|
+
data.tar.gz: b6a87a752eb5838441fde82c38725cc1ff76044aaca8e1b218635865b2121a7d38e549f8b4eb9dcb4c7dd0f0c57191284bd52338d553e36977d173845ddab0d6
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format=documentation
|
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
guard :rspec, cmd: "bin/rspec" do
|
2
|
+
require "guard/rspec/dsl"
|
3
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
4
|
+
|
5
|
+
# RSpec files
|
6
|
+
rspec = dsl.rspec
|
7
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
8
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
9
|
+
watch(rspec.spec_files)
|
10
|
+
|
11
|
+
# Ruby files
|
12
|
+
ruby = dsl.ruby
|
13
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
14
|
+
end
|
15
|
+
|
16
|
+
guard 'yard' do
|
17
|
+
watch(%r{app/.+\.rb})
|
18
|
+
watch(%r{lib/.+\.rb})
|
19
|
+
watch(%r{ext/.+\.c})
|
20
|
+
watch(%r{README.md})
|
21
|
+
watch(%r{CHANGELOG.md})
|
22
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Arsen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
# WialonApi
|
2
|
+
[![Build Status](https://travis-ci.org/thorn/wialon_api.svg?branch=master)](https://travis-ci.org/thorn/wialon_api)
|
3
|
+
[![Join the chat at https://gitter.im/thorn/wialon_api](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/thorn/wialon_api?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
4
|
+
|
5
|
+
`wialon_api` is a ruby client for a [Wialon](http://wialon.com/) web-based GPS tracking software platform [API](http://sdk.wialon.com/).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'wialon_api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install wialon_api
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Api method call
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# The first thing to do is to authorize client
|
29
|
+
client = WialonApi.authorize("wialon_test", "test")
|
30
|
+
|
31
|
+
# Now we can use API
|
32
|
+
client.core.get_account_data(type: 1)
|
33
|
+
# => {"plan"=>"Gurtam External",
|
34
|
+
# "enabled"=>1,
|
35
|
+
# "flags"=>16,
|
36
|
+
# "created"=>1362386585,
|
37
|
+
# "balance"=>"0.00",
|
38
|
+
# "daysCounter"=>0,
|
39
|
+
# "services"=>
|
40
|
+
# ....
|
41
|
+
|
42
|
+
# API calls with nested params could be implemented as follows
|
43
|
+
client.core.search_items({
|
44
|
+
"spec" => {
|
45
|
+
"itemsType" => "avl_unit",
|
46
|
+
"propName" => "sys_name",
|
47
|
+
"propValueMask" => "*",
|
48
|
+
"sortType" => "sys_name"
|
49
|
+
},
|
50
|
+
"force" => 1,
|
51
|
+
"flags" => 0x3FFFFFFFFFFFFFFF,
|
52
|
+
"from" => 0,
|
53
|
+
"to" => 0
|
54
|
+
})
|
55
|
+
|
56
|
+
=> {"searchSpec"=>{"itemsType"=>"avl_unit", "propName"=>"sys_name", "propValueMask"=>"*", "sortType"=>"sys_name", "propType"=>""},
|
57
|
+
# "dataFlags"=>4611686018427387903,
|
58
|
+
# "totalItemsCount"=>27,
|
59
|
+
# "indexFrom"=>0,
|
60
|
+
# "indexTo"=>0,
|
61
|
+
# "items"=>
|
62
|
+
# [{"nm"=>"_TK102",
|
63
|
+
# "cls"=>2,
|
64
|
+
# "id"=>12417697,
|
65
|
+
# "prp"=>{},
|
66
|
+
# "crt"=>717313,
|
67
|
+
# ..........
|
68
|
+
```
|
69
|
+
|
70
|
+
`wialon_api` uses a list of namespaces from API documentation:
|
71
|
+
|
72
|
+
* core
|
73
|
+
* items
|
74
|
+
* user
|
75
|
+
* resource
|
76
|
+
* account
|
77
|
+
* unit
|
78
|
+
* unit_group
|
79
|
+
* retranslator
|
80
|
+
* route
|
81
|
+
* messages
|
82
|
+
* report
|
83
|
+
* exchange
|
84
|
+
* render
|
85
|
+
* file
|
86
|
+
|
87
|
+
### Error handling
|
88
|
+
|
89
|
+
When wialon server returns an error, the WialonApi::Error exception is raised
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
[1] pry(main)> WialonApi.authorize("wrong_user", "password")
|
93
|
+
WialonApi::Error: Wialon server https://hst-api.wialon.com/wialon/ajax.html returned error 8: Invalid user name or password
|
94
|
+
```
|
95
|
+
|
96
|
+
### Logging
|
97
|
+
|
98
|
+
`wialon_api` logs information in `STDOUT` by default. This can be changed in configuration to any other logger e.g. `Rails.logger`.
|
99
|
+
|
100
|
+
Three types of information could be logged:
|
101
|
+
|
102
|
+
| | configuration key | default value | log level |
|
103
|
+
| ---------------------- | --------------- | ------------ | --------- |
|
104
|
+
| URL Request | `log_requests` | `true` | `debug` |
|
105
|
+
| error response JSON | `log_errors` | `true` | `warn` |
|
106
|
+
| success response JSON | `log_responses` | `false` | `debug` |
|
107
|
+
|
108
|
+
In a rails applications with default settings in production mode only server
|
109
|
+
response errors are logged. In development mode the gem logs server responses
|
110
|
+
and request URLs.
|
111
|
+
|
112
|
+
### Configuration
|
113
|
+
|
114
|
+
Global parameters could be set in a block of `WialonApi.configure`:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
WialonApi.configure do |config|
|
118
|
+
# Faraday adapter to make requests with:
|
119
|
+
# config.adapter = :net_http
|
120
|
+
|
121
|
+
# Faraday connection options
|
122
|
+
# config.faraday_options = {}
|
123
|
+
|
124
|
+
# HTTP verb for API methods (:get or :post)
|
125
|
+
# config.http_verb = :post
|
126
|
+
|
127
|
+
# Number of retries when connection is failed
|
128
|
+
# config.max_retries = 1
|
129
|
+
|
130
|
+
|
131
|
+
# Logging parameters:
|
132
|
+
# log everything through the rails logger
|
133
|
+
config.logger = Rails.logger
|
134
|
+
|
135
|
+
# log requests' URLs
|
136
|
+
# config.log_requests = true
|
137
|
+
|
138
|
+
# log response JSON after errors
|
139
|
+
# config.log_errors = true
|
140
|
+
|
141
|
+
# log response JSON after successful responses
|
142
|
+
# config.log_responses = false
|
143
|
+
|
144
|
+
# Wialon server host
|
145
|
+
# config.wialon_host = 'https://hst-api.wialon.com/wialon/ajax.html'
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
`Net::HTTP` is used by default for a HTTP requests. One can choose any [other adapter](https://github.com/technoweenie/faraday/blob/master/lib/faraday/adapter.rb) suported by `faraday`.
|
150
|
+
|
151
|
+
Options for faraday connection (e.g. proxy settings or SSL certificates path) could be set through `faraday_options` when configuring `wialon_api`
|
152
|
+
|
153
|
+
The default configuration could be generated in a Rails application using `wialon_api:install` command:
|
154
|
+
|
155
|
+
``` sh
|
156
|
+
$ cd /path/to/app
|
157
|
+
$ rails generate wialon_api:install
|
158
|
+
```
|
159
|
+
## TODO
|
160
|
+
|
161
|
+
* Implement methods from "Other request" documentation section
|
162
|
+
* Add documentation
|
163
|
+
|
164
|
+
## Contributing
|
165
|
+
|
166
|
+
1. Fork it ( https://github.com/[my-github-username]/wialon_api/fork )
|
167
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
168
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
169
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
170
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
desc 'Fires up the console with preloaded wialon_api'
|
4
|
+
task :console do
|
5
|
+
sh 'pry -I ./lib -r ./lib/wialon_api'
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
RSpec::Core::RakeTask.new do |t|
|
10
|
+
t.rspec_opts = '--color --format doc'
|
11
|
+
end
|
12
|
+
|
13
|
+
task default: :spec
|
data/bin/rspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rspec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rspec-core', 'rspec')
|
File without changes
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# A rails generator `vkontakte_api:install`.
|
2
|
+
# It creates a config file in `config/initializers/vkontakte_api.rb`.
|
3
|
+
class WialonApi::InstallGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
# Creates the config file.
|
7
|
+
def create_initializer
|
8
|
+
copy_file 'initializer.rb', 'config/initializers/wialon_api.rb'
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
WialonApi.configure do |config|
|
2
|
+
# Faraday adapter to make requests with:
|
3
|
+
# config.adapter = :net_http
|
4
|
+
|
5
|
+
# Faraday connection options
|
6
|
+
# config.faraday_options = {}
|
7
|
+
|
8
|
+
# HTTP verb for API methods (:get or :post)
|
9
|
+
# config.http_verb = :post
|
10
|
+
|
11
|
+
# Number of retries when connection is failed
|
12
|
+
# config.max_retries = 1
|
13
|
+
|
14
|
+
|
15
|
+
# Logging parameters:
|
16
|
+
# log everything through the rails logger
|
17
|
+
config.logger = Rails.logger
|
18
|
+
|
19
|
+
# log requests' URLs
|
20
|
+
# config.log_requests = true
|
21
|
+
|
22
|
+
# log response JSON after errors
|
23
|
+
# config.log_errors = true
|
24
|
+
|
25
|
+
# log response JSON after successful responses
|
26
|
+
# config.log_responses = false
|
27
|
+
|
28
|
+
# Wialon server host
|
29
|
+
# config.wialon_host = 'https://hst-api.wialon.com/wialon/ajax.html'
|
30
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module WialonApi
|
4
|
+
module Api
|
5
|
+
def self.call(service_name, args = {}, sid = nil)
|
6
|
+
parameters = { svc: service_name, params: args.to_json, sid: sid }
|
7
|
+
connection(url: WialonApi.wialon_host, sid: sid).send(WialonApi.http_verb, WialonApi.wialon_host, parameters).body
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.connection(options = {})
|
11
|
+
url = options.delete(:url)
|
12
|
+
Faraday.new(url, WialonApi.faraday_options) do |builder|
|
13
|
+
builder.request :url_encoded
|
14
|
+
builder.request :retry, WialonApi.max_retries
|
15
|
+
|
16
|
+
builder.response :wialon_logger
|
17
|
+
builder.response :mashify
|
18
|
+
builder.response :oj, preserve_raw: true
|
19
|
+
|
20
|
+
builder.adapter WialonApi.adapter
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module WialonApi
|
2
|
+
class Client
|
3
|
+
include WialonApi::Resolver
|
4
|
+
|
5
|
+
attr_reader :sid
|
6
|
+
|
7
|
+
def initialize(sid = nil)
|
8
|
+
@sid = sid
|
9
|
+
end
|
10
|
+
|
11
|
+
def authorized?
|
12
|
+
!@sid.nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
def execute(*args)
|
16
|
+
call_method(*args)
|
17
|
+
end
|
18
|
+
|
19
|
+
# If the called method is a namespace, it creates and returns a new `WialonApi::Namespace` instance.
|
20
|
+
# Otherwise it creates a `WialonApi::Method` instance and calls it passing the arguments and a block.
|
21
|
+
def method_missing(*args, &block)
|
22
|
+
if Namespace.exists?(args.first)
|
23
|
+
create_namespace(args.first)
|
24
|
+
else
|
25
|
+
call_method(args, &block)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module WialonApi
|
4
|
+
module Configuration
|
5
|
+
OPTION_NAMES = [
|
6
|
+
:wialon_host,
|
7
|
+
:http_verb,
|
8
|
+
:max_retries,
|
9
|
+
:faraday_options,
|
10
|
+
:adapter,
|
11
|
+
:logger,
|
12
|
+
:log_requests,
|
13
|
+
:log_errors,
|
14
|
+
:log_responses
|
15
|
+
]
|
16
|
+
|
17
|
+
attr_accessor(*OPTION_NAMES)
|
18
|
+
|
19
|
+
alias_method :log_requests?, :log_requests
|
20
|
+
alias_method :log_errors?, :log_errors
|
21
|
+
alias_method :log_responses?, :log_responses
|
22
|
+
|
23
|
+
DEFAULT_WIALON_HOST = 'https://hst-api.wialon.com/wialon/ajax.html'
|
24
|
+
DEFAULT_HTTP_VERB = :post
|
25
|
+
DEFAULT_MAX_RETRIES = 1
|
26
|
+
DEFAULT_ADAPTER = Faraday.default_adapter
|
27
|
+
DEFAULT_LOGGER_OPTIONS = {
|
28
|
+
requests: true,
|
29
|
+
errors: true,
|
30
|
+
responses: false
|
31
|
+
}
|
32
|
+
|
33
|
+
def configure
|
34
|
+
yield self if block_given?
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def reset
|
39
|
+
@wialon_host = DEFAULT_WIALON_HOST
|
40
|
+
@http_verb = DEFAULT_HTTP_VERB
|
41
|
+
@max_retries = DEFAULT_MAX_RETRIES
|
42
|
+
@faraday_options = {}
|
43
|
+
@adapter = DEFAULT_ADAPTER
|
44
|
+
@logger = ::Logger.new(STDOUT)
|
45
|
+
@log_requests = DEFAULT_LOGGER_OPTIONS[:requests]
|
46
|
+
@log_errors = DEFAULT_LOGGER_OPTIONS[:errors]
|
47
|
+
@log_responses = DEFAULT_LOGGER_OPTIONS[:responses]
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.extended(base)
|
51
|
+
base.reset
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module WialonApi
|
3
|
+
class Error < StandardError
|
4
|
+
attr_reader :error_code
|
5
|
+
attr_reader :error_messages
|
6
|
+
attr_reader :message
|
7
|
+
|
8
|
+
def initialize(data)
|
9
|
+
@error_code = data.error
|
10
|
+
@message = "Wialon server #{WialonApi.wialon_host} returned error #{error_code}: #{error_descriptions[@error_code.to_s]}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def error_descriptions
|
14
|
+
{
|
15
|
+
'0' => 'Successful operation (for example for logout it will be success exit)',
|
16
|
+
'1' => 'Invalid session',
|
17
|
+
'2' => 'Invalid service name',
|
18
|
+
'3' => 'Invalid result',
|
19
|
+
'4' => 'Invalid input',
|
20
|
+
'5' => 'Error performing request',
|
21
|
+
'6' => 'Unknown error',
|
22
|
+
'7' => 'Access denied',
|
23
|
+
'8' => 'Invalid user name or password',
|
24
|
+
'9' => 'Authorization server is unavailable',
|
25
|
+
'10' => 'Reached limit of concurrent requests',
|
26
|
+
'1001' => 'No messages for selected interval',
|
27
|
+
'1002' => 'Item with such unique property already exists or Item cannot be created according to billing restrictions',
|
28
|
+
'1003' => 'Only one request is allowed at the moment',
|
29
|
+
'2014' => 'Selected user is a creator for some system objects, thus this user cannot be bound to a new account'
|
30
|
+
}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module WialonApi
|
2
|
+
# Faraday middleware for logging requests and responses.
|
3
|
+
#
|
4
|
+
# It's behaviour depends on the logging options in the configuration.
|
5
|
+
class Logger < Faraday::Response::Middleware
|
6
|
+
# Creates a middleware instance.
|
7
|
+
# The logger is set from `:logger` configuration option.
|
8
|
+
def initialize(app)
|
9
|
+
super(app)
|
10
|
+
@logger = WialonApi.logger
|
11
|
+
end
|
12
|
+
|
13
|
+
# Logs the request if needed.
|
14
|
+
# @param [Hash] env Request data.
|
15
|
+
def call(env)
|
16
|
+
if WialonApi.log_requests?
|
17
|
+
@logger.debug "#{env[:method].to_s.upcase} #{env[:url]}"
|
18
|
+
@logger.debug "body: #{env[:body].inspect}" unless env[:method] == :get
|
19
|
+
end
|
20
|
+
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
24
|
+
# Logs the response (successful or not) if needed.
|
25
|
+
# @param [Hash] env Response data.
|
26
|
+
def on_complete(env)
|
27
|
+
if env[:body].error?
|
28
|
+
@logger.warn env[:raw_body] if WialonApi.log_errors?
|
29
|
+
else
|
30
|
+
@logger.debug env[:raw_body] if WialonApi.log_responses?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Faraday::Response.register_middleware wialon_logger: Logger
|
36
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module WialonApi
|
2
|
+
# An API method. It is responsible for generating it's full name and determining it's type.
|
3
|
+
class Method
|
4
|
+
include Resolvable
|
5
|
+
|
6
|
+
# A pattern for names of methods with a boolean result.
|
7
|
+
PREDICATE_NAMES = /^is.*\?$/
|
8
|
+
|
9
|
+
# Calling the API method.
|
10
|
+
# It delegates the network request to `API.call` and result processing to `Result.process`.
|
11
|
+
# @param [Hash] args Arguments for the API method.
|
12
|
+
def call(args = {}, &block)
|
13
|
+
response = Api.call(full_name, args, sid)
|
14
|
+
Result.process(response, block)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def full_name
|
20
|
+
[@previous_resolver.name, @name].join('/')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module WialonApi
|
2
|
+
# An API method namespace (such as `users` or `friends`).
|
3
|
+
#
|
4
|
+
# It includes `Resolvable` and `Resolver` and calls API methods via `Resolver#call_method`.
|
5
|
+
# It also holds the list of all known namespaces.
|
6
|
+
class Namespace
|
7
|
+
include Resolvable
|
8
|
+
include Resolver
|
9
|
+
|
10
|
+
# Creates and calls the `WialonApi::Method` using `WialonApi::Resolver#call_method`.
|
11
|
+
def method_missing(*args, &block)
|
12
|
+
call_method(args, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
# An array of all method namespaces.
|
17
|
+
#
|
18
|
+
# Lazily loads the list from `namespaces.yml` and caches it.
|
19
|
+
# @return [Array] An array of strings
|
20
|
+
def names
|
21
|
+
if @names.nil?
|
22
|
+
filename = File.expand_path('../namespaces.yml', __FILE__)
|
23
|
+
@names = YAML.load_file(filename) || []
|
24
|
+
end
|
25
|
+
|
26
|
+
@names
|
27
|
+
end
|
28
|
+
|
29
|
+
# Does a given namespace exist?
|
30
|
+
# @param [String, Symbol] name
|
31
|
+
def exists?(name)
|
32
|
+
names.include?(name.to_s)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module WialonApi
|
2
|
+
# A mixin for classes that will be resolved via `#method_missing`.
|
3
|
+
module Resolvable
|
4
|
+
attr_reader :name
|
5
|
+
|
6
|
+
# Creates a resolvable object keeping it's name and the object that resolved it.
|
7
|
+
# @param [String] name The name of this resolvable.
|
8
|
+
# @option options [Hashie::Mash] :resolver A mash holding information about the previous resolver.
|
9
|
+
def initialize(name, options = {})
|
10
|
+
@name = name.to_s
|
11
|
+
@previous_resolver = options.delete(:resolver)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns the sid from the previous resolver.
|
15
|
+
# @return [String] A sid.
|
16
|
+
def sid
|
17
|
+
@previous_resolver.sid
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module WialonApi
|
2
|
+
# A mixin for classes that will resolve other classes' objects via `#method_missing`.
|
3
|
+
module Resolver
|
4
|
+
# A `Hashie::Mash` structure holding the name and sid of current instance.
|
5
|
+
# @return [Hashie::Mash]
|
6
|
+
def resolver
|
7
|
+
@resolver ||= Hashie::Mash.new(name: @name, sid: sid)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def create_namespace(name)
|
13
|
+
WialonApi::Namespace.new(name, resolver: resolver)
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_method(name)
|
17
|
+
WialonApi::Method.new(name, resolver: resolver)
|
18
|
+
end
|
19
|
+
|
20
|
+
def call_method(args, &block)
|
21
|
+
create_method(args.shift).call(args.first || {}, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
# When this module is included, it undefines the `:send` instance method in the `base_class`
|
26
|
+
# so it can be resolved via `method_missing`.
|
27
|
+
def included(base_class)
|
28
|
+
base_class.class_eval do
|
29
|
+
undef_method :send
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|