rct 0.1 → 0.3

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/bin/rct CHANGED
@@ -19,7 +19,7 @@
19
19
  #
20
20
 
21
21
 
22
- #--------------------------------------------------------------------------------------------------
22
+ #------------------------------------------------------------------------------
23
23
  # rct main
24
24
  #
25
25
  # Runs either a CLI invocation or a test suite depending on arguments.
data/lib/rct.rb CHANGED
@@ -1,6 +1,5 @@
1
- #!/usr/bin/env jruby
2
1
  #
3
- # Copyright 2012 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2012-2013 Jyri J. Virkki <jyri@virkki.com>
4
3
  #
5
4
  # This file is part of rct.
6
5
  #
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2012 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2012-2013 Jyri J. Virkki <jyri@virkki.com>
3
3
  #
4
4
  # This file is part of rct.
5
5
  #
@@ -18,37 +18,63 @@
18
18
  #
19
19
 
20
20
 
21
- #--------------------------------------------------------------------------------------------------
22
- # Assorted utility methods.
21
+ #-----------------------------------------------------------------------------
22
+ # Assorted utility methods. TODO: cleanup
23
23
  #
24
24
  class RCT
25
25
 
26
+ @log_level = RESULT
26
27
 
27
- def self.rct_log(level, line)
28
- puts line
28
+
29
+ def self.log_level
30
+ @log_level
31
+ end
32
+
33
+
34
+ def self.set_log_level(level)
35
+ @log_level = RESULT if level == RESULT
36
+ @log_level = INFO if level == INFO
37
+ @log_level = DEBUG if level == DEBUG
38
+ end
39
+
40
+
41
+ def self.increase_log_level
42
+ @log_level += 1
43
+ end
44
+
45
+
46
+ def self.log(level, line)
47
+ if (level <= @log_level)
48
+ puts line
49
+ end
29
50
  end
30
51
 
52
+
31
53
  def self.error(msg)
32
54
  puts "error: #{msg}"
33
55
  end
34
56
 
57
+
35
58
  def self.bad_invocation(msg)
36
59
  error(msg)
37
60
  exit(1)
38
61
  end
39
62
 
63
+
40
64
  def self.die(msg)
41
65
  error(msg)
42
66
  exit(1)
43
67
  end
44
68
 
69
+
45
70
  def self.argv_get(pos)
46
- value = $ARGV[pos + 1]
47
- $ARGV.delete_at(pos + 1)
48
- $ARGV.delete_at(pos)
71
+ value = ARGV[pos + 1]
72
+ ARGV.delete_at(pos + 1)
73
+ ARGV.delete_at(pos)
49
74
  return value
50
75
  end
51
76
 
77
+
52
78
  def self.help
53
79
 
54
80
  # default is CLI invocation of method name
@@ -60,10 +86,16 @@ class RCT
60
86
 
61
87
  end
62
88
 
89
+
63
90
  def self.parse_global_options
64
91
  pos = 0
65
- while (pos < $ARGV.length)
66
- arg = $ARGV[pos]
92
+
93
+ if (ARGV == nil)
94
+ die("No arguments!")
95
+ end
96
+
97
+ while (pos < ARGV.length)
98
+ arg = ARGV[pos]
67
99
 
68
100
  if (arg == '-t' || arg == '--test')
69
101
  sset(RCT_MODE, RCT_MODE_TEST)
@@ -78,6 +110,10 @@ class RCT
78
110
  elsif (arg == '-p' || arg == '--port')
79
111
  sset(SERVER_PORT, argv_get(pos))
80
112
 
113
+ elsif (arg == '-v')
114
+ increase_log_level()
115
+ ARGV.delete_at(pos)
116
+
81
117
  else
82
118
  pos += 1
83
119
  end
@@ -85,10 +121,11 @@ class RCT
85
121
  end
86
122
  end
87
123
 
124
+
88
125
  def self.get_opt(info)
89
126
  pos = 0
