deltacloud-client 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/deltacloud.rb +35 -31
  2. data/lib/documentation.rb +22 -77
  3. metadata +2 -2
@@ -23,7 +23,7 @@ require 'logger'
23
23
  module DeltaCloud
24
24
 
25
25
  # Get a new API client instance
26
- #
26
+ #
27
27
  # @param [String, user_name] API user name
28
28
  # @param [String, password] API password
29
29
  # @param [String, user_name] API URL (eg. http://localhost:3001/api)
@@ -39,17 +39,30 @@ module DeltaCloud
39
39
  API.new(nil, nil, url).driver_name
40
40
  end
41
41
 
42
+ def self.define_class(name)
43
+ @defined_classes ||= []
44
+ if @defined_classes.include?(name)
45
+ self.module_eval("API::#{name}")
46
+ else
47
+ @defined_classes << name unless @defined_classes.include?(name)
48
+ API.const_set(name, Class.new)
49
+ end
50
+ end
51
+
52
+ def self.classes
53
+ @defined_classes || []
54
+ end
55
+
42
56
  class API
43
57
  attr_accessor :logger
44
58
  attr_reader :api_uri, :driver_name, :api_version, :features, :entry_points
45
- attr_reader :classes
46
59
 
47
60
  def initialize(user_name, password, api_url, opts={}, &block)
61
+ opts[:version] = true
48
62
  @logger = opts[:verbose] ? Logger.new(STDERR) : []
49
63
  @username, @password = user_name, password
50
64
  @api_uri = URI.parse(api_url)
51
65
  @features, @entry_points = {}, {}
52
- @classes = []
53
66
  @verbose = opts[:verbose] || false
54
67
  discover_entry_points
55
68
  yield self if block_given?
@@ -77,7 +90,7 @@ module DeltaCloud
77
90
  define_method model do |*args|
78
91
  request(:get, "/#{model}", args.first) do |response|
79
92
  # Define a new class based on model name
80
- c = Kernel.define_class("#{model.to_s.classify}")
93
+ c = DeltaCloud.define_class("#{model.to_s.classify}")
81
94
  # Create collection from index operation
82
95
  base_object_collection(c, model, response)
83
96
  end
@@ -86,7 +99,7 @@ module DeltaCloud
86
99
  define_method :"#{model.to_s.singularize}" do |*args|
87
100
  request(:get, "/#{model}/#{args[0]}") do |response|
88
101
  # Define a new class based on model name
89
- c = Kernel.define_class("#{model.to_s.classify}")
102
+ c = DeltaCloud.define_class("#{model.to_s.classify}")
90
103
  # Build class for returned object
91
104
  base_object(c, model, response)
92
105
  end
@@ -201,7 +214,6 @@ module DeltaCloud
201
214
  end
202
215
  end
203
216
  end
204
- add_class_record(obj)
205
217
  return obj
206
218
  end
207
219
 
@@ -260,7 +272,7 @@ module DeltaCloud
260
272
  instance = nil
261
273
 
262
274
  request(:post, entry_points[:instances], {}, params) do |response|
263
- c = Kernel.define_class("Instance")
275
+ c = DeltaCloud.define_class("Instance")
264
276
  instance = base_object(c, :instance, response)
265
277
  yield instance if block_given?
266
278
  end
@@ -356,11 +368,6 @@ module DeltaCloud
356
368
  }
357
369
  end
358
370
 
359
- def add_class_record(obj)
360
- return if self.classes.include?(obj.class)
361
- self.classes << obj.class
362
- end
363
-
364
371
  end
365
372
 
366
373
  class Documentation
@@ -429,7 +436,7 @@ module DeltaCloud
429
436
  attr_reader :name, :unit, :value, :kind
430
437
 
431
438
  def initialize(xml, name)
432
- @kind, @value, @unit = xml['kind'].to_sym, xml['value'], xml['unit']
439
+ @name, @kind, @value, @unit = xml['name'], xml['kind'].to_sym, xml['value'], xml['unit']
433
440
  declare_ranges(xml)
434
441
  self
435
442
  end
@@ -470,19 +477,25 @@ end
470
477
 
471
478
  class String
472
479
 
473
- # Create a class name from string
474
- def classify
475
- self.singularize.camelize
480
+ unless method_defined?(:classify)
481
+ # Create a class name from string
482
+ def classify
483
+ self.singularize.camelize
484
+ end
476
485
  end
477
486
 
478
- # Camelize converts strings to UpperCamelCase
479
- def camelize
480
- self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
487
+ unless method_defined?(:camelize)
488
+ # Camelize converts strings to UpperCamelCase
489
+ def camelize
490
+ self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
491
+ end
481
492
  end
482
493
 
483
- # Strip 's' character from end of string
484
- def singularize
485
- self.gsub(/s$/, '')
494
+ unless method_defined?(:singularize)
495
+ # Strip 's' character from end of string
496
+ def singularize
497
+ self.gsub(/s$/, '')
498
+ end
486
499
  end
487
500
 
