how do i create charts from a cvs file using ruby - ruby

csv sample:
Date,128,440,1024,Mixed
6/30/2010,342,-0.26%,-0.91%,1.51%,-0.97%
6/24/2010,0.23%,0.50%,-1.34%,0.67%
i want to render this data in a multi-line graph

Well, you first need to parse the CSV. I suggest FasterCSV - the RDoc explains pretty much everything you need to know.
You'll need to have ImageMagick and RMagick installed, then you can use Gruff. Or if you've got an Internet connection on the machine you are running the script on, you can use Google Charts with this Ruby plugin. Or if you want to get back SVG, consider Scruffy.
The page about Gruff has a code sample showing how to create a multi-line graph. Basically, you need to collect together all the data you want in each line into an array. Looks basically like the primary thing you need to do is array mangling.

Related

Regex for Ruby code extraction out of plain text?

I want to extract ruby code snippets out of plain text.
Using the gem https://github.com/Erol/yomu makes it possible to extract the text of a PDF document. Now I want to get just the well-formed ruby code out of, for instance, a ruby-programming-book.
Any idea how a regex for multi-line matches of ruby methods and classes could look like?
I tried many different expressions, but did not get the results, that I expected.
Try this
Go through the file line by line and try to parse each line as Ruby code
If a line parses as Ruby start adding more lines to it until they don't parse as Ruby code anymore
Voila, here is your first code snippet
Maybe apply some filter to exclude trivial snippets like single words
Repeat
This is the common best practice to extract source code from unstructured text like emails and what not. This has been used to scan millions of emails for research projects.
Use the ripper core library to parse Ruby code.

How to simply pull information from excel to use as a variable ruby

I am trying to pull product ID's, that are in Excel, into ruby to use as a temporary variable. I cannot find a simple solution and am not sure if I should use the spreadsheet gem or axlsx gem. If you guys could help get me started that would be amazing!
One simple solution would be to save the excel document as a csv. Then it would be relatively easy to to open the file with your ruby program, read in the data you need and put that into a variable, for example using the CSV library
CSV.foreach("path/to/file.csv") do |row|
# use row here
end

Wiki quotes API?

I would want to get a structured version of a Wikiquote page via JSON (basically I need all phrases)
Example: http://en.wikiquote.org/wiki/Fight_Club_(film)
I tried with: http://en.wikiquote.org/w/api.php?format=xml&action=parse&page=Fight_Club_(film)&prop=text
but I get all HTML source code. I need each pharse as an element of an Array
How could I achieve that with DBPEDIA?
For one thing Iam not sure whether you can query wiki quotes using DBpedia and secondly, DBpedia gives you only info box data in a structured way, it does not in a any way the article content in a structured way. Instead with a little bit of trouble you can use the Media wiki api to get the data
EDIT
The URI you are trying gives you a text so this will make things easier, but not completely.
Try this piece of code in your console:
require 'Nokogiri'
content = JSON.parse(open("http://en.wikiquote.org/w/api.php?format=json&action=parse&page=Fight_Club_%28film%29&prop=text").read)
data = content['parse']['text']['*']
xpath_data = Nokogiri::HTML data
xpath_data.xpath("//ul/li").map{|data_node| data_node.text}
This is the closest I have come to an answer, of course this is not completely right because you will get a lot on unnecessary data. But if you dig into Nokogiri and xpath and find out how to pin point the nodes you need you can get a solution which will give you correct quotes at least 90% of the time.
Just change the format to JSON. Look up the Wikipedia API for more details.
http://en.wikiquote.org/w/api.php?format=json&action=parse&page=Fight_Club_(film)&prop=text

How to create a spreadsheet with formulas using Rails?

I need some gem/plugin to create an Excel spreadsheet with formulas to use in my Rails application. Any suggestions?
I've used Roo and it's quite good and easy to do spreadsheet processing (once you get all the gem dependencies installed). However, it doesn't support formulas natively. It won't eval the formula and return the result (this would be difficult I think -- use the excel engine?) but it will give you the text of the formula, for example:
=SUM(.A1,.B1)
It'd be pretty easy to handle this specific case but if you have many different formulas and functions then rolling your own evaluator is going to be difficult. Going and getting A1 and B1 to add them together is very doable with Roo. It's just a question of how complex your formulas are.
writeexcel does it wonderfully!
I think you should create blank Excel file with formulas and then fill it with Rails. Because you can't create formulas with Ruby.
There's a spreadsheet gem listed on RubyGems but having never used it I can't recommend it.

Ruby XML Parsing with Nokogiri/XPath

I have a shopify store that I want to automatically update the product variants inventory levels with, using a live xml feed from the wholesaler I use.
I'm learning to program (Ruby) and this is my first project, but after researching here is how I think it should work.
Use Ruby/Nokugiri to parse the XML feed from the wholesaler, and then Xpath to locate both the unique product variant SKU code, and the stock level.
Somehow I need to use this SKU to refer back to my Shopify store product XML list, and pull out the variants unique ID using the SKU code.
Then use something like the builder gem to build the XML format that shopify needs, and then use curl to PUT the changes. I'm guessing I loop this process for every product?
I know Shopify only has a 300 call limit, so I've got the article on putting a delay in the script, but I get the feeling the above method isn't the easiest way to go about this?
With Shopify you need to apply the variant stock level update against unique variant xml files, so I need to build the unique xml file/code and PUT it against /admin/variants/#[thevariantid].xml
I'm looking forward to trying to put this together and learning in the process, but am I on the right track with this? Are there simpler gems I should be looking at?
n.b I've only recently started learning Ruby, and will head to Rails afterwards. I know a bit about XML and it's structure so should be ok finding what I need with XPath.
You’re on the right track, but I’d use the shopify_api gem to do the talking to Shopify instead of having to form the XML and URIs yourself: https://github.com/Shopify/shopify_api
There’s an article on our wiki that might also help you out with regards to the API call limit but just let me know if you need more space – we’re pretty flexible and the limit is really just there to keep scripts from going wild and affecting service for everyone else.
Your proposed path seems good, except that there's no need to use the 'builder' gem, as Nokogiri has some very nice XML-building built into it.

Resources