Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ensure reserved attributes aren't serialized and sent back to server

  • Loading branch information...
commit b25d2b8b42850039a93ff5545a90652c4a27a771 1 parent fd39fe7
@theganyo authored
View
6 lib/usergrid/core/entity.rb
@@ -40,6 +40,12 @@ def to_s
"resource: #{url}\ndata: #{data}"
end
+ def to_json(*args)
+ data.except(RESERVED).to_json *args
+ end
+ alias :encode :to_json
+ alias :dump :to_json
+
private
def method_missing(method, *args, &block)
View
7 lib/usergrid/core/resource.rb
@@ -3,8 +3,9 @@
module Usergrid
class Resource < RestClient::Resource
+ RESERVED = %w( created modified metadata uri )
DEFAULT_API_URL = 'https://api.usergrid.com'
- TYPE_HEADERS = { :content_type => :json, :accept => :json }
+ TYPE_HEADERS = { content_type: :json, accept: :json }
attr_reader :current_user, :api_url
@@ -92,12 +93,12 @@ def get(additional_headers={}, &block)
end
def post(payload, additional_headers={}, &block)
- payload = MultiJson.dump(payload) if payload.is_a?(Hash) || payload.is_a?(Array)
+ payload = MultiJson.dump(payload) unless payload.is_a? String
self.response = super payload, additional_headers, &block
end
def put(payload, additional_headers={}, &block)
- payload = MultiJson.dump(payload) if payload.is_a?(Hash) || payload.is_a?(Array)
+ payload = MultiJson.dump(payload) unless payload.is_a? String
self.response = super payload, additional_headers, &block
end
View
21 lib/usergrid/extensions/hash.rb
@@ -1,10 +1,5 @@
class Hash
- # recursive
- def add_dot_notation!
- add_dot_notation_recurse! self
- end
-
# not recursive
def symbolize_keys
inject({}) do |options, (key, value)|
@@ -13,15 +8,23 @@ def symbolize_keys
end
end
- private
+ def except(*keys)
+ dup.except! *keys
+ end
- def add_dot_notation_recurse!(_hash)
+ def except!(*keys)
+ keys.each { |key| key.is_a?(Array) ? except!(*key) : delete(key) }
+ self
+ end
+
+ # recursive
+ def add_dot_notation!(_hash=self)
_hash.each do |k,v|
getter = k.to_sym; setter = "#{k}=".to_sym
_hash.define_singleton_method getter, lambda { _hash[k] } unless _hash.respond_to? getter
_hash.define_singleton_method setter, lambda { |v| _hash[k] = v } unless _hash.respond_to? setter
- add_dot_notation_recurse!(v) if v.is_a? Hash
- v.each { |e| add_dot_notation_recurse!(e) if e.is_a? Hash } if v.is_a? Array
+ add_dot_notation!(v) if v.is_a? Hash
+ v.each { |e| add_dot_notation!(e) if e.is_a? Hash } if v.is_a? Array
end
end
end
View
6 spec/usergrid/core/entity_spec.rb
@@ -46,4 +46,10 @@
@application['foobars'].get.entity.should be_nil
end
+ it "should not serialize reserved attributes" do
+ dump = MultiJson.dump @user
+ hash = MultiJson.load dump
+ Usergrid::Resource::RESERVED.each { |a| hash.should_not have_key(a) }
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.