90
- while (pos < $ARGV.length)
91
- arg = $ARGV[pos]
127
+ while (pos < ARGV.length)
128
+ arg = ARGV[pos]
92
129
  if (arg == info[0] || arg == info[1])
93
130
  return argv_get(pos)
94
131
  else
@@ -97,6 +134,7 @@ class RCT
97
134
  end
98
135
  end
99
136
 
137
+
100
138
  def self.parse_options(opts, required)
101
139
  return if opts == nil
102
140
  opts.each { |key, info|
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2012 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2012-2013 Jyri J. Virkki <jyri@virkki.com>
3
3
  #
4
4
  # This file is part of rct.
5
5
  #
@@ -17,7 +17,7 @@
17
17
  # along with rct. If not, see <http://www.gnu.org/licenses/>.
18
18
  #
19
19
 
20
- RCT_VERSION = '0.1'
20
+ RCT_VERSION = '0.3'
21
21
 
22
22
  RCT_MODE = '_mode'
23
23
  RCT_MODE_CLI = 'cli'
@@ -36,6 +36,11 @@ REQ_PARAMS = '_req_params'
36
36
  REQ_BODY = '_req_body'
37
37
  REQ_HEADERS = '_req_headers'
38
38
 
39
+ REQ_AUTH_TYPE = '_auth_type'
40
+ REQ_AUTH_TYPE_BASIC = 'basic'
41
+ REQ_AUTH_NAME = '_req_auth_name'
42
+ REQ_AUTH_PWD = '_req_auth_pwd'
43
+
39
44
  RESULT = 1
40
45
  INFO = 2
41
46
  DEBUG = 3
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2012 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2012-2013 Jyri J. Virkki <jyri@virkki.com>
3
3
  #
4
4
  # This file is part of rct.
5
5
  #
@@ -18,17 +18,18 @@
18
18
  #
19
19
 
20
20
 
21
- #--------------------------------------------------------------------------------------------------
21
+ #------------------------------------------------------------------------------
22
22
  # Provides access to rct client methods as a CLI.
23
23
  #
24
- # In order to be accessible via CLI, a client class must implement a 'cli' method which
25
- # provides info about the CLI methods and their arguments.
24
+ # In order to be accessible via CLI, a client class must implement a
25
+ # 'cli' method which provides info about the CLI methods and their
26
+ # arguments.
26
27
  #
27
28
  class RCTCLI
28
29
 
29
30
 
30
31
  def self.rct_cli
31
- method = $ARGV.shift
32
+ method = ARGV.shift
32
33
  if (method == nil || method.empty?)
33
34
  RCT.bad_invocation("no CLI class/method given!")
34
35
  end
@@ -36,7 +37,8 @@ class RCTCLI
36
37
  method =~ /([^\.]*)\.(.*)/
37
38
  class_name = $1
38
39
  method_name = $2
39
- RCT.rct_log(DEBUG, "CLI class: #{class_name}, method: #{method_name}")
40
+ RCT.log(DEBUG, "Requested [#{method}]")
41
+ RCT.log(DEBUG, "CLI class: #{class_name}, method: #{method_name}")
40
42
 
41
43
  obj = Object::const_get(class_name).new()
42
44
  begin
@@ -60,10 +62,9 @@ class RCTCLI
60
62
  $HTTP.handle_request()
61
63
  }
62
64
 
63
- puts response
65
+ RCT.log(INFO, response)
64
66
  puts response.body
65
67
  end
66
68
 
67
69
 
68
70
  end
69
-
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2012 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2012-2013 Jyri J. Virkki <jyri@virkki.com>
3
3
  #
4
4
  # This file is part of rct.
5
5
  #
@@ -21,9 +21,10 @@
21
21
  require 'httpclient'
22
22
 
23
23
 
24
- #--------------------------------------------------------------------------------------------------
25
- # This class provides the HTTP connection support for rct. This serves to hide the details
26
- # of the underlying HTTP connector class so that it can be changed conveniently if desired.
24
+ #------------------------------------------------------------------------------
25
+ # This class provides the HTTP connection support for rct. This serves
26
+ # to hide the details of the underlying HTTP connector class so that
27
+ # it can be changed conveniently if desired.
27
28
  #
28
29
  # Currently using HTTPClient:
29
30
  # http://rubydoc.info/gems/httpclient/2.1.5.2/HTTPClient
@@ -31,7 +32,7 @@ require 'httpclient'
31
32
  class RCTHTTP
32
33
 
33
34
 
34
- #------------------------------------------------------------------------------------------------
35
+ #----------------------------------------------------------------------------
35
36
  # Constructor.
36
37
  #
37
38
  def initialize
@@ -39,12 +40,14 @@ class RCTHTTP
39
40
  end
40
41
 
41
42
 
42
- #------------------------------------------------------------------------------------------------
43
+ #----------------------------------------------------------------------------
43
44
  # Handle one HTTP request.
44
45
  #
45
- # Connection parameters are all obtained from the current state (see constants.rb):
46
+ # Connection parameters are all obtained from the current state (see
47
+ # constants.rb). Destination server is specified by type (SERVER_TYPE)
48
+ # OR by hostname+port. If both are given, type takes precedence.
46
49
  #
47
- # * SERVER_TYPE - Connect to a server of this type (overrides SERVER_HOSTNAME/SERVER_PORT).
50
+ # * SERVER_TYPE - Connect to a server of this type.
48
51
  # * SERVER_HOSTNAME - Connect to this server.
49
52
  # * SERVER_PORT - Connect to this port on server.
50
53
  # * SERVER_PROTOCOL - Protocol (http/https).
@@ -96,8 +99,20 @@ class RCTHTTP
96
99
  end
97
100
  headers['User-agent'] = "rct/#{RCT_VERSION}"
98
101
 
99
- res = nil
102
+ auth = RCT.sget(REQ_AUTH_TYPE)
103
+ if (auth != nil)
104
+ if (auth == REQ_AUTH_TYPE_BASIC)
105
+ name = RCT.sget(REQ_AUTH_NAME)
106
+ pwd = RCT.sget(REQ_AUTH_PWD)
107
+ @http_client.set_auth(nil, name, pwd)
108
+ else
109
+ raise "Requested auth type '#{auth}' unknown"
110
+ end
111
+ end
112
+
113
+ show_request(method, url, headers, RCT.sget(REQ_BODY))
100
114
 
115
+ res = nil
101
116
  begin
102
117
  if (method == "GET")
103
118
  res = @http_client.get(url, nil, headers)
@@ -116,15 +131,57 @@ class RCTHTTP
116
131
  else
117
132
  raise "Method #{method} not implemented yet!"
118
133
  end
134
+
119
135
  rescue Exception => e
120
136
  response = Response.new(nil)
121
137
  response.add_error(e.to_s)
138
+ show_response(response)
122
139
  return response
123
140
  end
124
141
 
142
+ show_response(res)
125
143
  response = Response.new(res)
126
144
  return response
127
145
  end
128
146
 
129
147
 
148
+ #----------------------------------------------------------------------------
149
+ # Show verbose info about the request.
150
+ #
151
+ def show_request(method, url, headers, body)
152
+ return if (RCT.log_level < INFO)
153
+
154
+ RCT.log(INFO, "-----[ REQUEST ]---" + "-" * 60)
155
+ RCT.log(INFO, "#{method} #{url}")
156
+ headers.each { |k,v|
157
+ RCT.log(INFO, "#{k}: #{v}")
158
+ }
159
+ RCT.log(INFO, "")
160
+
161
+ if (body != nil)
162
+ RCT.log(INFO, body)
163
+ RCT.log(INFO, "")
164
+ end
165
+ end
166
+
167
+
168
+ #----------------------------------------------------------------------------
169
+ # Show verbose info about the response.
170
+ #
171
+ def show_response(res)
172
+ return if (RCT.log_level < INFO)
173
+
174
+ RCT.log(INFO, "-----[ RESPONSE ]--" + "-" * 60)
175
+ RCT.log(INFO, "#{res.to_s}")
176
+
177
+ headers = res.headers
178
+ if (headers != nil)
179
+ headers.each { |k,v|
180
+ RCT.log(INFO, "XH: #{k}: #{v}")
181
+ }
182
+ end
183
+
184
+ RCT.log(INFO, res.body)
185
+ end
186
+
130
187
  end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2012 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2012-2013 Jyri J. Virkki <jyri@virkki.com>
3
3
  #
4
4
  # This file is part of rct.
5
5
  #
@@ -18,24 +18,26 @@
18
18
  #
19
19
 
20
20
 
21
- #--------------------------------------------------------------------------------------------------
21
+ #------------------------------------------------------------------------------
22
22
  # The Response object represents the response from one HTTP call.
23
- # This Response class wraps the response object from the underlying HTTP client library and
24
- # provides assorted convenience methods.
23
+ # This Response class wraps the response object from the underlying
24
+ # HTTP client library and provides assorted convenience methods.
25
25
  #
26
26
  # Client class methods will receive a Response object back from yield.
27
27
  # Client class methods must return this Response object.
28
- #--
29
- # Currently using response from HTTPClient: http://rubydoc.info/gems/httpclient/HTTP/Message
30
- #++
28
+ #
29
+ # Currently using response from HTTPClient:
30
+ # http://rubydoc.info/gems/httpclient/HTTP/Message
31
+ #
32
+ #
31
33
  class Response
32
34
 
33
35
 
34
- #------------------------------------------------------------------------------------------------
36
+ #----------------------------------------------------------------------------
35
37
  # Constructor...
36
38
  #
37
- # The 'res' argument must never be nil for successful responses. It may be nil if this
38
- # represents an error response.
39
+ # The 'res' argument must never be nil for successful responses. It
40
+ # may be nil if this represents an error response.
39
41
  #
40
42
  def initialize(res)
41
43
  @res = res
@@ -43,9 +45,10 @@ class Response
43
45
  end
44
46
 
45
47
 
46
- #------------------------------------------------------------------------------------------------
48
+ #----------------------------------------------------------------------------
47
49
  # Flag this reponse as an error and add an error message string.
48
- # This method may be called multiple times, the error messages are appended to each other.
50
+ # This method may be called multiple times, the error messages are
51
+ # appended to each other.
49
52
  #
50
53
  def add_error(msg)
51
54
  if (@fail_msg == nil)
@@ -56,8 +59,9 @@ class Response
56
59
  end
57
60
 
58
61
 
59
- #------------------------------------------------------------------------------------------------
60
- # Returns true unless this response has been flagged as an error via add_error() method.
62
+ #----------------------------------------------------------------------------
63
+ # Returns true unless this response has been flagged as an error via
64
+ # add_error() method.
61
65
  #
62
66
  def ok
63
67
  return true if @fail_msg == nil
@@ -65,7 +69,7 @@ class Response
65
69
  end
66
70
 
67
71
 
68
- #------------------------------------------------------------------------------------------------
72
+ #----------------------------------------------------------------------------
69
73
  # Return HTTP status code (or -1 if an error)
70
74
  #
71
75
  def status
@@ -74,7 +78,7 @@ class Response
74
78
  end
75
79
 
76
80
 
77
- #------------------------------------------------------------------------------------------------
81
+ #----------------------------------------------------------------------------
78
82
  # Return requested HTTP header (or nil if an error)
79
83
  #
80
84
  def header(name)
@@ -83,7 +87,16 @@ class Response
83
87
  end
84
88
 
85
89
 
86
- #------------------------------------------------------------------------------------------------
90
+ #----------------------------------------------------------------------------
91
+ # Return all HTTP headers (or nil if an error)
92
+ #
93
+ def headers
94
+ return nil if @res == nil
95
+ return @res.header
96
+ end
97
+
98
+
99
+ #----------------------------------------------------------------------------
87
100
  # Return HTTP response body (or nil if an error)
88
101
  #
