Created
January 20, 2011 21:15
-
-
Save frangucc/788690 to your computer and use it in GitHub Desktop.
Provides Native Ruby Object based on JSON string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#JSON object wrapper providing a native Ruby datatype | |
class UserResultSet | |
attr_reader :uri, :devices :email | |
def initialize(attributes) | |
@uri = attributes[:uri] | |
@devices = attributes[:email] | |
@email = attributes[:first_name] | |
end | |
def self.from_json(json_string) | |
parsed = JSON.parse(json_string) | |
devices = parsed["results"].map do |item| | |
device.new(item) | |
end | |
new( | |
:devices => devices, | |
:email => parsed["email"], | |
:uri => parsed["uri"]) | |
end | |
class Device | |
attr_reader :platform_id | |
def initialize(attributes) | |
@platform_id = attributes["platform_id"] | |
end | |
end | |
results = UserResultSet.from_json(input) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment