boxrubylib 0.0.1
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.
- data/History.txt +4 -0
- data/License.txt +621 -0
- data/Manifest.txt +14 -0
- data/PostInstall.txt +3 -0
- data/README.rdoc +37 -0
- data/Rakefile +26 -0
- data/lib/boxrubylib/boxclientlib.rb +1036 -0
- data/lib/boxrubylib/restclientlib.rb +284 -0
- data/lib/boxrubylib.rb +8 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/test_boxrubylib.rb +11 -0
- data/test/test_helper.rb +3 -0
- metadata +83 -0
@@ -0,0 +1,284 @@
|
|
1
|
+
#= restclientlib.rb
|
2
|
+
#Author:: Takehiko Watanabe
|
3
|
+
#CopyRights:: Canon Software Inc.
|
4
|
+
#Created date:: 2009/06/10
|
5
|
+
#Version:: 1.0.0
|
6
|
+
#
|
7
|
+
#This file contains RestClientLib module.
|
8
|
+
|
9
|
+
require "net/http"
|
10
|
+
require "net/https"
|
11
|
+
require "openssl"
|
12
|
+
require "rexml/document"
|
13
|
+
require "digest/md5"
|
14
|
+
require "cgi"
|
15
|
+
Net::HTTP.version_1_2
|
16
|
+
#
|
17
|
+
#= module of Rest Client Library
|
18
|
+
#
|
19
|
+
module RestClientLib
|
20
|
+
|
21
|
+
#Http client class.
|
22
|
+
#
|
23
|
+
#attributes:
|
24
|
+
# proxyUrl - Proxy server URL, if you don't want to use Proxy set nil.
|
25
|
+
# proxyPort - Proxy server port.
|
26
|
+
class HttpClient
|
27
|
+
attr_accessor :proxyUrl, :proxyPort, :useSSL
|
28
|
+
|
29
|
+
# Constructor.
|
30
|
+
#
|
31
|
+
def initialize
|
32
|
+
@proxyUrl = nil
|
33
|
+
@proxyPort = nil
|
34
|
+
@useSSL = false;
|
35
|
+
end
|
36
|
+
|
37
|
+
# Throw http get request method to the server.
|
38
|
+
#
|
39
|
+
# [server]
|
40
|
+
# Server address.
|
41
|
+
# [port]
|
42
|
+
# Server port. If you want to use default port 80, you pass the parameter - nil.
|
43
|
+
# [uri]
|
44
|
+
# URI(include parameter and value).
|
45
|
+
# [header]
|
46
|
+
# HTTP header. If you don't need to describe, you pass the parameter - nil.
|
47
|
+
#
|
48
|
+
# [Return value]
|
49
|
+
# Http response body.
|
50
|
+
def httpGetRequest(server, port, uri, header)
|
51
|
+
port = 80 if (port == nil)
|
52
|
+
h = Net::HTTP::Proxy(@proxyUrl,@proxyPort).new(server, port)
|
53
|
+
if (@useSSL == true)
|
54
|
+
h.use_ssl = true
|
55
|
+
h.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
56
|
+
end
|
57
|
+
return h.start() { |http|
|
58
|
+
response = http.get(uri, header)
|
59
|
+
response.body
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
# Throw http post request method to the server.
|
64
|
+
#
|
65
|
+
# [server]
|
66
|
+
# Server address.
|
67
|
+
# [port]
|
68
|
+
# Server port. If you want to use default port 80, you pass the parameter - nil.
|
69
|
+
# [uri]
|
70
|
+
# URI.
|
71
|
+
# [header]
|
72
|
+
# HTTP header. If you don't need to describe, you pass the parameter - nil.
|
73
|
+
# [data]
|
74
|
+
# HTTP query data.
|
75
|
+
#
|
76
|
+
# [Return value]
|
77
|
+
# Http response body.
|
78
|
+
def httpPostRequest(server, port, uri, header, data)
|
79
|
+
port = 80 if (port == nil)
|
80
|
+
h = Net::HTTP::Proxy(@proxyUrl,@proxyPort).new(server, port)
|
81
|
+
if (@useSSL == true)
|
82
|
+
h.use_ssl = true
|
83
|
+
h.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
84
|
+
end
|
85
|
+
return h.start() { |http|
|
86
|
+
response = http.post(uri, data, header)
|
87
|
+
response.body
|
88
|
+
}
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
# Generate query string.
|
93
|
+
#
|
94
|
+
# [params]
|
95
|
+
# Query parameter. You need to pass this parameter to hash type.
|
96
|
+
#
|
97
|
+
# [Return value]
|
98
|
+
# Http response body.
|
99
|
+
def genQueryString(params)
|
100
|
+
ret = Array.new
|
101
|
+
params.each do |k,l|
|
102
|
+
if l.kind_of?(Array)
|
103
|
+
l.each do |m|
|
104
|
+
ret.push(CGI::escape(k.to_s) + "=" + CGI::escape(m.to_s))
|
105
|
+
end
|
106
|
+
else
|
107
|
+
ret.push(CGI::escape(k.to_s) + "=" + CGI::escape(l.to_s))
|
108
|
+
end
|
109
|
+
end
|
110
|
+
ret.join("&")
|
111
|
+
end
|
112
|
+
|
113
|
+
# Determine MIME type from file name.
|
114
|
+
#
|
115
|
+
# [filename]
|
116
|
+
# File name.
|
117
|
+
#
|
118
|
+
# [Return value]
|
119
|
+
# MIME type.
|
120
|
+
def resolveMime(filename)
|
121
|
+
if filename =~ /\.(\w+)$/
|
122
|
+
extn = $1
|
123
|
+
case extn
|
124
|
+
when "form" then return "multipart/form-data"
|
125
|
+
when "txt" then return "text/plain"
|
126
|
+
when "html" then return "text/html"
|
127
|
+
when "htm" then return "text/html"
|
128
|
+
when "jpg" then return "image/jpeg"
|
129
|
+
when "png" then return "image/png"
|
130
|
+
when "gif" then return "image/gif"
|
131
|
+
else return "application/octet-stream"
|
132
|
+
end
|
133
|
+
else
|
134
|
+
return "application/octet-stream"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# Assemble to multipart query.
|
139
|
+
#
|
140
|
+
# [name]
|
141
|
+
# Parameter name.
|
142
|
+
# [value]
|
143
|
+
# Parameter value.
|
144
|
+
#
|
145
|
+
# [Return value]
|
146
|
+
# Multipart query.
|
147
|
+
def toMultiPart(name, value)
|
148
|
+
return "Content-Disposition: form-data; name=\"#{CGI::escape(name)}\"\n\n#{value.to_s}\n"
|
149
|
+
end
|
150
|
+
|
151
|
+
# File to multipart query.
|
152
|
+
#
|
153
|
+
# [filename]
|
154
|
+
# File name.
|
155
|
+
# [content]
|
156
|
+
# File contents.
|
157
|
+
#
|
158
|
+
# [Return value]
|
159
|
+
# File query.
|
160
|
+
def fileToMultiPart(filename, content)
|
161
|
+
mimetype = resolveMime(filename)
|
162
|
+
ret = "Content-Disposition: form-data; name=\"fileField\"; filename=\"#{filename}\"\n"
|
163
|
+
if (mimetype != nil)
|
164
|
+
ret += "Content-Type: #{mimetype}\n\n" + content + "\n"
|
165
|
+
end
|
166
|
+
return ret
|
167
|
+
end
|
168
|
+
|
169
|
+
# Generate query for post method.
|
170
|
+
#
|
171
|
+
# [filename]
|
172
|
+
# File name.
|
173
|
+
# [params]
|
174
|
+
# Parameters.
|
175
|
+
# [data]
|
176
|
+
# File data.
|
177
|
+
# [boundary]
|
178
|
+
# Query boundary.
|
179
|
+
#
|
180
|
+
# [Return value]
|
181
|
+
# Query string.
|
182
|
+
def prepareQuery(filename, params, data, boundary)
|
183
|
+
query = ""
|
184
|
+
if (params != nil)
|
185
|
+
params.each do |key, value|
|
186
|
+
if (defined?(value)&&(value.instance_of?(Array)))
|
187
|
+
value.each do |item|
|
188
|
+
query += "--" + boundary +"\n" + toMultiPart(key, item)
|
189
|
+
end
|
190
|
+
else
|
191
|
+
query += "--" + boundary + "\n" + toMultiPart(key, value)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
if ((filename != nil)&&(data != nil))
|
196
|
+
query += "--" + boundary + "\n";
|
197
|
+
query += fileToMultiPart(filename, data);
|
198
|
+
end
|
199
|
+
query += "--" + boundary + "--\n" if (query != nil)
|
200
|
+
|
201
|
+
return query
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
#Rest client class.
|
206
|
+
#
|
207
|
+
class RestClient < HttpClient
|
208
|
+
# Constructor.
|
209
|
+
#
|
210
|
+
def initialize
|
211
|
+
super
|
212
|
+
end
|
213
|
+
|
214
|
+
# Throw rest get request method to the server.
|
215
|
+
#
|
216
|
+
# [server]
|
217
|
+
# Server address.
|
218
|
+
# [port]
|
219
|
+
# Server port. If you want to use default port 80, you pass the parameter - nil.
|
220
|
+
# [apipath]
|
221
|
+
# Api path to rest request.
|
222
|
+
# [params]
|
223
|
+
# Parameters to get. You need to pass this parameter to hash type.
|
224
|
+
# [header]
|
225
|
+
# HTTP header. If you don't need to describe, you pass the parameter - nil.
|
226
|
+
#
|
227
|
+
# [Return value]
|
228
|
+
# Xml document.
|
229
|
+
def getRequest(server, port, apipath, params, header)
|
230
|
+
if (params != nil)
|
231
|
+
uri = apipath + "?" + genQueryString(params)
|
232
|
+
else
|
233
|
+
uri = apipath
|
234
|
+
end
|
235
|
+
body = self.httpGetRequest(server, port, uri, header)
|
236
|
+
begin
|
237
|
+
return REXML::Document.new(body) if body
|
238
|
+
return nil
|
239
|
+
rescue
|
240
|
+
body
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
# Throw rest post request method to the server.
|
245
|
+
# Notice - This method makes content-type header field automatically,
|
246
|
+
# so you don't set content-type to "header" parameter.
|
247
|
+
#
|
248
|
+
# [server]
|
249
|
+
# Server address.
|
250
|
+
# [port]
|
251
|
+
# Server port. If you want to use default port 80, you pass the parameter - nil.
|
252
|
+
# [apipath]
|
253
|
+
# Api path to rest request.
|
254
|
+
# [fileName]
|
255
|
+
# File name, if you want to post file.
|
256
|
+
# [data]
|
257
|
+
# File data, if you want to post file.
|
258
|
+
# [params]
|
259
|
+
# Parameters to post. You need to pass this parameter to hash type.
|
260
|
+
# [header]
|
261
|
+
# HTTP header. If you don't need to describe, you pass the parameter - nil.
|
262
|
+
#
|
263
|
+
# [Return value]
|
264
|
+
# Xml document.
|
265
|
+
def postRequest(server, port, apipath, fileName, data, params, header)
|
266
|
+
boundary = Digest::MD5.hexdigest(fileName != nil ? fileName : Time.now.to_s).to_s
|
267
|
+
query = self.prepareQuery(fileName, params, data, boundary)
|
268
|
+
if (header != nil)
|
269
|
+
localheader = Hash[header]
|
270
|
+
else
|
271
|
+
localheader = Hash.new
|
272
|
+
end
|
273
|
+
localheader['Content-Type'] = "multipart/form-data, boundary=" + boundary + "\r\n"
|
274
|
+
localheader['Content-Length'] = query.length.to_s
|
275
|
+
body = self.httpPostRequest(server, port, apipath, localheader, query)
|
276
|
+
begin
|
277
|
+
return REXML::Document.new(body) if body
|
278
|
+
return nil
|
279
|
+
rescue
|
280
|
+
return body
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
data/lib/boxrubylib.rb
ADDED
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/boxrubylib.rb'}"
|
9
|
+
puts "Loading boxrubylib 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)
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: boxrubylib
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tomohiko Ariki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-21 00:00:00 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.2
|
24
|
+
version:
|
25
|
+
description: Ruby library of OpenBox API.
|
26
|
+
email:
|
27
|
+
- tomohiko.ariki@nifty.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- License.txt
|
35
|
+
- Manifest.txt
|
36
|
+
- PostInstall.txt
|
37
|
+
files:
|
38
|
+
- History.txt
|
39
|
+
- License.txt
|
40
|
+
- Manifest.txt
|
41
|
+
- PostInstall.txt
|
42
|
+
- README.rdoc
|
43
|
+
- Rakefile
|
44
|
+
- lib/boxrubylib.rb
|
45
|
+
- lib/boxrubylib/boxclientlib.rb
|
46
|
+
- lib/boxrubylib/restclientlib.rb
|
47
|
+
- script/console
|
48
|
+
- script/destroy
|
49
|
+
- script/generate
|
50
|
+
- test/test_boxrubylib.rb
|
51
|
+
- test/test_helper.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://boxrubylib.rubyforge.org
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message: PostInstall.txt
|
57
|
+
rdoc_options:
|
58
|
+
- --main
|
59
|
+
- README.rdoc
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project: boxrubylib
|
77
|
+
rubygems_version: 1.3.5
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Ruby library of OpenBox API.
|
81
|
+
test_files:
|
82
|
+
- test/test_boxrubylib.rb
|
83
|
+
- test/test_helper.rb
|