Skip to content

Instantly share code, notes, and snippets.

@denyago
Last active December 24, 2015 07:09
Show Gist options
  • Select an option

  • Save denyago/6761317 to your computer and use it in GitHub Desktop.

Select an option

Save denyago/6761317 to your computer and use it in GitHub Desktop.
require 'erb'
require 'net/http'
require 'json'
template = %q{
This is one: <%= data_obj.one %>
The foo is <%= data_obj.foo %>
}
class MyDataObj
def initialize(url)
@url = URI(url)
end
def fetch
contents = Net::HTTP.get(@uri)
@json = JSON.parse(contents)
self
end
def foo
@json['foo'] rescue ''
end
def one
@json['one'] rescue ''
end
end
data_obj = MyDataObj.new('http://example.com/api.json').fetch
puts ERB.new(template).result binding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment