Skip to content

Instantly share code, notes, and snippets.

@basicavisual
Last active November 13, 2017 05:52
Show Gist options
  • Save basicavisual/8115dc5c9e2643de48bdc0b2a7213c19 to your computer and use it in GitHub Desktop.
Save basicavisual/8115dc5c9e2643de48bdc0b2a7213c19 to your computer and use it in GitHub Desktop.
[
{
"id": "851f799f-0852-439e-b9b2-df92c43e7672",
"rating": 1,
"name": "Barajas, Bahena and Kano",
"contact": {
"site": "https://federico.com",
"email": "[email protected]",
"phone": "534 814 204"
},
"address": {
"street": "82247 Mariano Entrada",
"city": "Mérida Alfredotown",
"state": "Durango",
"location": {
"lat": 19.440057053713137,
"lng": -99.12704709742486
}
}
},
{
"id": "edb50561-46f9-4541-9c04-8de82401cc13",
"rating": 4,
"name": "Pedraza - Niño",
"contact": {
"site": "http://josé eduardo.gob.mx",
"email": "[email protected]",
"phone": "537 531 201"
},
"address": {
"street": "207 Chavarría Lado",
"city": "Franciscohaven",
"state": "Baja California Norte",
"location": {
"lat": 19.442683820522397,
"lng": -99.12502459288842
}
}
}
]
# this is the parsed json after a.read
=> [{"id"=>"851f799f-0852-439e-b9b2-df92c43e7672", "rating"=>1, "name"=>"Barajas, Bahena and Kano", "contact"=>{"site"=>"https://federico.com", "email"=>"[email protected]", "phone"=>"534 814 204"}, "address"=>{"street"=>"82247 Mariano Entrada", "city"=>"Mérida Alfredotown", "state"=>"Durango", "location"=>{"lat"=>19.440057053713137, "lng"=>-99.12704709742486}}}, {"id"=>"4e17896d-a26f-44ae-a8a4-5fbd5cde79b0", "rating"=>0, "name"=>"Hernández - Lira", "contact"=>{"site"=>"http://graciela.com.mx", "email"=>"[email protected]", "phone"=>"570 746 998"}, "address"=>{"street"=>"93725 Erick Arroyo", "city"=>"Mateofurt", "state"=>"Hidalgo", "location"=>{"lat"=>19.437904276994995, "lng"=>-99.12865767750226}}}]
class ApiRead
require 'open-uri'
require 'json'
def read
# this goes fine
data = JSON.parse(open("http://example.com/example.json").read)["data"]
end
# This works:
# a = ApiRead.new
# a = a.read
# a[0]['name']
def seed
# This doesn't. I call this like a.seed and get "undefined method `seed' for #<Array:0x00562fb82042a0>"
self.each do |record|
puts record['id']
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment