arcus 0.0.3 → 0.0.4

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/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ end
8
+ desc "Run tests"
9
+
10
+ require 'rspec/core/rake_task'
11
+ RSpec::Core::RakeTask.new('spec')
@@ -15,8 +15,10 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Arcus::VERSION
17
17
 
18
+ gem.add_dependency "i18n"
18
19
  gem.add_dependency "active_support"
19
20
  gem.add_dependency "nori"
20
21
  gem.add_dependency "nokogiri"
21
22
  gem.add_dependency "cmdparse"
23
+ gem.add_dependency "rspec"
22
24
  end
data/bin/arcus CHANGED
@@ -29,7 +29,7 @@ error = case
29
29
  "Default response must me set in either #{rc_file} or as a environmental variable (ARCUS_DEFAULT_RESPONSE)"
30
30
  when $settings["api_key"] && !$settings["api_secret"]
31
31
  "API Secret must be set when using an API Key"
32
- when $settings["api_secret"] && !settings["api_key"]
32
+ when $settings["api_secret"] && !$settings["api_key"]
33
33
  "API Key must be set when using an API Secret"
34
34
  end
35
35
 
@@ -1,10 +1,2 @@
1
1
  require "arcus/version"
2
2
  require "arcus/api"
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
@@ -5,6 +5,9 @@ require 'active_support/core_ext/hash/reverse_merge'
5
5
  require 'nori'
6
6
  require 'nokogiri'
7
7
  require 'digest/md5'
8
+ require 'base64'
9
+ require 'cgi'
10
+ require 'openssl'
8
11
  require 'logger'
9
12
  require 'json'
10
13
  require 'arcus/helpers'
@@ -46,15 +49,43 @@ module Arcus
46
49
  end
47
50
 
48
51
  class Request
49
- attr_accessor :command_name, :params, :api_uri, :callbacks
52
+ attr_accessor :command_name, :params, :api_uri, :callbacks, :api_key, :api_secret
50
53
 
51
- def initialize(command_name, params, api_uri, callbacks)
54
+ def initialize(command_name, params, callbacks)
52
55
  @command_name = command_name
53
56
  @params = params
54
- @api_uri = api_uri
57
+ @api_uri = Api.settings.api_uri
58
+ @api_key = Api.settings.api_key
59
+ @api_secret = Api.settings.api_secret
55
60
  @callbacks = callbacks
56
61
  end
57
62
 
63
+ def generate_signature(params, api_secret)
64
+ sorted_params = params.map { |k, v| [k, CGI.escape(v.to_s).gsub('+', '%20').downcase] }.sort_by { |key, value| key.to_s }
65
+ data = parameterize(sorted_params, false).downcase
66
+ hash = OpenSSL::HMAC.digest('sha1', api_secret, data)
67
+ Base64.encode64(hash).chomp
68
+ end
69
+
70
+ def parameterize(params, escape=true)
71
+ params = params.collect do |k, v|
72
+ if escape
73
+ "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
74
+ else
75
+ "#{k}=#{v}"
76
+ end
77
+ end
78
+ params.join('&')
79
+ end
80
+
81
+ def generate_params_str(params, api_key = nil, api_secret = nil)
82
+ if api_key && api_secret
83
+ params[:apiKey] = @api_key
84
+ params[:signature] = generate_signature(params, api_secret)
85
+ end
86
+ parameterize(params)
87
+ end
88
+
58
89
  def fetch(response_type = :object)
59
90
  @params[:response] =
60
91
  case response_type
@@ -65,8 +96,14 @@ module Arcus
65
96
  end
66
97
  http = Net::HTTP.new(@api_uri.host, @api_uri.port)
67
98
  http.read_timeout = 5
68
- http.open_timeout = 1
69
- req_url = @api_uri.path + "?" + URI.encode_www_form(params.merge({:command => @command_name}))
99
+ http.open_timeout = 5
100
+
101
+ if @api_uri.scheme == "https"
102
+ http.use_ssl = true
103
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
104
+ end
105
+ params = @params.merge({:command => @command_name})
106
+ req_url = @api_uri.path + "?" + generate_params_str(params, @api_key, @api_secret)
70
107
  Arcus.log.debug { "Sending: #{req_url}" } if Api.settings.verbose
71
108
  response = begin
72
109
  http.get(req_url)
@@ -249,16 +286,18 @@ module Arcus
249
286
 
250
287
  def prepare(params = {}, callbacks = {})
251
288
  params[:response] ||= Api.settings.default_response
252
- Request.new(self.name, params, Api.settings.api_uri, callbacks)
289
+ Request.new(self.name, params, callbacks)
253
290
  end
254
291
  end
255
292
  target_clazz.actions << action_clazz
256
293
 
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)
294
+ target_clazz.instance_eval do
295
+ define_method(action.to_sym) do |params = {}, callbacks = {}|
296
+ action_clazz.new.prepare(params, callbacks)
297
+ end
298
+ define_method("#{action}!".to_sym) do |params = {}, callbacks = {}|
299
+ action_clazz.new.prepare(params, callbacks)
300
+ end
262
301
  end
263
302
  end
264
303
  end
@@ -103,7 +103,7 @@ module Arcus
103
103
  begin
104
104
  response_type = optional_params[:response] || Api.settings.default_response
105
105
  if a.sync
106
- result = a.prepare(required_params.merge(optional_params)).fetch
106
+ result = a.new.prepare(required_params.merge(optional_params)).fetch
107
107
  job_id = result["#{a.name.downcase}response"]["jobid"]
108
108
  job_finished = false
109
109
  until job_finished
@@ -1,3 +1,3 @@
1
1
  module Arcus
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+ require 'rspec/core'
3
+
4
+ Arcus::Api.configure do |c|
5
+ c.api_uri = "http://icloud-staging-api.logicworks.net:8096/client/api"
6
+ c.default_response = "json"
7
+ end
8
+
9
+ include Arcus::Api
10
+
11
+ describe Domain do
12
+ pending "Domain test"
13
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'arcus/api'
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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-22 00:00:00.000000000 Z
12
+ date: 2012-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: active_support
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -75,6 +91,22 @@ dependencies:
75
91
  - - ! '>='
76
92
  - !ruby/object:Gem::Version
77
93
  version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
78
110
  description: API and client CLI tool for cloudstack
79
111
  email:
80
112
  - atistler@gmail.com
@@ -98,8 +130,9 @@ files:
98
130
  - lib/arcus/commands.xml
99
131
  - lib/arcus/helpers.rb
100
132
  - lib/arcus/version.rb
133
+ - spec/arcus_spec.rb
134
+ - spec/spec_helper.rb
101
135
  - test.rb
102
- - test/test_arcus.rb
103
136
  homepage: ''
104
137
  licenses: []
105
138
  post_install_message:
@@ -125,4 +158,5 @@ signing_key:
125
158
  specification_version: 3
126
159
  summary: API and client CLI tool for cloudstack
127
160
  test_files:
128
- - test/test_arcus.rb
161
+ - spec/arcus_spec.rb
162
+ - spec/spec_helper.rb
@@ -1,19 +0,0 @@
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