Reputation: 708
I am using a site's API to retrieve the following information:
5097,2593,1289766548 1541,99,65990390 934,99,73230517 3055,99,62377700
1410,99,81193882 1232,99,50959566 340,99,31225295 2303,99,46585590
91,99,200000000 2258,99,32250727 692,99,40608716 4397,99,23545788
2371,99,30486082 408,99,33064494 136,99,54937860 2410,99,23192056
858,99,30378477 1088,99,21174680 174,99,76296917 477,99,50883493
2577,99,24367856 603,99,34401457 1556,99,24433483 1115,99,22150489
1365,99,23373048 4889,120,121954995 10957,97,10702990 -1,-1 -1,-1
9460,3985121 -1,-1 -1,-1 25908,1608 17180,1538 19932,1519 33486,1651
-1,-1 2589,401 14280,1262 -1,-1 -1,-1 -1,-1 34212,360000 -1,-1 -1,-1 -1,-1
Note that the numbers are in sets of 3, separated by commas, then a space, then to the next set.
xxxx,xxxx,xxxxxxxxxx yyyy,yy,yyyyyyyy zzzz,zz,zzzzzzzz
Here is my controller:
class PagesController < ApplicationController
def home
require 'open-uri'
@username = "brink"
@url = "http://hiscore.runescape.com/index_lite.ws?player=#{@username}"
@doc = Nokogiri::HTML(open(@url))
end
def help
end
end
Here is how I am putting out the information in the view:
<h1>Welcome to xpTrack</h1>
<p>
<%= @username.capitalize %>
</p>
<%= @doc.text %>
Is there a way for me to create an array that splits at each space between each set of numbers? I have tried:
@stats = @doc.split('\n')
, but that returns:
undefined method `split' (some Nokogiri error)
Upvotes: 1
Views: 689
Reputation: 5343
Don't use Nokogiri unless you expect XML or HTML. Just use open().read
to get a string, and split it up.
Also, I suspect .scan(/(\d+),(\d+),(\d+)/)
, or some variation thereof, will work better than split.
Upvotes: 3
Reputation: 10754
You can try:
# split on spaces
@stats = @doc.text.split(' ')
# => ["xxxx,xxxx,xxxxxxxxxx","yyyy,yy,yyyyyyyy","zzzz,zz,zzzzzzzz"]
# split on spaces and then on commas
@stats = @doc.text.split(' ').map { |a| a.split(",") }
# => [["xxxx","xxxx","xxxxxxxxxx"],["yyyy","yy","yyyyyyyy"],["zzzz","zz","zzzzzzzz"]]
Upvotes: 3