89
102
  def body
@@ -92,9 +105,10 @@ class Response
92
105
  end
93
106
 
94
107
 
95
- #------------------------------------------------------------------------------------------------
96
- # Return short string representation. On error, contains the error message(s). Otherwise,
97
- # contains the HTTP response code and text description.
108
+ #----------------------------------------------------------------------------
109
+ # Return short string representation. On error, contains the error
110
+ # message(s). Otherwise, contains the HTTP response code and text
111
+ # description.
98
112
  #
99
113
  def to_s
100
114
  return "error: #{@fail_msg}" if @res == nil
@@ -18,20 +18,22 @@
18
18
  #
19
19
 
20
20
 
21
- #--------------------------------------------------------------------------------------------------
21
+ #------------------------------------------------------------------------------
22
22
  # This is the global state where mostly everything is kept in rct.
23
23
  #
24
- # There are two kinds of state, permanent and temporary. Values set in temporary state can be
25
- # removed with the reset() method. Values in permanent state remain available for the lifetime
26
- # of this State object (unless individually removed with delete() method).
24
+ # There are two kinds of state, permanent and temporary. Values set in
25
+ # temporary state can be removed with the reset() method. Values in
26
+ # permanent state remain available for the lifetime of this State
27
+ # object (unless individually removed with delete() method).
27
28
  #
28
- # When retrieving a value, content in the temporary state overrides content in permanent state
29
- # if the key is present in both. This provides a way to temporarily override a global default.
29
+ # When retrieving a value, content in the temporary state overrides
30
+ # content in permanent state if the key is present in both. This
31
+ # provides a way to temporarily override a global default.
30
32
  #
31
33
  class State
32
34
 
33
35
 
34
- #------------------------------------------------------------------------------------------------
36
+ #----------------------------------------------------------------------------
35
37
  # Constructor...
36
38
  #
37
39
  def initialize
@@ -40,7 +42,7 @@ class State
40
42
  end
41
43
 
42
44
 
43
- #------------------------------------------------------------------------------------------------
45
+ #----------------------------------------------------------------------------
44
46
  # Set (or change) 'key' in state to contain 'value'.
45
47
  # If 'temp' is true, this key is stored in temporary state only.
46
48
  #
@@ -53,7 +55,7 @@ class State
53
55
  end
54
56
 
55
57
 
56
- #------------------------------------------------------------------------------------------------
58
+ #----------------------------------------------------------------------------
57
59
  # Get the value of 'key' (if available, or returns nil).
58
60
  #
59
61
  def get(key)
@@ -65,7 +67,7 @@ class State
65
67
  end
66
68
 
67
69
 
68
- #------------------------------------------------------------------------------------------------
70
+ #----------------------------------------------------------------------------
69
71
  # Delete the given 'key' from both permanent and temporary state.
70
72
  #
71
73
  def delete(key)
@@ -74,7 +76,7 @@ class State
74
76
  end
75
77
 
76
78
 
77
- #------------------------------------------------------------------------------------------------
79
+ #----------------------------------------------------------------------------
78
80
  # Reset temporary state.
79
81
  # Returns the old temporary state hash object.
80
82
  #
@@ -22,17 +22,18 @@ require 'securerandom'
22
22
  require 'json'
23
23
 
24
24
 
25
- #--------------------------------------------------------------------------------------------------
25
+ #------------------------------------------------------------------------------
26
26
  # Base class for rct client classes.
27
27
  #
28
28
  class RCTClient
29
29
 
30
30
 
31
- #------------------------------------------------------------------------------------------------
32
- # Adds an additional query param "name=value" to the given 'params'. This 'params' may be nil.
31
+ #----------------------------------------------------------------------------
32
+ # Adds an additional query param "name=value" to the given 'params'.
33
+ # This 'params' may be nil.
33
34
  #
34
- # The new param is added only if both name and value are non-nil, otherwise params is
35
- # returned unchanged.
35
+ # The new param is added only if both name and value are non-nil,
36
+ # otherwise params is returned unchanged.
36
37
  #
