biola_web_services 0.1.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
|
|
1
|
+
module BiolaWebServices
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :url
|
4
|
+
attr_accessor :cert_path
|
5
|
+
attr_accessor :key_path
|
6
|
+
attr_accessor :key_password
|
7
|
+
attr_accessor :verify_ssl
|
8
|
+
attr_accessor :ssl_version
|
9
|
+
|
10
|
+
# DEFAULTS
|
11
|
+
@url = 'https://ws.biola.edu'
|
12
|
+
@verify_ssl = true
|
13
|
+
end
|
14
|
+
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
module BiolaWebServices
|
2
|
-
|
3
2
|
class Service
|
4
|
-
|
5
3
|
require 'json'
|
6
4
|
|
7
5
|
@service_name = nil
|
@@ -10,42 +8,47 @@ module BiolaWebServices
|
|
10
8
|
@service_name = service_name
|
11
9
|
end
|
12
10
|
|
13
|
-
def
|
14
|
-
|
11
|
+
def send(method_name, args)
|
12
|
+
request = Net::HTTP::Post.new request_path(method_name)
|
13
|
+
request.set_form_data args
|
14
|
+
|
15
|
+
response = BiolaWebServices.connection.start{|conn| conn.request(request)}
|
15
16
|
begin
|
16
|
-
JSON.parse response
|
17
|
-
rescue
|
18
|
-
response
|
17
|
+
JSON.parse response.body
|
18
|
+
rescue JSON::ParserError
|
19
|
+
response.body # not all responses are JSON
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
23
|
def method_missing(m, *args)
|
23
|
-
|
24
24
|
method_name = ruby_to_ws_method_name(m)
|
25
25
|
modifier = m.to_s[-1, 1]
|
26
26
|
args = args[0] if args.length == 1
|
27
27
|
|
28
28
|
case modifier
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
29
|
+
when '?' # return true or false
|
30
|
+
response = send(method_name, args)
|
31
|
+
%w(true t yes y 1).include?(response.to_s.downcase.strip)
|
32
|
+
when '!' # raise errors
|
33
|
+
send(method_name, args)
|
34
|
+
else # return nil on error
|
35
|
+
begin
|
36
|
+
send(method_name, args)
|
37
|
+
rescue
|
38
|
+
nil
|
39
|
+
end
|
40
40
|
end
|
41
|
-
|
42
41
|
end
|
43
42
|
|
43
|
+
private
|
44
|
+
def request_path(method_name)
|
45
|
+
@uri ||= URI.parse(BiolaWebServices.config.url)
|
46
|
+
[@uri.path, @service_name, method_name].join('/')
|
47
|
+
end
|
48
|
+
|
44
49
|
# Basically just a stripped down, customized version of Rails' camelize
|
45
50
|
def ruby_to_ws_method_name(method_name)
|
46
51
|
method_name.to_s.gsub(/(^|_)(.)/) { $2.upcase }.delete('?!')
|
47
52
|
end
|
48
|
-
|
49
53
|
end
|
50
|
-
|
51
|
-
end
|
54
|
+
end
|
data/lib/biola_web_services.rb
CHANGED
@@ -1,2 +1,42 @@
|
|
1
|
-
|
2
|
-
require 'biola_web_services/
|
1
|
+
module BiolaWebServices
|
2
|
+
require 'biola_web_services/service'
|
3
|
+
require 'biola_web_services/configuration'
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
def self.configure
|
7
|
+
yield config
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.config
|
11
|
+
@config ||= Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
public
|
15
|
+
|
16
|
+
def self.connection
|
17
|
+
return @connection unless @connection.nil?
|
18
|
+
|
19
|
+
cert = OpenSSL::X509::Certificate.new(File.read(self.config.cert_path))
|
20
|
+
key = OpenSSL::PKey::RSA.new(File.read(self.config.key_path), self.config.key_password)
|
21
|
+
uri = URI.parse(self.config.url)
|
22
|
+
|
23
|
+
@connection = Net::HTTP.new(uri.host, uri.port)
|
24
|
+
if uri.scheme == 'https'
|
25
|
+
@connection.use_ssl = true
|
26
|
+
@connection.cert = cert
|
27
|
+
@connection.key = key
|
28
|
+
@connection.verify_mode = self.config.verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
|
29
|
+
@connection.ssl_version = self.config.ssl_version unless self.config.ssl_version.nil?
|
30
|
+
end
|
31
|
+
|
32
|
+
@connection
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.method_missing(m, *args)
|
36
|
+
if args.empty?
|
37
|
+
BiolaWebServices::Service.new(m)
|
38
|
+
else
|
39
|
+
super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: biola_web_services
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,30 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
13
|
-
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: rest-client
|
16
|
-
requirement: &82335240 !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: 1.6.0
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: *82335240
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: json
|
27
|
-
requirement: &82334560 !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
29
|
-
requirements:
|
30
|
-
- - ~>
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: 1.5.0
|
33
|
-
type: :runtime
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *82334560
|
12
|
+
date: 2012-04-13 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
36
14
|
description: Simple Ruby wrapper for Biola Web Services REST calls
|
37
15
|
email: adam.crownoble@biola.edu
|
38
16
|
executables: []
|
@@ -41,8 +19,8 @@ extra_rdoc_files: []
|
|
41
19
|
files:
|
42
20
|
- lib/biola_web_services.rb
|
43
21
|
- lib/biola_web_services/version.rb
|
44
|
-
- lib/biola_web_services/biola_web_services.rb
|
45
22
|
- lib/biola_web_services/service.rb
|
23
|
+
- lib/biola_web_services/configuration.rb
|
46
24
|
homepage: http://biola.edu/it
|
47
25
|
licenses: []
|
48
26
|
post_install_message:
|
@@ -1,133 +0,0 @@
|
|
1
|
-
module BiolaWebServices
|
2
|
-
|
3
|
-
require 'rest_client'
|
4
|
-
require 'json'
|
5
|
-
|
6
|
-
def self.url
|
7
|
-
@@url ||= 'https://ws.biola.edu'
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.url=(url)
|
11
|
-
@@url = url
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.auth_type
|
15
|
-
@@auth_type ||= :cert
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.auth_type=(auth_type)
|
19
|
-
unless [:cert, :login].include? auth_type
|
20
|
-
raise ArgumentError, 'Valid types are :cert and :login'
|
21
|
-
end
|
22
|
-
|
23
|
-
@@auth_type = auth_type.to_sym
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.cert_path
|
27
|
-
@@cert_path.to_s
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.cert_path=(cert_path)
|
31
|
-
@@cert_path = cert_path
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.key_path
|
35
|
-
@@key_path.to_s
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.key_path=(key_path)
|
39
|
-
@@key_path = key_path
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.key_password
|
43
|
-
@@key_password.to_s
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.key_password=(key_password)
|
47
|
-
@@key_password = key_password
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.username
|
51
|
-
@@username.to_s
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.username=(username)
|
55
|
-
@@username = username
|
56
|
-
end
|
57
|
-
|
58
|
-
def self.password
|
59
|
-
@@password.to_s
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.password=(password)
|
63
|
-
@@pasword = password
|
64
|
-
end
|
65
|
-
|
66
|
-
def self.verify_ssl
|
67
|
-
@@verify_ssl ||= true
|
68
|
-
end
|
69
|
-
|
70
|
-
def self.verify_ssl=(verify_ssl)
|
71
|
-
@@verify_ssl = verify_ssl
|
72
|
-
end
|
73
|
-
|
74
|
-
private
|
75
|
-
|
76
|
-
def self.cert
|
77
|
-
@@cert ||= nil
|
78
|
-
end
|
79
|
-
|
80
|
-
def self.cert=(cert)
|
81
|
-
@@cert = cert
|
82
|
-
end
|
83
|
-
|
84
|
-
def self.key
|
85
|
-
@@key ||= nil
|
86
|
-
end
|
87
|
-
|
88
|
-
def self.key=(key)
|
89
|
-
@@key = key
|
90
|
-
end
|
91
|
-
|
92
|
-
def self.connect_with_cert(service_name)
|
93
|
-
|
94
|
-
if self.cert.nil?
|
95
|
-
self.cert = OpenSSL::X509::Certificate.new(File.read(self.cert_path))
|
96
|
-
end
|
97
|
-
|
98
|
-
if self.key.nil?
|
99
|
-
self.key = OpenSSL::PKey::RSA.new(File.read(self.key_path), self.key_password)
|
100
|
-
end
|
101
|
-
|
102
|
-
return RestClient::Resource.new(
|
103
|
-
"#{self.url}/#{service_name}/rest",
|
104
|
-
:ssl_client_cert => self.cert,
|
105
|
-
:ssl_client_key => self.key,
|
106
|
-
:verify_ssl => self.verify_ssl
|
107
|
-
)
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
def self.connect_with_login(service_name)
|
112
|
-
return RestClient::Resource.new(
|
113
|
-
"#{self.url}/#{service_name}/rest",
|
114
|
-
:user=>self.username,
|
115
|
-
:password=>self.password
|
116
|
-
)
|
117
|
-
end
|
118
|
-
|
119
|
-
public
|
120
|
-
|
121
|
-
def self.connection(service_name)
|
122
|
-
if self.auth_type == :cert
|
123
|
-
return self.connect_with_cert(service_name)
|
124
|
-
else
|
125
|
-
return self.connect_with_login(service_name)
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def self.method_missing(m, *args)
|
130
|
-
BiolaWebServices::Service.new(m)
|
131
|
-
end
|
132
|
-
|
133
|
-
end
|