chemook78
chemook78

Reputation: 1198

Save JSON from API endpoint to database in Ruby on Rails app

I am new to Ruby on Rails and need some advice on this! I trying to build a webscraper and have a JSON file here at Kimono Labs, where you can turn website data into API's:

https://www.kimonolabs.com/api/3obhv4p0?apikey=WWHHbKEkOmAPXsObOccPBXTb5NgRyCNO that I want to save to the database of my Ruby on Rails application.

Specifically it's the "results" key that I want to save to the database. It contains data from Google Finance with the company, URL to quote page, P/E and latest price. Which has the following format:

"results": {
    "collection1": [
      {
        "property1": {
          "href": "https://www.google.com/finance?catid=TRBC:57&sort=a&ei=Tx2WVonTG9uhe7Hpv_AN",
          "text": "Company"
        },
        "property2": "P/E (ttm)",
        "property3": "Quote",
        "index": 1,
        "url": "https://www.google.com/finance?catid=TRBC%3A57&sort=PE_RATIO&ei=6tyMVrqxIdaP0ASF0pbACQ"
      },
      {
        "property1": {
          "href": "https://www.google.com/finance?q=NASDAQ:NANO&ei=Tx2WVonTG9uhe7Hpv_AN",
          "text": "Nanometrics Incorporated"
        },
        "property2": "10,100.72",
        "property3": "14.04",
        "index": 2,
        "url": "https://www.google.com/finance?catid=TRBC%3A57&sort=PE_RATIO&ei=6tyMVrqxIdaP0ASF0pbACQ"
      },

This is the migration I have:

ActiveRecord::Schema.define(version: 20160108073353) do

  create_table "stocks", force: :cascade do |t|
    t.string   "company"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "url"
    t.float    "pe"
    t.float    "quote"
  end

The question is how do I load this JSON file into my application? I think it has the following steps and therefore my question can be broken up into:

Thanks for the help!

Upvotes: 2

Views: 2809

Answers (1)

Inpego
Inpego

Reputation: 2667

Yes, these are right steps.

First, you should create Stock model:

$ rails g model stock --skip

And then:

# get JSON
result = Net::HTTP.get(URI.parse('https://www.kimonolabs.com/api/3obhv4p0?apikey=WWHHbKEkOmAPXsObOccPBXTb5NgRyCNO'))

# parse JSON
json = JSON.parse(result)

# save data to DB
json['results']['collection1'][1..-1].each do |data| # [1..-1] ignores first dummy element
  Stock.create(
    company: data['property1']['text'],
    url: data['url'],
    pe: data['property2'].gsub(',', ''), # .gsub removes thousands separator
    quote: data['property3'].gsub(',', '')
  )
end

Upvotes: 7

Related Questions