Solved

data download

Posted on 2011-02-24
4
344 Views
Last Modified: 2012-05-11
Want to be able to download xml data from a url and present the data in an array or hash.
Is this poss and how - any examples would be great.

Regards
0
Comment
Question by:philsivyer
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
JESii earned 500 total points
ID: 34973219
Here's some sample code that may help you out. One shows how to access a website using Ruby and the other demonstrates how to process data into arrays.  that should get you started a bit.

I've posted some answers to this type of question before, but can't seem to find them in my search results...


### Get information from the web example
require 'open-uri'
require 'csv'

#Get info about specific stocks
def get_info stock_symbol
  puts "#{stock_symbol} Current Ticker Information"
  url = "http://download.finance.yahoo.com/d/quotes.csv?s=#{stock_symbol}&f=sl1d1t1c1ohgv&e=.csv"
  puts "Connecting to #{url}\n\n\n"

  csv = CSV.parse(open(url).read)

sink=File.open("c:/shares.txt","a")
  #parse csv data
  csv.each do |row|
    puts "--------------------------------------------------"
    sink.puts "--------------------------------------------------"
   
    puts "Information current as of #{row[3]} on #{row[2]}\n\n"
    sink.puts "Information current as of #{row[3]} on #{row[2]}\n\n"
   
    puts "#{row[0]}'s last trade was - $#{row[1]}  (increase of #{row[4]})\n\n"
    sink.puts "#{row[0]}'s last trade was - $#{row[1]}  (increase of #{row[4]})\n\n"
   
    puts "\tOpened at $#{row[5]}"
    sink.puts "\tOpened at $#{row[5]}"
   
    puts "\tRange for the day $#{row[7]} - $#{row[6]}"
    sink.puts "\tRange for the day $#{row[7]} - $#{row[6]}"
   
   
  end
  puts "--------------------------------------------------"
  sink.puts "--------------------------------------------------"
  sink.close
end


#print "Enter stock symbol (separate by space if > 1): "
stock_symbols = %w{ibm ctxs vti rbs}

#Get stock information
stock_symbols.each do |symbol|
  get_info(symbol.upcase)
end

### Sample code to get data into array
#file1

require 'fastercsv'

class Bank
attr_accessor :name, :funds, :loans, :admin

def initialize(nm, fnds, lns, ad)
@name = nm 
@funds = fnds 
@loans = lns 
@admin = ad  
 end

#there would be several methods that I took out here as not relevant for this question

end





class Readbank
def initialize()
@bank = Array.new
end
def get_bank
 @bank
end
def read_in_banks(file_name)
 CSV.foreach(file_name, ?\n) do |row|
	 p row
@bank << Bank.new(row["name"].to_i,row["funds"].to_i,row["loan"].to_i,row["admin"])
p @bank
end
 end
 @bank
end


#file2

require "bank"
#require 'readbank'

#CREATES A READBANK INSTANCE TO BE USED TO READ IN BANK.CSV FILE
rbank1 = Readbank.new
csv_file_name = ARGV[0]
rbank1.read_in_banks(csv_file_name)

#PARSES BANK.CSV FILE INTO 5 BANK INSTANCES 
bankA = Bank.new(rbank1.get_bank[0])
bankB = Bank.new(rbank1.get_bank[1])
bankC = Bank.new(rbank1.get_bank[2])
bankD = Bank.new(rbank1.get_bank[3])
bankE = Bank.new(rbank1.get_bank[4])

Open in new window

0
 

Author Comment

by:philsivyer
ID: 34978455
Thanks
0
 

Author Closing Comment

by:philsivyer
ID: 34978460
thanks
0
 
LVL 12

Expert Comment

by:JESii
ID: 34979012
You're welcome; glad that helped. And thank you for the points!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now