abiquo-api 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 23b4167bbf873afc7d296d61698a49b030db6f95
4
- data.tar.gz: 862682f6f160d1289679db619cebd07a4fdd65f8
3
+ metadata.gz: fcac0cebe8eca1e8436a1e201a4fbc5ec3d7e30c
4
+ data.tar.gz: d7d7b8036b8746c68e72c8d8d83e5e02a119845f
5
5
  SHA512:
6
- metadata.gz: f1b726b40ba781cef65752029080f30fbdb1df17cedf8c1d6a11809d61d5bec04d312523eabe00fe09eb202d8195e72c3f43089614ee2ab4651e95563b16a279
7
- data.tar.gz: ba8daf8ea98f990cffdab92ac583c525d5719e5a7efdc4fc50907d15b8794553beed35efcc72781dd62ab677aa8394b6f932b249a1f24c82d94e60d8e6718ea9
6
+ metadata.gz: 772ad5ac86fb5cef5a94a6fb4d623da66dc83c5cf4b76e5e8fbbe2db2af1837ceabd83e917250ab8cc0b56f2d06a4bd8e5076d16fe1d367037b62d2256b06521
7
+ data.tar.gz: 8ec539f19258de9cf0bf4f9be60050956b27265680e46ba228a5793704547cfaaa0de7609d6c2abfd5c63381712216dc51a836b6027d40f5100a6d8446e874d7
data/README.md CHANGED
@@ -27,7 +27,7 @@ Or, if you want to force a specific API version:
27
27
  ```ruby
28
28
  require 'abiquo-api'
29
29
 
30
- a = AbiquoAPI.new(:abiquo_api_url => 'https://10.60.13.40/api',
30
+ abq = AbiquoAPI.new(:abiquo_api_url => 'https://10.60.13.40/api',
31
31
  :abiquo_username => "admin",
32
32
  :abiquo_password => "xabiquo",
33
33
  :version => "2.9")
@@ -37,9 +37,10 @@ Then you can start browsing the API:
37
37
 
38
38
  ```ruby
39
39
  l = AbiquoAPI::Link.new(:href => '/api/cloud/virtualdatacenters',
40
- :type => 'application/vnd.abiquo.virtualdatacenters+json')
40
+ :type => 'application/vnd.abiquo.virtualdatacenters+json',
41
+ :client => abq)
41
42
 