37
38
  def add_param(params, name, value)
38
39
  if (name == nil || name.empty? || value == nil || value.empty?)
@@ -47,7 +48,7 @@ class RCTClient
47
48
  end
48
49
 
49
50
 
50
- #------------------------------------------------------------------------------------------------
51
+ #----------------------------------------------------------------------------
51
52
  # Returns a random UUID.
52
53
  #
53
54
  def uuid
@@ -55,8 +56,9 @@ class RCTClient
55
56
  end
56
57
 
57
58
 
58
- #------------------------------------------------------------------------------------------------
59
- # Return true if var contains something (that is, it is not nil and not empty).
59
+ #----------------------------------------------------------------------------
60
+ # Return true if var contains something (that is, it is not nil and
61
+ # not empty).
60
62
  #
61
63
  def set(var)
62
64
  return false if var == nil
@@ -65,9 +67,9 @@ class RCTClient
65
67
  end
66
68
 
67
69
 
68
- #------------------------------------------------------------------------------------------------
69
- # If the global state contains a non-nil/non-empty value for key 'name', add this to the
70
- # 'hash' provided.
70
+ #----------------------------------------------------------------------------
71
+ # If the global state contains a non-nil/non-empty value for key
72
+ # 'name', add it to the 'hash' provided.
71
73
  #
72
74
  def add_to_hash_if_set(name, hash)
73
75
  value = RCT.sget(name)
@@ -78,7 +80,7 @@ class RCTClient
78
80
  end
79
81
 
80
82
 
81
- #------------------------------------------------------------------------------------------------
83
+ #----------------------------------------------------------------------------
82
84
  # Log a message. Level is one of RESULT, INFO, DEBUG.
83
85
  #
84
86
  def log(level, line)
@@ -86,7 +88,7 @@ class RCTClient
86
88
  end
87
89
 
88
90
 
89
- #------------------------------------------------------------------------------------------------
91
+ #----------------------------------------------------------------------------
90
92
  # Set (or change) 'key' in state to contain 'value'.
91
93
  # If 'temp' is true, this key is stored in temporary state only.
92
94
  #
@@ -95,7 +97,7 @@ class RCTClient
95
97
  end
96
98
 
97
99
 
98
- #------------------------------------------------------------------------------------------------
100
+ #----------------------------------------------------------------------------
99
101
  # Set (or change) 'key' in temporary state to contain 'value'.
100
102
  #
101
103
  def ssettmp(key, value)
@@ -103,7 +105,7 @@ class RCTClient
103
105
  end
104
106
 
105
107
 
106
- #------------------------------------------------------------------------------------------------
108
+ #----------------------------------------------------------------------------
107
109
  # Get the value of 'key' (if available, or returns nil).
108
110
  #
109
111
  def sget(key)
@@ -111,7 +113,7 @@ class RCTClient
111
113
  end
112
114
 
113
115
 
114
- #------------------------------------------------------------------------------------------------
116
+ #----------------------------------------------------------------------------
115
117
  # Delete the given 'key' from both permanent and temporary state.
116
118
  #
117
119
  def sdelete(key)
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: rct
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: '0.1'
5
+ version: '0.3'
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jyri J. Virkki
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-17 00:00:00.000000000 Z
12
+ date: 2013-06-11 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: wip
15
15
  email: jyri@virkki.com
@@ -19,13 +19,13 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - lib/rct/baseutils.rb
22
- - lib/rct/constants.rb
23
22
  - lib/rct/rct_cli.rb
24
- - lib/rct/rct_http.rb
25
- - lib/rct/response.rb
23
+ - lib/rct/constants.rb
26
24
  - lib/rct/state.rb
27
- - lib/rct.rb
25
+ - lib/rct/response.rb
26
+ - lib/rct/rct_http.rb
28
27
  - lib/rct_client.rb
28
+ - lib/rct.rb
29
29
  - bin/rct
30
30
  homepage: https://github.com/jvirkki/rct
31
31
  licenses: