switchvoxFix 0.1.6 → 0.1.7
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 +4 -4
- data/History.txt +4 -0
- data/Manifest.txt +16 -0
- data/lib/switchvoxFix.rb +1 -1
- data/lib/switchvoxFix/array.rb +14 -0
- data/lib/switchvoxFix/base.rb +146 -0
- data/lib/switchvoxFix/hash.rb +17 -0
- data/lib/switchvoxFix/net_http_digest_auth.rb +34 -0
- data/lib/switchvoxFix/object.rb +5 -0
- data/lib/switchvoxFix/version.rb +1 -1
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- metadata +11 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c75f4b5a22db8e34414ddf7838a6050bcd7370a
|
4
|
+
data.tar.gz: a0ede2a6ff9eead761b10a4b2e1dc661fc72b7a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64f4649f231ed26e4c6eab1b359c6cf7594dcb77cf006fb742334ba904a5f731958d694094ccc2659bd25064e56bd2c086119ede723852c32f7068844ec28f2f
|
7
|
+
data.tar.gz: 8a73aa06bf1cede51956da3f74307989d9deb8f6a1c25e63d1e109e04b40247e5246ff2b5ffa40d0d136a67e12c9c11f6fba7af4a0243fac6ce77f43f4a92aed
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.md
|
4
|
+
Rakefile
|
5
|
+
lib/switchvoxFix.rb
|
6
|
+
lib/switchvoxFix/array.rb
|
7
|
+
lib/switchvoxFix/base.rb
|
8
|
+
lib/switchvoxFix/hash.rb
|
9
|
+
lib/switchvoxFix/net_http_digest_auth.rb
|
10
|
+
lib/switchvoxFix/object.rb
|
11
|
+
script/console
|
12
|
+
script/destroy
|
13
|
+
script/generate
|
14
|
+
test/test_helper.rb
|
15
|
+
test/test_json_to_obj.rb
|
16
|
+
test/test_switchvox.rb
|
data/lib/switchvoxFix.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
# A fancy way of iterating over an array and converting hashes to objects
|
2
|
+
class Array
|
3
|
+
def to_obj
|
4
|
+
# Make a deep copy of the array
|
5
|
+
a = Marshal.load(Marshal.dump(self))
|
6
|
+
a.each do |i|
|
7
|
+
case i.class.to_s
|
8
|
+
when "Hash" then i = i.to_obj
|
9
|
+
when "Array" then i = i.to_obj
|
10
|
+
end
|
11
|
+
end
|
12
|
+
a
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
module SwitchvoxFix
|
4
|
+
|
5
|
+
require 'uri'
|
6
|
+
require 'net/https'
|
7
|
+
require 'openssl'
|
8
|
+
require 'digest/md5'
|
9
|
+
require 'rubygems'
|
10
|
+
require 'json'
|
11
|
+
|
12
|
+
# Raised when credentials are incorrect
|
13
|
+
class LoginError < RuntimeError
|
14
|
+
end
|
15
|
+
|
16
|
+
# Raised when the response.body is empty
|
17
|
+
class EmptyResponse < RuntimeError
|
18
|
+
end
|
19
|
+
|
20
|
+
# Raised when a response is returned that we don't know how to handle
|
21
|
+
class UnhandledResponse < RuntimeError
|
22
|
+
end
|
23
|
+
|
24
|
+
# The primary class used to interact with Switchvox.
|
25
|
+
class Base
|
26
|
+
|
27
|
+
URL = "/json"
|
28
|
+
attr :host, true
|
29
|
+
attr :url, true
|
30
|
+
attr :user, false
|
31
|
+
attr :pass, false
|
32
|
+
attr :connection, true
|
33
|
+
attr :session, true
|
34
|
+
attr :debug, true
|
35
|
+
attr :auth_header, true
|
36
|
+
|
37
|
+
def initialize(host, user, pass, port, options={})
|
38
|
+
{:debug => false}.merge! options
|
39
|
+
@debug = options[:debug]
|
40
|
+
|
41
|
+
@host = host
|
42
|
+
@user = user
|
43
|
+
@pass = pass
|
44
|
+
@url = URI.parse("https://" + @host + URL)
|
45
|
+
@url.port = port
|
46
|
+
|
47
|
+
@ssl = true
|
48
|
+
|
49
|
+
@connection = false
|
50
|
+
@auth_header = false
|
51
|
+
login!
|
52
|
+
raise LoginError, "Invalid Username or Password" unless logged_in?
|
53
|
+
end
|
54
|
+
|
55
|
+
# A standard REST call to get a list of entries
|
56
|
+
def request(method, parameters={})
|
57
|
+
login! unless logged_in?
|
58
|
+
json = wrap_json(method, parameters)
|
59
|
+
|
60
|
+
# Send the request
|
61
|
+
header = {'Content-Type' => "application/json"}
|
62
|
+
request = Net::HTTP::Post.new(@url.path, header)
|
63
|
+
request.digest_auth(@user, @pass, @auth_header)
|
64
|
+
request.body = json
|
65
|
+
response = @connection.request(request)
|
66
|
+
|
67
|
+
if @debug
|
68
|
+
puts "#{method}: Request"
|
69
|
+
puts json
|
70
|
+
puts "\n"
|
71
|
+
end
|
72
|
+
|
73
|
+
case response
|
74
|
+
when Net::HTTPOK
|
75
|
+
raise EmptyResponse unless response.body
|
76
|
+
response_json = JSON.parse response.body
|
77
|
+
if @debug
|
78
|
+
puts "#{method}: Response:"
|
79
|
+
pp response_json
|
80
|
+
puts "\n\n"
|
81
|
+
end
|
82
|
+
response_obj = response_json["response"]["result"].to_obj
|
83
|
+
return response_obj
|
84
|
+
when Net::HTTPUnauthorized
|
85
|
+
login!
|
86
|
+
request(method, parameters)
|
87
|
+
when Net::HTTPForbidden
|
88
|
+
raise LoginError, "Invalid Username or Password"
|
89
|
+
else raise UnhandledResponse, "Can't handle response #{response}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
protected
|
94
|
+
|
95
|
+
# Check to see if we are logged in
|
96
|
+
def logged_in?
|
97
|
+
return false unless @auth_header
|
98
|
+
true
|
99
|
+
end
|
100
|
+
|
101
|
+
# Attempt HTTP Digest Authentication with Switchvox
|
102
|
+
def login!
|
103
|
+
connect! unless connected?
|
104
|
+
@auth_header = @connection.head(@url.path)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Check to see if we have an HTTP/S Connection
|
108
|
+
def connected?
|
109
|
+
return false unless @connection
|
110
|
+
return false unless @connection.started?
|
111
|
+
true
|
112
|
+
end
|
113
|
+
|
114
|
+
# Connect to the remote Switchvox system. If SSL is enabled, ignore certificate checking.
|
115
|
+
def connect!
|
116
|
+
@connection = Net::HTTP.new(@url.host, @url.port)
|
117
|
+
if @ssl
|
118
|
+
@connection.use_ssl = true
|
119
|
+
@connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
120
|
+
end
|
121
|
+
if @debug
|
122
|
+
#@connection.set_debug_output $stderr
|
123
|
+
end
|
124
|
+
@connection.start
|
125
|
+
end
|
126
|
+
|
127
|
+
# Wrap a JSON method and parameters in a Switchvox compatible request body.
|
128
|
+
def wrap_json(method, parameters={})
|
129
|
+
json = <<-"EOF"
|
130
|
+
{
|
131
|
+
\"request\": {
|
132
|
+
\"method\": \"#{method}\",
|
133
|
+
\"parameters\": #{parameters.to_json}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
EOF
|
137
|
+
json.gsub!(/^\s{8}/,'')
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# A fancy way of iterating over a hash and converting hashes to objects
|
2
|
+
class Hash
|
3
|
+
def to_obj
|
4
|
+
o = Object.new
|
5
|
+
self.each do |k,v|
|
6
|
+
# If we're looking at a hash or array, we need to look through them and convert any hashes to objects as well
|
7
|
+
case v.class.to_s
|
8
|
+
when "Hash" then v = v.to_obj
|
9
|
+
when "Array" then v = v.to_obj
|
10
|
+
end
|
11
|
+
o.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
|
12
|
+
o.class.send(:define_method, k, proc{o.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable
|
13
|
+
o.class.send(:define_method, "#{k}=", proc{|v| o.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable
|
14
|
+
end
|
15
|
+
o
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Net
|
2
|
+
module HTTPHeader
|
3
|
+
require 'digest/md5'
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
@@nonce_count = -1
|
7
|
+
CNONCE = Digest::MD5.new.update("%x" % (Time.now.to_i + rand(65535))).hexdigest
|
8
|
+
def digest_auth(user, password, response)
|
9
|
+
# based on http://segment7.net/projects/ruby/snippets/digest_auth.rb
|
10
|
+
@@nonce_count += 1
|
11
|
+
|
12
|
+
response['www-authenticate'] =~ /^(\w+) (.*)/
|
13
|
+
|
14
|
+
params = {}
|
15
|
+
$2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }
|
16
|
+
|
17
|
+
a_1 = "#{user}:#{params['realm']}:#{password}"
|
18
|
+
a_2 = "#{@method}:#{@path}"
|
19
|
+
request_digest = ''
|
20
|
+
request_digest << Digest::MD5.new.update(a_1).hexdigest
|
21
|
+
request_digest << ':' << params['nonce']
|
22
|
+
request_digest << ':' << Digest::MD5.new.update(a_2).hexdigest
|
23
|
+
|
24
|
+
header = []
|
25
|
+
header << "Digest username=\"#{user}\""
|
26
|
+
header << "realm=\"#{params['realm']}\""
|
27
|
+
header << "uri=\"#{@path}\""
|
28
|
+
header << "nonce=\"#{params['nonce']}\""
|
29
|
+
header << "response=\"#{Digest::MD5.new.update(request_digest).hexdigest}\""
|
30
|
+
|
31
|
+
@header['Authorization'] = header
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/switchvoxFix/version.rb
CHANGED
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/switchvoxFix.rb'}"
|
9
|
+
puts "Loading switchvoxFix gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switchvoxFix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mitchell Thompson
|
@@ -48,13 +48,23 @@ extra_rdoc_files: []
|
|
48
48
|
files:
|
49
49
|
- ".gitignore"
|
50
50
|
- Gemfile
|
51
|
+
- History.txt
|
51
52
|
- LICENSE.txt
|
53
|
+
- Manifest.txt
|
52
54
|
- README.md
|
53
55
|
- Rakefile
|
54
56
|
- bin/console
|
55
57
|
- bin/setup
|
56
58
|
- lib/switchvoxFix.rb
|
59
|
+
- lib/switchvoxFix/array.rb
|
60
|
+
- lib/switchvoxFix/base.rb
|
61
|
+
- lib/switchvoxFix/hash.rb
|
62
|
+
- lib/switchvoxFix/net_http_digest_auth.rb
|
63
|
+
- lib/switchvoxFix/object.rb
|
57
64
|
- lib/switchvoxFix/version.rb
|
65
|
+
- script/console
|
66
|
+
- script/destroy
|
67
|
+
- script/generate
|
58
68
|
- switchvoxFix.gemspec
|
59
69
|
homepage: https://github.com/wrenchell/switchvoxFix
|
60
70
|
licenses:
|