mtik 4.0.2 → 4.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0faffc781b693499b3a24a0b07cc918c146903a6
4
+ data.tar.gz: 2415ca8ad18b2f83e55ca29e81bfe776f507700c
5
+ SHA512:
6
+ metadata.gz: 82acc2725c7f074df88f22585a13a1395ffedd4bf9194daa167e893f1d33d11da70bb4a9825c25c294863b7bd88b8bdf3c435d848461862402bf22cf26824e58
7
+ data.tar.gz: 04e7aca873f26094fdcd701431bba659ad0663f7528b2ab03ddc9192276cafbda3b80f6d68698d366c9ae74c1049e33602adf8a59b2b625e177f0d2b8c2474ad
@@ -1,3 +1,6 @@
1
+ 2014-02-14 (14 FEB 2014) VERSION 4.2.3 Aaron D. Gifford (http://www.aarongifford.com)
2
+ * Update to fetch() utility, along with some very minor some cosmetic changes
3
+
1
4
  2013-06-06 (06 JUN 2013) VERSION 4.0.2 Aaron D. Gifford (http://www.aarongifford.com)
2
5
  Bart Braem (http://www.lalunerouge.net/)
3
6
  * Merged Bart Braem's implementation of timeouts and bumped up the version. Thanks, Bart!
@@ -1 +1 @@
1
- 4.0.2
1
+ 4.0.3
@@ -2,11 +2,13 @@
2
2
  ########################################################################
3
3
  #--
4
4
  #
5
- # FILE: json.rb -- Example of using the Ruby MikroTik API in Ruby
5
+ # FILE: tikjson.rb -- Example of using the Ruby MikroTik API in Ruby
6
+ # to execute an API command and retrieve results
7
+ # in JSON format
6
8
  #
7
9
  #++
8
10
  # Author:: Aaron D. Gifford - http://www.aarongifford.com/
9
- # Copyright:: Copyright (c) 2009-2011, InfoWest, Inc.
11
+ # Copyright:: Copyright (c) 2009-2014, InfoWest, Inc.
10
12
  # License:: BSD license
11
13
  #--
12
14
  # Redistribution and use in source and binary forms, with or without
@@ -134,7 +134,7 @@ class MTik::Connection
134
134
  begin
135
135
  addr = Socket.getaddrinfo(@host, nil)
136
136
  @sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
137
-
137
+
138
138
  begin
139
139
  @sock.connect_nonblock(Socket.pack_sockaddr_in(@port, addr[0][3]))
140
140
  rescue Errno::EINPROGRESS
@@ -142,10 +142,10 @@ class MTik::Connection
142
142
  if ready
143
143
  @sock
144
144
  else
145
- raise Errno::ETIMEDOUT
145
+ raise Errno::ETIMEDOUT
146
146
  end
147
147
  end
148
-
148
+
149
149
  rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, Errno::ENETUNREACH,
150
150
  Errno::EHOSTUNREACH => e
151
151
  @sock = nil
@@ -257,7 +257,7 @@ class MTik::Connection
257
257
  sentence = get_sentence ## This call must be ATOMIC or re-entrant safety fails
258
258
 
259
259
  ## Check for '!fatal' before checking for a tag--'!fatal'
260
- ## is never(???) tagged:
260
+ ## is never(???) tagged:
261
261
  if sentence.key?('!fatal')
262
262
  ## FATAL ERROR has occured! (Or a '/quit' command was issued...)
263
263
  if @data.length > 0
@@ -356,7 +356,7 @@ class MTik::Connection
356
356
  ## +args+:: Zero or more arguments to the command
357
357
  ## +callback+:: Proc/lambda code (or code block if not provided as
358
358
  ## an argument) to be called. (See the +await_completion+
359
- ##
359
+ ##
360
360
  def send_request(await_completion, command, *args, &callback)
361
361
  if await_completion.is_a?(MTik::Request)
362
362
  req = await_completion
@@ -525,16 +525,53 @@ class MTik::Connection
525
525
  ## +total+:: Final expected file size in bytes
526
526
  ## +bytes+:: Number of bytes transferred so far
527
527
  ## +request+:: The MTik::Request object
528
- def fetch(url, filename, timeout=nil, &callback)
528
+ def fetch(url, filename=nil, timeout=nil, &callback)
529
+ require 'uri'
530
+
531
+ uri = URI(url)
532
+ filename = File.basename(uri.path) if filename.nil?
533
+
529
534
  total = bytes = oldbytes = 0
530
535
  status = ''
531
536
  done = false
532
537
  lastactivity = Time.now
533
- req = get_reply_each(
534
- '/tool/fetch',
535
- '=url=' + url,
536
- '=dst-path=' + filename
537
- ) do |r, s|
538
+
539
+ ## RouterOS versions 4.9 and prior (not sure if this version cut-off
540
+ ## is exactly right) would accept the url parameter, but failed to
541
+ ## download the files. So for versions older than this, we'll use
542
+ ## the mode/src-path/port parameters instead if possible.
543
+ if !@os_version.nil? && lambda {|a,b|
544
+ sr = %r{(?:\.|rc|beta|alpha)}
545
+ a = a.split(sr).map{|i| i.to_i}
546
+ b = b.split(sr).map{|i| i.to_i}
547
+ i = 0
548
+ while i < a.size && i < b.size
549
+ return -1 if a[i] < b[i]
550
+ return 1 if a[i] > b[i]
551
+ i += 1
552
+ end
553
+ return a.size <=> b.size
554
+ }.call(@os_version, '4.9') < 1
555
+ command = [
556
+ '/tool/fetch', '=mode=' + uri.scheme,
557
+ '=src-path=' + uri.path + (uri.query.size > 0 ? '?' + uri.query : ''),
558
+ '=dst-path=' + filename
559
+ ]
560
+ case uri.scheme
561
+ when 'http'
562
+ command << '=port=80'
563
+ when 'https'
564
+ command << '=port=443'
565
+ end
566
+ else
567
+ command = [
568
+ '/tool/fetch',
569
+ '=url=' + url,
570
+ '=dst-path=' + filename
571
+ ]
572
+ end
573
+
574
+ req = get_reply_each(command[0], *command[1..-1]) do |r, s|
538
575
  if s.key?('!re') && !done
539
576
  unless s.key?('status')
540
577
  raise MTik::Error.new("Unknown response to '/tool/fetch': missing 'status' in response.")
@@ -624,7 +661,7 @@ class MTik::Connection
624
661
  else
625
662
  raise MTik::Error.new("Invalid settings match class '#{keyitem}' (expected Array, Regexp, or String)")
626
663
  end
627
-
664
+
628
665
  if s.key?(key)
629
666
  ## A key matches! && s[k] != v
630
667
  oldv = s[k]
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mtik
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.2
5
- prerelease:
4
+ version: 4.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Aaron D. Gifford
@@ -24,12 +23,12 @@ files:
24
23
  - CHANGELOG.txt
25
24
  - LICENSE.txt
26
25
  - README.txt
27
- - VERSION.txt
28
26
  - Rakefile
29
- - examples/tikjson.rb
27
+ - VERSION.txt
30
28
  - bin/tikcli
31
29
  - bin/tikcommand
32
30
  - bin/tikfetch
31
+ - examples/tikjson.rb
33
32
  - lib/mtik.rb
34
33
  - lib/mtik/connection.rb
35
34
  - lib/mtik/error.rb
@@ -39,26 +38,25 @@ files:
39
38
  - lib/mtik/timeouterror.rb
40
39
  homepage: http://www.aarongifford.com/computers/mtik/
41
40
  licenses: []
41
+ metadata: {}
42
42
  post_install_message:
43
43
  rdoc_options: []
44
44
  require_paths:
45
45
  - lib
46
46
  required_ruby_version: !ruby/object:Gem::Requirement
47
- none: false
48
47
  requirements:
49
- - - ! '>='
48
+ - - '>='
50
49
  - !ruby/object:Gem::Version
51
50
  version: '0'
52
51
  required_rubygems_version: !ruby/object:Gem::Requirement
53
- none: false
54
52
  requirements:
55
- - - ! '>='
53
+ - - '>='
56
54
  - !ruby/object:Gem::Version
57
55
  version: '0'
58
56
  requirements: []
59
57
  rubyforge_project: mtik
60
- rubygems_version: 1.8.25
58
+ rubygems_version: 2.2.1
61
59
  signing_key:
62
- specification_version: 3
60
+ specification_version: 4
63
61
  summary: MTik implements the MikroTik RouterOS API for use in Ruby.
64
62
  test_files: []