488
501
  # Convert string to float if string value seems like Float
@@ -497,12 +510,3 @@ class String
497
510
  end
498
511
 
499
512
  end
500
-
501
- module Kernel
502
-
503
- # Get defined class or declare a new one, when class was not declared before
504
- def define_class(name)
505
- DeltaCloud.const_get(name) rescue DeltaCloud.const_set(name, Class.new)
506
- end
507
-
508
- end
@@ -1,4 +1,5 @@
1
1
  require 'lib/deltacloud'
2
+ require 'ap'
2
3
 
3
4
  skip_methods = [ "id=", "uri=" ]
4
5
 
@@ -12,86 +13,30 @@ end
12
13
  @dc.entry_points.keys.each do |ep|
13
14
  @dc.send(ep)
14
15
  end
15
- class_list = @dc.classes
16
-
17
- def read_method_description(c, method)
18
- if method =~ /es$/
19
- " # Read #{c.downcase} collection from Deltacloud API"
20
- else
21
- case method
22
- when "uri"
23
- " # Return URI to API for this object"
24
- when "action_urls"
25
- " # Return available actions API URL"
26
- when "client"
27
- " # Return instance of API client"
28
- else
29
- " # Get #{method} attribute value from #{c.downcase}"
30
- end
31
- end
32
- end
33
-
34
- def read_parameters(c, method)
35
- out = []
36
- if method =~ /es$/
37
- out << " # @param [String, #id] Filter by ID"
38
- end
39
- out.join("\n")
40
- end
41
16
 
42
- def read_return_value(c, method)
43
- if method =~ /es$/
44
- rt = "Array"
45
- else
46
- rt = "String"
47
- end
48
- " # @return [String] Value of #{method}"
49
- end
17
+ class_list = @dc.classes
50
18
 
51
19
  out = []
52
20
 
53
- class_list.each do |c|
54
- class_name = "#{c}".gsub(/^DeltaCloud::/, '')
55
- out << "module DeltaCloud"
56
- out << " class API"
57
- @dc.entry_points.keys.each do |ep|
58
- out << "# Return #{ep.to_s.classify} object with given id\n"
59
- out << "# "
60
- out << "# *#{@dc.documentation(ep.to_s).description}*"
61
- out << "# @return [#{ep.to_s.classify}]"
62
- out << "def #{ep.to_s.gsub(/s$/, '')}"
63
- out << "end"
64
- out << "# Return collection of #{ep.to_s.classify} objects"
65
- out << "# "
66
- out << "# *#{@dc.documentation(ep.to_s).description}*"
67
- @dc.documentation(ep.to_s, 'index').params.each do |p|
68
- out << p.to_comment
69
- end
70
- out << "# @return [Array] [#{ep.to_s.classify}]"
71
- out << "def #{ep}(opts={})"
72
- out << "end"
73
- end
74
- out << " end"
75
- out << " class #{class_name}"
76
- c.instance_methods(false).each do |method|
77
- next if skip_methods.include?(method)
78
- params = read_parameters(class_name, method)
79
- retval = read_return_value(class_name, method)
80
- out << read_method_description(class_name, method)
81
- out << params if params
82
- out << retval if retval
83
- out << " def #{method}"
84
- out << " # This method was generated dynamically from API"
85
- out << " end\n"
86
- end
87
- out << " end"
21
+ out << "module DeltaCloud"
22
+ out << "class API"
23
+ @dc.entry_points.keys.each do |method|
24
+ out << "# @return [#{method.to_s.singularize.classify}]"
25
+ out << "def #{method}(opts={})"
26
+ out << "end"
27
+ out << "# @return #{method.to_s.singularize.classify}"
28
+ out << "def #{method.to_s.singularize}(id)"
88
29
  out << "end"
89
30
  end
90
-
91
- FileUtils.rm_r('doc') rescue nil
92
- FileUtils.mkdir_p('doc')
93
- File.open('doc/deltacloud.rb', 'w') do |f|
94
- f.puts(out.join("\n"))
95
- end
96
- system("yardoc -m markdown --readme README --title 'Deltacloud Client Library' 'lib/*.rb' 'doc/deltacloud.rb' --verbose")
97
- FileUtils.rm('doc/deltacloud.rb')
31
+ out << "end"
32
+ out << "end"
33
+
34
+ ap out
35
+
36
+ #FileUtils.rm_r('doc') rescue nil
37
+ #FileUtils.mkdir_p('doc')
38
+ #File.open('doc/deltacloud.rb', 'w') do |f|
39
+ # f.puts(out.join("\n"))
40
+ #end
41
+ #system("yardoc -m markdown --readme README --title 'Deltacloud Client Library' 'lib/*.rb' 'doc/deltacloud.rb' --verbose")
42
+ #FileUtils.rm('doc/deltacloud.rb')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deltacloud-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Red Hat, Inc.
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-06-29 00:00:00 +02:00
12
+ date: 2010-07-07 00:00:00 +02:00
13
13
  default_executable: deltacloudc
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency