ruby-dovado 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c269ddd9eff2fa5106fa286c8498b084622003c4
4
- data.tar.gz: c51e6186541f708b122c5161138cbb6dd89c52aa
3
+ metadata.gz: 5e7c4e1e5304745d859175e5a713dd34a1cf0ded
4
+ data.tar.gz: 928808f3fb205fa3eb4f6850603dfc2fe04f04f1
5
5
  SHA512:
6
- metadata.gz: 905cdbe46e78f7d5765014a4a3e7d5b08c71505e9c83e987e439b46a7b6974779804b8a872e064410d4f2bb20c50aae98db0cf904c18140cf49b0592c54cc443
7
- data.tar.gz: d15460a40263fdf2f296caa06b852cc00de0ab26b6b212ad55ed6e2c76fa1d42bca8b0bfecc972bc032ca8a51ee3f9bc884e22c13f9763d50b28301c60e1b941
6
+ metadata.gz: 05c59b4500d3a2e9866d0b05c16b89dd043e33daf464ae7f7ce91bfcce7e9d1202380659caad73843976040a38170bb8243028639955641de9d7fb76de9b44b2
7
+ data.tar.gz: 8da24c1d83c61cd4e4c8ada42617ec5416146fe7a800fb68c6e586954ecd9c545c899d92adcf57fb1d29b33190e9e1cff11aa8771442ae1f1082a5494183825c
data/README.md CHANGED
@@ -3,6 +3,9 @@
3
3
  A Dovado Router API for Ruby.
4
4
 
