arcus 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/arcus CHANGED
@@ -1,29 +1,37 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'arcus/cli'
4
+ require 'uri'
4
5
  require 'yaml'
5
6
 
7
+ RC_FILE = "#{Dir.home}/.arcusrc"
6
8
 
7
- rc_file = "#{Dir.home}/.arcusrc"
8
9
  $settings = {}
9
- if (File.exists? rc_file)
10
- $settings = YAML.load_file(rc_file)
10
+ if (File.exists? RC_FILE)
11
+ $settings = YAML.load_file(RC_FILE)
11
12
  end
12
13
 
13
14
  def process_env(setting, key)
14
- if ENV.has_key?(key) && ! ENV[key].empty?
15
+ if ENV.has_key?(key) && !ENV[key].empty?
15
16
  $settings[setting] = ENV[key]
16
17
  end
17
18
  end
18
19
 
19
20
  process_env("api_uri", "ARCUS_API_URI")
20
21
  process_env("default_response", "ARCUS_DEFAULT_RESPONSE")
21
-
22
- error = if ! $settings.has_key? "api_uri"
23
- "API URI must me set in either #{rc_file} or as a environmental variable (ARCUS_API_URI)"
24
- elsif ! $settings.has_key? "default_response"
25
- "Default response must me set in either #{rc_file} or as a environmental variable (ARCUS_DEFAULT_RESPONSE)"
26
- end
22
+ process_env("api_key", "ARCUS_API_KEY")
23
+ process_env("api_secret", "ARCUS_API_SECRET")
24
+
25
+ error = case
26
+ when !$settings.has_key?("api_uri")
27
+ "API URI must me set in either #{rc_file} or as a environmental variable (ARCUS_API_URI)"
28
+ when !$settings.has_key?("default_response")
29
+ "Default response must me set in either #{rc_file} or as a environmental variable (ARCUS_DEFAULT_RESPONSE)"
30
+ when $settings["api_key"] && !$settings["api_secret"]
31
+ "API Secret must be set when using an API Key"
32
+ when $settings["api_secret"] && !settings["api_key"]
33
+ "API Key must be set when using an API Secret"
34
+ end
27
35
 
28
36
  if error
29
37
  puts error
@@ -32,7 +40,9 @@ end
32
40
 
33
41
  arcus = Arcus::Cli.new(
34
42
  :api_uri => URI($settings["api_uri"]),
35
- :default_response => $settings["default_response"]
43
+ :default_response => $settings["default_response"],
44
+ :api_key => $settings["api_key"],
45
+ :api_secret => $settings["api_secret"]
36
46
  )
37
47
 
38
48
  arcus.run
data/lib/arcus/api.rb CHANGED
@@ -20,23 +20,25 @@ module Arcus
20
20
  module Api
21
21
 
22
22
  @@targets = []
23
- @@verbose = false
24
- @@default_response = "json"
25
23
 
26
- def self.verbose=(val)
27
- @@verbose = val
28
- end
24
+ class Settings
25
+ attr_accessor :api_uri, :api_xml, :api_key, :api_secret, :default_response, :verbose
29
26
 
30
- def self.verbose
31
- @@verbose
27
+ def initialize
28
+ @api_xml = File.dirname(__FILE__) + "/commands.xml"
29
+ @default_response = "json"
30
+ @verbose = false;
31
+ end
32
32
  end
33
33
 
34
- def self.default_response=(val)
35
- @@default_response = val
34
+ @@settings = Settings.new
35
+
36
+ def self.settings
37
+ @@settings
36
38
  end
37
39
 
38
- def self.default_response
39
- @@default_response
40
+ def self.settings=(val)
41
+ @@settings = val
40
42
  end
41
43
 
42
44
  def self.targets
@@ -64,8 +66,8 @@ module Arcus
64
66
  http = Net::HTTP.new(@api_uri.host, @api_uri.port)
65
67
  http.read_timeout = 5
66
68
  http.open_timeout = 1
67
- req_url = api_uri.path + "?" + URI.encode_www_form(params.merge({:command => @command_name}))
68
- Arcus.log.debug { "Sending: #{req_url}" } if Api.verbose == true
69
+ req_url = @api_uri.path + "?" + URI.encode_www_form(params.merge({:command => @command_name}))
70
+ Arcus.log.debug { "Sending: #{req_url}" } if Api.settings.verbose
69
71
  response = begin
70
72
  http.get(req_url)
71
73
  rescue Timeout::Error => e
@@ -77,7 +79,7 @@ module Arcus
77
79
  e.api_uri = @api_uri
78
80
  raise e
79
81
  end
80
- Arcus.log.debug { "Received: #{response.body}" } if Api.verbose == true
82
+ Arcus.log.debug { "Received: #{response.body}" } if Api.settings.verbose
81
83
  response.instance_eval do
82
84
  class << self
83
85
  attr_accessor :response_type
