LVS-JSONService 0.2.6 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
data/.specification CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: LVS-JSONService
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.8
5
5
  require_paths:
6
6
  - lib
7
7
  platform: ruby
data/JSONService.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{JSONService}
5
- s.version = "0.2.6"
5
+ s.version = "0.2.8"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["LVS"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.6
1
+ 0.2.8
@@ -142,55 +142,47 @@ module LVS
142
142
  end
143
143
 
144
144
  def initialize(values = {})
145
- values.each_pair do |key, value|
146
- key = key.underscore
147
- new_instance_methods = "
148
- def #{key}
149
- @#{key}
150
- end
151
- def #{key}=(value)
152
- @#{key} = value
153
- end
154
- "
155
-
156
- # If the key starts with has_ create alias to has_ method removing has
157
- # and putting ? at the end
158
- if key =~ /^has_/
159
- temp_key = "#{key.gsub(/^has_/, '')}?"
160
- new_instance_methods << "
161
- def #{temp_key}
162
- @#{key}
163
- end
164
- "
165
- end
166
-
167
- self.instance_eval(new_instance_methods)
168
-
169
- if value.is_a?(Hash)
170
- self.instance_variable_set("@#{key}", self.class.new(value))
171
- elsif value.is_a?(Array)
172
- self.instance_variable_set("@#{key}", value.collect {|v| if v.is_a?(Hash) or v.is_a?(Array) then self.class.new(v) else v end })
173
- else
174
- if key =~ /date$/
175
- value = Time.at(value/1000)
176
- elsif key =~ /^has_/
177
- self.instance_variable_set("@#{key}", value)
178
- !(value == 0 || value.blank?)
179
- elsif key =~ /\?$/
180
- key = "has_#{key.chop}"
181
- !(value == 0 || value.blank?)
182
- end
183
-
184
- self.instance_variable_set("@#{key}", value)
145
+ @data = values
146
+ end
147
+
148
+ def id
149
+ @data["id"]
150
+ end
151
+
152
+ def name_to_key(name)
153
+ key = name.gsub(/[=?]/, '')
154
+ key.camelize(:lower)
155
+ end
156
+
157
+ def method_missing(name, *args)
158
+ name = name.to_s
159
+ key = name_to_key(name)
160
+ value = @data[key]
161
+ if name =~ /=$/
162
+ @data[key] = args[0]
163
+ elsif name =~ /\?$/
164
+ value = @data[name_to_key("has_#{key}")]
165
+ !(value == 0 || value.blank?)
166
+ elsif name =~ /^has_/
167
+ !(value == 0 || value.blank?)
168
+ else
169
+ if (value.is_a?(Hash))
170
+ value = self.class.new(value)
171
+ elsif (value.is_a?(Array))
172
+ value = value.collect {|v| if v.is_a?(Hash) or v.is_a?(Array) then self.class.new(v) else v end }
173
+ elsif name =~ /date$/
174
+ value = Time.at(value/1000)
185
175
  end
186
176
  end
187
- end
188
-
189
- def method_missing(*args)
190
- unless self.class.ignore_missing
191
- self.class.debug("Method #{args[0]} called on #{self.class} but is non-existant, returned default FALSE")
192
- super
177
+ if value.nil?
178
+ if self.class.ignore_missing
179
+ self.class.debug("Method #{name} called on #{self.class} but is non-existant, returned nil")
180
+ return nil
181
+ else
182
+ raise NoMethodError.new("Method #{name} called on #{self.class} but is non-existant, returned nil")
183
+ end
193
184
  end
185
+ value
194
186
  end
195
187
  end
196
188
 
@@ -97,7 +97,7 @@ module LVS
97
97
  if response.body.size < 1024 || options[:debug]
98
98
  LVS::JsonService::Logger.debug "Response (#{timing}): #{response.body.gsub(/\n/, '')}"
99
99
  else
100
- LVS::JsonService::Logger.debug "Response Snippet (#{timing}): #{response.body.gsub(/\n/, '')[0..1024]}"
100
+ LVS::JsonService::Logger.debug "Response Snippet (#{timing} to return #{"%.1f" % (response.body.size/1024)}kB): #{response.body.gsub(/\n/, '')[0..1024]}"
101
101
  end
102
102
  if result.is_a?(Hash) && result.has_key?("PCode")
103
103
  raise LVS::JsonService::Error.new(result["message"], result["PCode"], service, args, result)
@@ -18,4 +18,15 @@ describe LVS::JsonService::Base do
18
18
  obj = RaiseOnMissingExample.new
19
19
  lambda {obj.do_voodoo}.should raise_error(NoMethodError)
20
20
  end
21
+
22
+ it "should find camelCase attributes using camelCase or ruby_sytax" do
23
+ class AttributeNames < LVS::JsonService::Base
24
+ self.ignore_missing = true
25
+ fake_service :call, '{"longMethodName":1}'
26
+ end
27
+ obj = AttributeNames.call
28
+ obj.longMethodName.should eql(1)
29
+ obj.long_method_name.should eql(1)
30
+ end
31
+
21
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: LVS-JSONService
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - LVS