5
5
  [![Build Status](https://drone.io/bitbucket.org/janlindblom/ruby-dovado/status.png)](https://drone.io/bitbucket.org/janlindblom/ruby-dovado/latest)
6
+ [![Gem](https://img.shields.io/gem/v/ruby-dovado.svg?style=flat-square)](https://rubygems.org/gems/ruby-dovado)
7
+ [![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg?style=flat-square)](http://www.rubydoc.info/gems/ruby-dovado/frames)
8
+ [![License](http://img.shields.io/badge/license-MIT-yellowgreen.svg?style=flat-square)](#copyright)
6
9
 
7
10
  This library serves to enable easy access to the built in, Telnet-based, rudimentary API of the [routers from Dovado](http://www.dovado.com/en/products) running software version 6 and 7 (applies to the original Tiny and Go routers, among others). It might work with software version 8 routers (the Tiny AC) too but I have no means to test against that since I don't have one of the later routers.
8
11
 
@@ -21,9 +24,9 @@ gem "ruby-dovado"
21
24
  They load it in your code:
22
25
 
23
26
  ```ruby
24
- require dovado
27
+ require "dovado"
25
28
 
26
- router = Dovado::Router.new(address: 192.168.0.1”, user: admin”, password: password)
29
+ router = Dovado::Router.new(address: "192.168.0.1", user: "admin", password: "password")
27
30
  router.info
28
31
  router.sms.load_messages
29
32
  message = router.sms.get_message 12
data/lib/dovado.rb CHANGED
@@ -3,7 +3,7 @@ require 'celluloid/current'
3
3
  # The Ruby-Dovado library.
4
4
  #
5
5
  # @author Jan Lindblom <janlindblom@fastmail.fm>
6
- # @version 1.0.1
6
+ # @version 1.0.2
7
7
  module Dovado
8
8
  end
9
9
 
@@ -12,11 +12,18 @@ require 'dovado/utilities'
12
12
  require 'dovado/client'
13
13
 
14
14
  require 'dovado/router'
15
+
15
16
  require 'dovado/router/services'
17
+
16
18
  require 'dovado/router/info'
17
19
  require 'dovado/router/info/operator'
18
20
  require 'dovado/router/info/operator/telia'
19
21
 
22
+ require 'dovado/router/internet'
23
+
24
+ require 'dovado/router/traffic/amount'
25
+ require 'dovado/router/traffic'
26
+
20
27
  require 'dovado/router/sms'
21
28
  require 'dovado/router/sms/messages'
22
29
  require 'dovado/router/sms/message'
data/lib/dovado/router.rb CHANGED
@@ -64,6 +64,24 @@ module Dovado
64
64
  router_services
65
65
  end
66
66
 
67
+ # Get the Internet Connection object.
68
+ # @since 1.0.2
69
+ # @return [Internet] the Internet Connection object.
70
+ # @see {Internet}
71
+ def internet
72
+ Internet.supervise as: :internet, size: 1 unless Actor[:internet]
73
+ Actor[:internet]
74
+ end
75
+
76
+ # Get the Data Traffic object.
77
+ # @since 1.0.2
78
+ # @return [Traffic] the Data Traffic object
79
+ # @see {Traffic}
80
+ def traffic
81
+ Traffic.supervise as: :traffic, size: 1 unless Actor[:traffic]
82
+ Actor[:traffic]
83
+ end
84
+
67
85
  # Fetch information from the router.
68
86
  #
69
87
  # @return [Info] The {Info} object.
@@ -87,6 +87,14 @@ module Dovado
87
87
  @data.keys
88
88
  end
89
89
 
90
+ # Check if the {Info} object has a given key.
91
+ #
92
+ # @param [Symbol] key the key to check for.
93
+ # @return [Boolean] +true+ or +false+
94
+ def has_key?(key)
95
+ keys.member?(key)
96
+ end
97
+
90
98
  private
91
99
 
92
100
  def touch!
@@ -0,0 +1,63 @@
1
+ module Dovado
2
+ class Router
3
+ # Internet Connection.
4
+ #
5
+ # @since 1.0.2
6
+ class Internet
7
+ include Celluloid
8
+
9
+ # Create a new {Internet} object.
10
+ def initialize
11
+ @state = ThreadSafe::Cache.new
12
+ @state[:status] = :offline
13
+ end
14
+
15
+ # Enable internet connection.
16
+ def on!
17
+ client = Actor[:client]
18
+ client.connect unless client.connected?
19
+ client.authenticate unless client.authenticated?
20
+ client.command("internet on")
21
+ status = :online
22
+ end
23
+
24
+ # Disable internet connection.
25
+ def off!
26
+ client = Actor[:client]
27
+ client.connect unless client.connected?
28
+ client.authenticate unless client.authenticated?
29
+ client.command("internet off")
30
+ status = :offline
31
+ end
32
+
33
+ # Check if the internet connection is up.
34
+ #
35
+ # @return [Boolean] +true+ if internet was enabled, false otherwise.
36
+ def on?
37
+ status == :online
38
+ end
39
+
40
+ # Check if the internet connection is down.
41
+ #
42
+ # @return [Boolean] +true+ if internet was disabled, false otherwise.
43
+ def off?
44
+ status == :offline
45
+ end
46
+
47
+ # Return the current status of the internet connection.
48
+ #
49
+ # @return [Symbol] one of: +:online+ or +:offline+
50
+ def status
51
+ @state[:status]
52
+ end
53
+
54
+ # Set the current status of the internet connection.
55
+ #
56
+ # @param [Symbol] value one of: +:online+ or +:offline+
57
+ def status=(value)
58
+ @state[:status] = value
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -57,6 +57,14 @@ module Dovado
57
57
  @list.keys
58
58
  end
59
59
 
60
+ # Check if the {Services} object has a given key.
61
+ #
62
+ # @param [Symbol] key the key to check for.
63
+ # @return [Boolean] +true+ or +false+
64
+ def has_key?(key)
65
+ keys.member?(key)
66
+ end
67
+
60
68
  # Checks if this {Services} object is still valid.
61
69
  #
62
70
  # @return [Boolean] +true+ or +false+.
@@ -0,0 +1,91 @@
1
+ require 'dovado/router/traffic/amount'
2
+
3
+ module Dovado
4
+ class Router
5
+ # Traffic Counters.
6
+ #
7
+ # @since 1.0.2
8
+ class Traffic
9
+ include Celluloid
10
+
11
+ # Create a new {Internet} object.
12
+ def initialize
13
+ @data = ThreadSafe::Cache.new
14
+ @client = Actor[:client] unless @client
15
+ @last_update = nil
16
+ @data[:down] = Traffic::Amount.new(0)
17
+ @data[:up] = Traffic::Amount.new(0)
18
+ end
19
+
20
+ # Data upload traffic amount.
21
+ #
22
+ # @return [Amount] amount of uploaded data.
23
+ def up
24
+ update!
25
+ @data[:up]
26
+ end
27
+
28
+ # Data download traffic amount.
29
+ #
30
+ # @return [Amount] amount of downloaded data.
31
+ def down
32
+ update!
33
+ @data[:down]
34
+ end
35
+
36
+ # Update this {Traffic} object.
37
+ def update!
38
+ unless valid?
39
+ begin
40
+ up, down = fetch_from_router
41
+ rescue
42
+ up, down = fetch_from_router_info
43
+ end
44
+ @data[:down] = Traffic::Amount.new(down)
45
+ @data[:up] = Traffic::Amount.new(up)
46
+ touch!
47
+ end
48
+ end
49
+
50
+ # Determine if this traffic object is valid.
51
+ #
52
+ # @return [Boolean] true or false.
53
+ def valid?
54
+ return false if @last_update.nil?
55
+ (@last_update + SecureRandom.random_number(9) + 1 <= Time.now.to_i)
56
+ end
57
+
58
+ private
59
+
60
+ def touch!
61
+ @last_update = Time.now.to_i
62
+ end
63
+
64
+ def fetch_from_router
65
+ up, down = [@data[:up], @data[:down]]
66
+ client = Actor[:client]
67
+ client.connect unless client.connected?
68
+ client.authenticate unless client.authenticated?
69
+ string = client.command('traffic')
70
+ matched = string.match(/(\d+)\W(\d+)\W\/\W(\d+)/)
71
+ if matched
72
+ up = matched[3].to_i if matched[3]
73
+ down = matched[2].to_i if matched[2]
74
+ else
75
+ up, down = fetch_from_router_info
76
+ end
77
+ [up, down]
78
+ end
79
+
80
+ def fetch_from_router_info
81
+ up, down = [@data[:up], @data[:down]]
82
+ Info.supervise as: :router_info, size: 1 unless Actor[:router_info]
83
+ router_info = Actor[:router_info]
84
+ down = router_info[:traffic_modem_rx].to_i if router_info.has_key? :traffic_modem_rx
85
+ up = router_info[:traffic_modem_rx].to_i if router_info.has_key? :traffic_modem_tx
86
+ [up, down]
87
+ end
88
+
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,55 @@
1
+ module Dovado
2
+ class Router
3
+ class Traffic
4
+ # Data amount data type. Extends Numeric and can be used as an Integer
5
+ # with the addition of getting the amount as bytes, kilobytes, megabytes
6
+ # or gigabytes.
7
+ #
8
+ # @since 1.0.2
9
+ class Amount < Numeric
10
+
11
+ # The default base of a kilobyte.
12
+ DEFAULT_KILO_BASE = 1024
13
+
14
+ # Create a new Amount object.
15
+ #
16
+ # @param [Numeric] value value of this {Amount}.
17
+ # @param [Integer] base the base of a kilobyte.
18
+ def initialize(value, base=DEFAULT_KILO_BASE)
19
+ raise ArgumentError.new "Argument is not numeric: #{value}" unless value.is_a? Numeric
20
+ @value = value
21
+ @base = base
22
+ end
23
+
24
+ # The {Amount} in bytes
25
+ def bytes
26
+ @value * @base
27
+ end
28
+
29
+ # The {Amount} in kilobytes
30
+ def kilobytes
31
+ @value
32
+ end
33
+
34
+ # The {Amount} in megabytes
35
+ def megabytes
36
+ (kilobytes / @base.to_f).round(2)
37
+ end
38
+
39
+ # The {Amount} in gigabytes
40
+ def gigabytes
41
+ (megabytes / @base.to_f).round(2)
42
+ end
43
+
44
+ end
45
+
46
+ # Create a new {Amount} object.
47
+ #
48
+ # @param [Numeric] value initial value of the {Amount}.
49
+ # @param [Integer] base the base of a kilobyte.
50
+ def Amount(value, base=DEFAULT_KILO_BASE)
51
+ Amount.new(value, base)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,4 +1,4 @@
1
1
  module Dovado
2
2
  # Current version of the library.
3
- VERSION = '1.0.1'
3
+ VERSION = '1.0.2'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-dovado
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Lindblom
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-21 00:00:00.000000000 Z
11
+ date: 2015-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: celluloid
@@ -133,10 +133,13 @@ files:
133
133
  - lib/dovado/router/info.rb
134
134
  - lib/dovado/router/info/operator.rb
135
135
  - lib/dovado/router/info/operator/telia.rb
136
+ - lib/dovado/router/internet.rb
136
137
  - lib/dovado/router/services.rb
137
138
  - lib/dovado/router/sms.rb
138
139
  - lib/dovado/router/sms/message.rb
139
140
  - lib/dovado/router/sms/messages.rb
141
+ - lib/dovado/router/traffic.rb
142
+ - lib/dovado/router/traffic/amount.rb
140
143
  - lib/dovado/utilities.rb
141
144
  - lib/dovado/version.rb
142
145
  - ruby-dovado.gemspec