@@ -121,6 +123,15 @@ module Arcus
121
123
  end
122
124
  end
123
125
 
126
+ class Scope
127
+ attr_accessor :options
128
+
129
+ def initialize(options = {})
130
+ @options = options;
131
+ end
132
+ end
133
+
134
+
124
135
  def self.parse_action(name)
125
136
  action, target = name.split(/([A-Z].*)/, 2)
126
137
  target = "User" if %w(login logout).include?(action)
@@ -128,7 +139,7 @@ module Arcus
128
139
  [action, target]
129
140
  end
130
141
 
131
- def self.load_config(api_xml)
142
+ def self.load_config_file(api_xml)
132
143
  contents = File.read(api_xml).to_s
133
144
  md5 = Digest::MD5.hexdigest(contents)
134
145
  api_xml_basename = File.basename(api_xml)
@@ -148,20 +159,25 @@ module Arcus
148
159
  config
149
160
  end
150
161
 
151
- def self.configure(options = {})
162
+ def scoped(options = {})
163
+ yield Scope.new(options = {})
164
+ end
165
+
166
+ def self.configure
152
167
  Arcus.log = Logger.new(STDOUT)
153
168
  Arcus.log.level = Logger::DEBUG
154
169
  Arcus.log.formatter = proc do |severity, datetime, progname, msg|
155
170
  "#{severity} - #{datetime}: #{msg}\n"
156
171
  end
157
- api_xml = options[:api_xml] || (raise ArgumentError, "api_xml file: commands.xml file location required")
158
- api_uri = options[:api_uri] || (raise ArgumentError, "api_uri: URI of API required")
159
- @@default_response = options[:default_response] if options[:default_response]
160
- to_name = lambda { |a| a["name"].to_sym }
161
172
 
162
- config = self.load_config(api_xml)
173
+ yield(self.settings)
174
+
175
+ self.settings.api_xml || (raise ArgumentError, "api_xml file: commands.xml file location required")
176
+ self.settings.api_uri || (raise ArgumentError, "api_uri: URI of API required")
163
177
 
164
- config["commands"]["command"].each do |c|
178
+ api_config = self.load_config_file(self.settings.api_xml)
179
+
180
+ api_config["commands"]["command"].each do |c|
165
181
  command_name = c["name"]
166
182
  command_desc = c["description"]
167
183
  command_async = c["isAsync"] == "true" ? true : false
@@ -182,9 +198,6 @@ module Arcus
182
198
  required_args = all_args.select { |a| a["required"] == "true" }
183
199
  optional_args = all_args.select { |a| a["required"] == "false" }
184
200
 
185
- all_names = all_args.map(&to_name)
186
- required_names = required_args.map(&to_name)
187
-
188
201
  def self.class_exists?(class_name)
189
202
  c = self.const_get(class_name)
190
203
  return c.is_a?(Class)
@@ -204,30 +217,30 @@ module Arcus
204
217
 
205
218
  if !self.class_exists?(target)
206
219
  target_clazz = self.create_class(target, Target) do
207
- cattr_accessor :actions, :opts
208
- self.actions = self.opts = []
220
+ cattr_accessor :actions
221
+ self.actions = []
209
222
  end
210
223
  @@targets << target_clazz
224
+
211
225
  else
212
226
  target_clazz = self.get_class(target)
213
227
  end
214
228
 
215
-
216
229
  target_clazz.class_eval do
230
+
217
231
  if !class_exists?(action.capitalize)
218
232
  action_clazz = create_class(action.capitalize, Action) do
219
- cattr_accessor :opts
220
- self.opts = []
221
- end
222
- target_clazz.actions << action_clazz
223
- else
224
- action_clazz = get_class(action.capitalize)
225
- end
226
- action_clazz.class_eval do
227
- class << self
228
- attr_accessor :name, :description, :is_async,
229
- :required_args, :optional_args,
230
- :action_name, :api_uri, :sync
233
+ cattr_accessor :name, :description, :is_async,
234
+ :required_args, :optional_args,
235
+ :action_name, :sync
236
+
237
+ self.name = command_name
238
+ self.description = command_desc
239
+ self.action_name = action
240
+ self.required_args = required_args
241
+ self.optional_args = optional_args
242
+ self.is_async = command_async
243
+ self.sync = false
231
244
 
232
245
  def prepare!(params = {}, callbacks = {})
233
246
  check_args(params, all_names, required_names)
@@ -235,30 +248,21 @@ module Arcus
235
248
  end
236
249
 
237
250
  def prepare(params = {}, callbacks = {})
238
- params[:response] ||= Api.default_response
239
- Request.new(self.name, params, self.api_uri, callbacks)
251
+ params[:response] ||= Api.settings.default_response
252
+ Request.new(self.name, params, Api.settings.api_uri, callbacks)
240
253
  end
241
254
  end
242
- end
243
- action_clazz.name = command_name
244
- action_clazz.description = command_desc
245
- action_clazz.action_name = action
246
- action_clazz.required_args = required_args
247
- action_clazz.optional_args = optional_args
248
- action_clazz.api_uri = api_uri
249
- action_clazz.is_async = command_async
250
- action_clazz.sync = false
251
-
252
-
253
- define_method(action.to_sym) do |params = {}, callbacks = {}|
254
- action_clazz.prepare(params, callbacks)
255
- end
256
- define_method("#{action}!".to_sym) do |params = {}, callbacks = {}|
257
- action_clazz.prepare(params, callbacks)
258
- end
255
+ target_clazz.actions << action_clazz
259
256
 
257
+ target_clazz.define_singleton_method(action.to_sym) do |params = {}, callbacks = {}|
258
+ action_clazz.new.prepare(params, callbacks)
259
+ end
260
+ target_clazz.define_singleton_method("#{action}!".to_sym) do |params = {}, callbacks = {}|
261
+ action_clazz.new.prepare(params, callbacks)
262
+ end
263
+ end
260
264
  end
261
265
  end
262
266
  end
263
267
  end
264
- end
268
+ end
data/lib/arcus/cli.rb CHANGED
@@ -8,8 +8,12 @@ module Arcus
8
8
  include Api
9
9
 
10
10
  def initialize(options = {})
11
- options[:api_xml] = File.dirname(__FILE__) + "/commands.xml"
12
- Api.configure(options)
11
+ Api.configure do |c|
12
+ c.api_uri = options[:api_uri] if options[:api_uri]
13
+ c.api_key = options[:api_key] if options[:api_key]
14
+ c.api_secret = options[:api_secret] if options[:api_secret]
15
+ c.default_response = options[:default_response] if options[:default_response]
16
+ end
13
17
  end
14
18
 
15
19
  def word_wrap(text, *args)
@@ -51,7 +55,7 @@ module Arcus
51
55
  cmd.program_version = [0, 0, 1]
52
56
  cmd.options = CmdParse::OptionParserWrapper.new do |opt|
53
57
  opt.separator "Global options:"
54
- opt.on("--verbose", "Be verbose when outputting info") { Arcus::Api.verbose = true }
58
+ opt.on("--verbose", "Be verbose when outputting info") { Arcus::Api.settings.verbose = true }
55
59
  end
56
60
  cmd.add_command(CmdParse::HelpCommand.new)
57
61
  cmd.add_command(CmdParse::VersionCommand.new)
@@ -97,7 +101,7 @@ module Arcus
97
101
  missing_params = a.required_args.map { |n| n["name"].to_sym } - required_params.keys
98
102
  if (missing_params.empty?)
99
103
  begin
100
- response_type = optional_params[:response] || Api.default_response
104
+ response_type = optional_params[:response] || Api.settings.default_response
101
105
  if a.sync
102
106
  result = a.prepare(required_params.merge(optional_params)).fetch
103
107
  job_id = result["#{a.name.downcase}response"]["jobid"]
@@ -111,7 +115,7 @@ module Arcus
111
115
  end
112
116
  puts AsyncJobResult.new.query({:jobid => job_id}).fetch(response_type.to_sym)
113
117
  else
114
- puts a.prepare(required_params.merge(optional_params)).fetch(response_type.to_sym)
118
+ puts a.new.prepare(required_params.merge(optional_params)).fetch(response_type.to_sym)
115
119
  end
116
120
  rescue Timeout::Error => e
117
121
  puts "Timeout connecting to #{e.api_uri}"
data/lib/arcus/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Arcus
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/test.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Test
2
+ attr_accessor :hello
3
+ end
4
+
5
+ t = Test.new
6
+ t.hello = "HELLO"
7
+ p t
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require 'hola'
3
+
4
+ class HolaTest < Test::Unit::TestCase
5
+ def test_english_hello
6
+ assert_equal "hello world",
7
+ Hola.hi("english")
8
+ end
9
+
10
+ def test_any_hello
11
+ assert_equal "hello world",
12
+ Hola.hi("ruby")
13
+ end
14
+
15
+ def test_spanish_hello
16
+ assert_equal "hola mundo",
17
+ Hola.hi("spanish")
18
+ end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arcus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -98,6 +98,8 @@ files:
98
98
  - lib/arcus/commands.xml
99
99
  - lib/arcus/helpers.rb
100
100
  - lib/arcus/version.rb
101
+ - test.rb
102
+ - test/test_arcus.rb
101
103
  homepage: ''
102
104
  licenses: []
103
105
  post_install_message:
@@ -122,4 +124,5 @@ rubygems_version: 1.8.23
122
124
  signing_key:
123
125
  specification_version: 3
124
126
  summary: API and client CLI tool for cloudstack
125
- test_files: []
127
+ test_files:
128
+ - test/test_arcus.rb