?
Solved

data download

Posted on 2011-02-24
4
Medium Priority
?
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
JESii earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

764 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