Skip to content

Instantly share code, notes, and snippets.

@frangucc
Created January 20, 2011 21:15
Show Gist options
  • Save frangucc/788690 to your computer and use it in GitHub Desktop.
Save frangucc/788690 to your computer and use it in GitHub Desktop.
Provides Native Ruby Object based on JSON string.
#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