42
- a.get(l)
43
+ l.get
43
44
  ```
44
45
 
45
46
  ## Client object
@@ -62,6 +63,10 @@ vapp = vdc.link(:virtualappliances).get.first
62
63
 
63
64
  This is used to map Abiquo API objects.
64
65
 
66
+ ## Generic list
67
+
68
+ This is used to iterate over paginated lists.
69
+
65
70
  ## Examples
66
71
 
67
72
  ### Browse the API
@@ -1,7 +1,8 @@
1
- require 'abiquo-api/errors.rb'
2
- require 'abiquo-api/httpclient.rb'
3
- require 'abiquo-api/link.rb'
4
- require 'abiquo-api/model.rb'
1
+ require 'abiquo-api/collection'
2
+ require 'abiquo-api/errors'
3
+ require 'abiquo-api/httpclient'
4
+ require 'abiquo-api/link'
5
+ require 'abiquo-api/model'
5
6
 
6
7
  ##
7
8
  # Ruby Abiquo API client main class
@@ -27,11 +28,6 @@ class AbiquoAPI
27
28
  #
28
29
  attr_accessor :user
29
30
 
30
- ##
31
- # The config properties for the UI.
32
- #
33
- attr_accessor :properties
34
-
35
31
  ##
36
32
  # The Abiquo API version used by this client.
37
33
  #
@@ -69,13 +65,6 @@ class AbiquoAPI
69
65
  @enterprise = AbiquoAPIClient::Link.new(loginresp['links'].select {|l| l['rel'] == 'enterprise'}.first)
70
66
  @user = AbiquoAPIClient::LinkModel.new(loginresp.merge({:client => self}))
71
67
 
72
- @properties = @http_client.request(
73
- :expects => [200],
74
- :method => 'GET',
75
- :path => "#{api_path}/config/properties",
76
- :accept => 'application/vnd.abiquo.systemproperties+json'
77
- )
78
-
79
68
  if options.has_key? :version
80
69
  @version = options[:version][0..2]
81
70
  else
@@ -89,6 +78,18 @@ class AbiquoAPI
89
78
 
90
79
  self
91
80
  end
81
+
82
+ ##
83
+ # Loads System properties
84
+ #
85
+ def properties
86
+ @http_client.request(
87
+ :expects => [200],
88
+ :method => 'GET',
89
+ :path => "#{api_path}/config/properties",
90
+ :accept => 'application/vnd.abiquo.systemproperties+json'
91
+ )
92
+ end
92
93
 
93
94
  ##
94
95
  # Returns a new instance of the {AbiquoAPIClient::LinkModel} class.
@@ -100,6 +101,18 @@ class AbiquoAPI
100
101
  AbiquoAPIClient::LinkModel.new(hash.merge({ :client => self}))
101
102
  end
102
103
 
104
+ ##
105
+ # Returns a new instance of the {AbiquoAPIClient::LinkCollection}
106
+ # class.
107
+ #
108
+ # Parameters:
109
+ # An instance of {AbiquoAPIClient::Link} pointing to the URL of
110
+ # the collection.
111
+ #
112
+ def list(link, options = {})
113
+ AbiquoAPI::LinkCollection.new(self.get(link, options), link.type, self)
114
+ end
115
+
103
116
  ##
104
117
  # Executes an HTTP GET over the {AbiquoAPIClient::Link} passed as parameter.
105
118
  #
@@ -131,15 +144,10 @@ class AbiquoAPI
131
144
 
132
145
  resp = @http_client.request(req_hash)
133
146
 
134
- if resp.is_a? Array
135
- tmp_a = []
136
- resp.each do |r|
137
- tmp_r = AbiquoAPIClient::LinkModel.new(r.merge({:client => self}))
138
- tmp_a << tmp_r
139
- end
140
- tmp_a
141
- else
147
+ if resp['collection'].nil?
142
148
  AbiquoAPIClient::LinkModel.new(resp.merge({ :client => self}))
149
+ else
150
+ resp
143
151
  end
144
152
  end
145
153
 
@@ -0,0 +1,173 @@
1
+ require 'formatador'
2
+
3
+ module AbiquoAPIClient
4
+ ##
5
+ # Represents a collection of resources in the Abiquo API.
6
+ #
7
+ class LinkCollection
8
+ def initialize(parsed_response, type, client)
9
+ @size = parsed_response['totalSize'].nil? ? parsed_response['collection'].count : parsed_response['totalSize']
10
+ if type.include? ";"
11
+ @type = type.split(';').first
12
+ else
13
+ @type = type
14
+ end
15
+
16
+ unless parsed_response['links'].empty?
17
+ coluri = URI.parse(parsed_response['links'].first['href'])
18
+ @path = coluri.path
19
+
20
+ opts = coluri.query
21
+ unless opts.nil?
22
+ opt_hash = opts.split("&").map{|e| { e.split("=").first.to_sym => e.split("=").last }}.reduce({}) {|h,pairs| pairs.each {|k,v| h[k] ||= v}; h}
23
+ @page_size = opt_hash[:limit].to_i
24
+
25
+ st = opt_hash[:startwith].nil? ? 0 : opt_hash[:startwith].to_i
26
+ @current_page = (st / @page_size) + 1
27
+ end
28
+
29
+ @links = parsed_response['links']
30
+ end
31
+
32
+ @collection = parsed_response['collection'].map {|r| client.new_object(r)}
33
+
34
+ @client = client
35
+ end
36
+
37
+ ##
38
+ # Returns the total size of the collection
39
+ #
40
+ def size
41
+ @size
42
+ end
43
+ alias count size
44
+
45
+ ##
46
+ # Returns the first element in the collection
47
+ #
48
+ def first(count = nil)
49
+ if count.nil?
50
+ @collection.first
51
+ else
52
+ out = []
53
+ @collection.first(count).each do |item|
54
+ out << item
55
+ end
56
+ out
57
+ end
58
+ end
59
+
60
+ ##
61
+ # Returns the last element in the collection
62
+ #
63
+ def last
64
+ out = nil
65
+
66
+ each {|i| out = i }
67
+
68
+ out
69
+ end
70
+
71
+ ##
72
+ # Returns an array representing the collection
73
+ #
74
+ def to_a
75
+ out = []
76
+
77
+ each { |e| out << e }
78
+
79
+ out
80
+ end
81
+
82
+ ##
83
+ # Selects elements of the collections for which
84
+ # the supplied block evaluates to true
85
+ #
86
+ def select
87
+ out = []
88
+
89
+ each { |e| out << e if yield(e) }
90
+
91
+ out
92
+ end
93
+
94
+ ##
95
+ # Returns an array resulting of applying the provided
96
+ # block to all of the elements of the collection
97
+ #
98
+ def map
99
+ out = []
100
+
101
+ each { |e| out << yield(e) }
102
+
103
+ out
104
+ end
105
+ alias collect map
106
+
107
+ ##
108
+ # Iterates the collection
109
+ #
110
+ def each
111
+ if block_given?
112
+ unless @current_page == 1 or @current_page.nil?
113
+ next_page = retrieve('first')
114
+ @collection = next_page.nil? ? [] : next_page
115
+ end
116
+
117
+ loop do
118
+ @collection.each do |item|
119
+ yield item
120
+ end
121
+
122
+ break if @links.nil? or @links.select {|l| l['rel'].eql? "next" }.first.nil?
123
+
124
+ next_page = retrieve('next')
125
+ @collection = next_page.nil? ? [] : next_page
126
+ end
127
+ else
128
+ self.to_enum
129
+ end
130
+ end
131
+
132
+ ##
133
+ # Pretty print the object.
134
+ #
135
+ def inspect
136
+ Thread.current[:formatador] ||= Formatador.new
137
+ data = "#{Thread.current[:formatador].indentation}<#{self.class.name}"
138
+ Thread.current[:formatador].indent do
139
+ unless self.instance_variables.empty?
140
+ vars = self.instance_variables.clone
141
+ vars.delete(:@client)
142
+ vars.delete(:@page)
143
+ data << " "
144
+ data << vars.map { |v| "#{v}=#{instance_variable_get(v.to_s).inspect}" }.join(", ")
145
+ end
146
+ end
147
+ data << " >"
148
+ data
149
+ end
150
+
151
+ private
152
+
153
+ def retrieve(rel)
154
+ return nil if @links.nil?
155
+ f = @links.select {|l| l['rel'].eql? rel }.first
156
+ return nil if f.nil?
157
+
158
+ q = URI.parse(f['href']).query.split('&').map {|it| it.split('=') }
159
+ opts = Hash[q.map{ |k, v| [k.to_sym, v] }]
160
+
161
+ l = AbiquoAPIClient::Link.new(:href => f['href'],
162
+ :type => @type)
163
+ resp = @client.get(l, opts)
164
+
165
+ st = opts[:startwith].nil? ? 0 : opts[:startwith].to_i
166
+ @current_page = (st / @page_size) + 1
167
+
168
+ @links = resp['links']
169
+
170
+ resp['collection'].map {|e| @client.new_object(e) }
171
+ end
172
+ end
173
+ end
@@ -76,33 +76,6 @@ module AbiquoAPIClient
76
76
  rescue
77
77
  response = response.body
78
78
  end
79
-
80
- # Handle pagination
81
- if not response['links'].nil? and response['links'].select {|l| l['rel'].eql? "next" }.count > 0
82
- items = []
83
- items = items + response['collection'] if not response['collection'].nil?
84
-
85
- loop do
86
- next_url = response['links'].select {|l| l['rel'].eql? "next" }.first['href']
87
- uri = URI.parse(next_url)
88
- params[:path] = uri.path
89
- params[:query] = uri.query
90
- params[:headers] = headers
91
- response = issue_request(params)
92
- response = JSON.parse(response.body) unless response.body.empty?
93
- items = items + response['collection'] if not response['collection'].nil?
94
-
95
- break if response['links'].select {|l| l['rel'].eql? "next" }.count == 0
96
- end
97
-
98
- items
99
- else
100
- if not response['collection'].nil?
101
- response['collection']
102
- else
103
- response.nil? ? nil : response
104
- end
105
- end
106
79
  end
107
80
 
108
81
  private
@@ -45,13 +45,19 @@ module AbiquoAPIClient
45
45
 
46
46
  ##
47
47
  # If the :client attribute is not nil, will retrieve
48
- # the resource that this link represents, or nil otherwise
48
+ # the resource or collection that this link represents,
49
+ # or nil otherwise
49
50
  #
50
51
  def get(options = {})
51
52
  if @client.nil?
52
53
  return nil
53
54
  else
54
- @client.get(self, options)
55
+ r = @client.get(self, options)
56
+ if r.is_a? Hash
57
+ AbiquoAPI::LinkCollection.new(r, self.type, @client)
58
+ else
59
+ r
60
+ end
55
61
  end
56
62
  end
57
63
 
@@ -101,17 +101,32 @@ module AbiquoAPIClient
101
101
  end
102
102
 
103
103
  ##
104
- # Retrieves the link that has the 'rel' attribute specified
105
- # as parameter.
104
+ # Returns an array of {AbiquoAPI::Link} for the resource
105
+ #
106
+ def links
107
+ self.links.map {|l| l.values }.flatten
108
+ end
109
+
110
+ ##
111
+ # Retrieves the link or links that hve the 'rel' attribute
112
+ # specified as parameter.
106
113
  #
107
114
  # Parameters:
108
115
  # [link_rel] The 'rel' value to look for, symbolized.
109
116
  #
110
- # Returns the first link found with the 'rel' attribute
117
+ # Returns the link the 'rel' attribute
111
118
  # specified or nil if not found.
112
119
  #
113
120
  def link(link_rel)
114
- self.links.select {|l| l[link_rel] }.first[link_rel]
121
+ ls = self.links.select {|l| l[link_rel] }.map { |t| t.values }.flatten
122
+ case ls.count
123
+ when 1
124
+ ls.first
125
+ when 0
126
+ nil
127
+ else
128
+ ls
129
+ end
115
130
  end
116
131
 
117
132
  ##
@@ -1,3 +1,3 @@
1
1
  module AbiquoAPIClient
2
- VERSION = '0.0.3'
3
- end
2
+ VERSION = '0.0.4'
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abiquo-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc Cirauqui
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-15 00:00:00.000000000 Z
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon
@@ -82,6 +82,7 @@ files:
82
82
  - README.md
83
83
  - abiquo-api.gemspec
84
84
  - lib/abiquo-api.rb
85
+ - lib/abiquo-api/collection.rb
85
86
  - lib/abiquo-api/errors.rb
86
87
  - lib/abiquo-api/httpclient.rb
87
88
  - lib/abiquo-api/link.rb
@@ -111,3 +112,4 @@ signing_key:
111
112
  specification_version: 4
112
113
  summary: Simple Abiquo API client
113
114
  test_files: []
115
+ has_rdoc: