cfoundry 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -65,7 +65,8 @@ module CFoundry
65
65
  headers.merge!(options[:headers]) if options[:headers]
66
66
 
67
67
  if params
68
- path += "?" + encode_params(params)
68
+ uri = URI.parse(path)
69
+ path += (uri.query ? "&" : "?") + encode_params(params)
69
70
  end
70
71
 
71
72
  req = options.dup
@@ -74,17 +74,22 @@ module CFoundry::V2
74
74
  end
75
75
 
76
76
  define_method(plural) do |*args|
77
- depth, query = args
78
- depth ||= 1
77
+ get("v2", plural, nil => :json, :params => params_from(args))
78
+ end
79
+ end
79
80
 
80
- params = { :"inline-relations-depth" => depth }
81
81
 
82
- if query
83
- params[:q] = "#{query.keys.first}:#{query.values.first}"
84
- end
82
+ def params_from(args)
83
+ depth, query = args
84
+ depth ||= 1
85
+
86
+ params = { :"inline-relations-depth" => depth }
85
87
 
86
- get("v2", plural, nil => :json, :params => params)
88
+ if query
89
+ params[:q] = "#{query.keys.first}:#{query.values.first}"
87
90
  end
91
+
92
+ params
88
93
  end
89
94
 
90
95
  private
@@ -115,29 +115,23 @@ module CFoundry::V2
115
115
  end
116
116
 
117
117
 
118
- # TODO: allow direct filtering
119
- def app_by_name(name)
120
- current_space.apps.find do |a|
121
- a.name == name
122
- end
123
- end
124
-
125
-
126
118
  [ :app, :organization, :space, :user, :runtime, :framework,
127
119
  :service, :service_plan, :service_binding, :service_instance
128
120
  ].each do |singular|
129
- klass = singular.to_s.capitalize.gsub(/(.)_(.)/) do
121
+ plural = :"#{singular}s"
122
+
123
+ classname = singular.to_s.capitalize.gsub(/(.)_(.)/) do
130
124
  $1 + $2.upcase
131
125
  end
132
126
 
133
- plural = :"#{singular}s"
127
+ klass = CFoundry::V2.const_get(classname)
134
128
 
135
- has_space =
136
- CFoundry::V2.const_get(klass).method_defined? :space
129
+ has_space = klass.method_defined? :space
130
+ has_name = klass.method_defined? :name
137
131
 
138
132
  define_method(singular) do |*args|
139
133
  guid, _ = args
140
- CFoundry::V2.const_get(klass).new(guid, self)
134
+ klass.new(guid, self)
141
135
  end
142
136
 
143
137
  define_method(plural) do |*args|
@@ -154,48 +148,38 @@ module CFoundry::V2
154
148
  end
155
149
  end
156
150
 
157
- define_method(:"#{singular}_from") do |path, *args|
158
- depth, _ = args
159
- depth ||= 1
160
-
161
- uri = URI.parse(path)
162
-
163
- if uri.query
164
- uri.query += "&inline-relations-depth=#{depth}"
165
- else
166
- uri.query = "inline-relations-depth=#{depth}"
151
+ if has_name
152
+ define_method(:"#{singular}_by_name") do |name|
153
+ if has_space && current_space
154
+ current_space.send(plural, 1, :name => name).first
155
+ else
156
+ send(plural, 1, :name => name).first
157
+ end
167
158
  end
159
+ end
168
160
 
161
+ define_method(:"#{singular}_from") do |path, *args|
169
162
  send(
170
163
  :"make_#{singular}",
171
164
  @base.request_path(
172
165
  :get,
173
- uri.to_s,
174
- nil => :json))
166
+ path,
167
+ nil => :json,
168
+ :params => @base.params_from(args)))
175
169
  end
176
170
 
177
171
  define_method(:"#{plural}_from") do |path, *args|
178
- depth, _ = args
179
- depth ||= 1
180
-
181
- uri = URI.parse(path)
182
-
183
- if uri.query
184
- uri.query += "&inline-relations-depth=#{depth}"
185
- else
186
- uri.query = "inline-relations-depth=#{depth}"
187
- end
188
-
189
172
  @base.request_path(
190
173
  :get,
191
- uri.to_s,
192
- nil => :json)[:resources].collect do |json|
174
+ path,
175
+ nil => :json,
176
+ :params => @base.params_from(args))[:resources].collect do |json|
193
177
  send(:"make_#{singular}", json)
194
178
  end
195
179
  end
196
180
 
197
181
  define_method(:"make_#{singular}") do |json|
198
- CFoundry::V2.const_get(klass).new(
182
+ klass.new(
199
183
  json[:metadata][:guid],
200
184
  self,
201
185
  json)
@@ -53,16 +53,27 @@ module CFoundry::V2
53
53
  object = opts[:as] || singular
54
54
  plural_object = :"#{object}s"
55
55
 
56
- define_method(plural) {
57
- if manifest[:entity].key? plural
58
- manifest[:entity][plural].collect do |json|
56
+ define_method(plural) { |*args|
57
+ depth, query = args
58
+
59
+ if manifest[:entity].key?(plural)
60
+ objs = manifest[:entity][plural]
61
+
62
+ if query
63
+ find_by = query.keys.first
64
+ find_val = query.values.first
65
+ objs = objs.select { |o| o[:entity][find_by] == find_val }
66
+ end
67
+
68
+ objs.collect do |json|
59
69
  @client.send(:"make_#{object}", json)
60
70
  end
61
71
  else
62
72
  @client.send(
63
73
  :"#{plural_object}_from",
64
74
  send("#{plural}_url"),
65
- opts[:depth] || 1)
75
+ depth || opts[:depth],
76
+ query)
66
77
  end
67
78
  }
68
79
 
@@ -1,4 +1,4 @@
1
1
  module CFoundry # :nodoc:
2
2
  # CFoundry library version number.
3
- VERSION = "0.3.7"
3
+ VERSION = "0.3.8"
4
4
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfoundry
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 7
10
- version: 0.3.7
9
+ - 8
10
+ version: 0.3.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Suraci