
Security News
Follow-up and Clarification on Recent Malicious Ruby Gems Campaign
A clarification on our recent research investigating 60 malicious Ruby gems.
"Representative" makes it easier to create XML or JSON representations of your Ruby objects.
It works best when you want the output to roughly follow the object structure, but still want complete control of the result.
Given a Ruby data-structure:
@books = [
Book.new(
:title => "Sailing for old dogs",
:authors => ["Jim Watson"],
:published => Publication.new(
:by => "Credulous Print",
:year => 1994
)
),
Book.new(
:title => "On the horizon",
:authors => ["Zoe Primpton", "Stan Ford"],
:published => Publication.new(
:by => "McGraw-Hill",
:year => 2005
)
),
Book.new(
:title => "The Little Blue Book of VHS Programming",
:authors => ["Henry Nelson"],
:rating => "****"
)
]
Representative::Nokogiri can be used to generate XML:
xml = Representative::Nokogiri.new do |r|
r.list_of :books, @books do
r.element :title
r.list_of :authors
r.element :published do
r.element :by
r.element :year
end
end
end
puts xml.to_s
which produces:
<books type="array">
<book>
<title>Sailing for old dogs</title>
<authors type="array">
<author>Jim Watson</author>
</authors>
<published>
<by>Credulous Print</by>
<year>1994</year>
</published>
</book>
<book>
<title>On the horizon</title>
<authors type="array">
<author>Zoe Primpton</author>
<author>Stan Ford</author>
</authors>
<published>
<by>McGraw-Hill</by>
<year>2005</year>
</published>
</book>
<book>
<title>The Little Blue Book of VHS Programming</title>
<authors type="array">
<author>Henry Nelson</author>
</authors>
<published/>
</book>
</books>
Notice that:
list_of
for a collection attribute generates an "array" element, which plays nicely
with most Ruby XML-to-hash converters.Representative::Json can be used to generate JSON, using exactly the same DSL:
json = Representative::Json.new do |r|
r.list_of :books, @books do
r.element :title
r.list_of :authors
r.element :published do
r.element :by
r.element :year
end
end
end
puts json.to_s
producing:
[
{
"title": "Sailing for old dogs",
"authors": [
"Jim Watson"
],
"published": {
"by": "Credulous Print",
"year": 1994
}
},
{
"title": "On the horizon",
"authors": [
"Zoe Primpton",
"Stan Ford"
],
"published": {
"by": "McGraw-Hill",
"year": 2005
}
},
{
"title": "The Little Blue Book of VHS Programming",
"authors": [
"Henry Nelson"
],
"published": null
}
]
Representative is packaged as a Gem. Install with:
gem install representative
A separate gem, RepresentativeView, integrates Representative as an ActionPack template format.
Representative includes integration with Tilt, which can be enabled with:
require "representative/tilt_integration"
This registers handlers for ".xml.rep
" and ".json.rep
" templates.
Copyright (c) 2009-2018 Mike Williams. See LICENSE for details.
If Representative is not your cup of tea, you may prefer:
Just don't go back to using "this_thing.to_xml
" and "that_thing.to_json
", m'kay?
FAQs
Unknown package
We found that representative demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.
Research
/Security News
A malicious Go module posing as an SSH brute forcer exfiltrates stolen credentials to a Telegram bot controlled by a Russian-speaking threat actor.