Solved

data download

Posted on 2011-02-24
4
353 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Attempting to install LESS on Windows 7 with Ruby Installer 11 754
Ruby on Rails 4 461
How to get POST to show a page on ruby and rails 5 339
Merge XML files using a unique key? 8 143
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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