Last active
December 24, 2015 07:09
-
-
Save denyago/6761317 to your computer and use it in GitHub Desktop.
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
| 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