?
Solved

data download

Posted on 2011-02-24
4
Medium Priority
?
365 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 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

864 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