Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

data download

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

618 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