gchart not working - ruby

Okay, I want to draw charts within Ruby, so I've found gem called Googlecharts. Here's the problem:
-> % irb
1.9.2-p320 :001 > require 'gchart'
=> true
1.9.2-p320 :002 > Gchart.line(:size => '200x200', :title => 'title', :bg => 'ffffff', :data => [10,20,30,50])
=> "http://chart.apis.google.com/chart?"
As you can see, there's no link. What's wrong?

Related

Is there a trick to using phantomjs for javascript driven sites?

Trying to use phantomjs to get content created by javascript and having a little trouble. Does phantomjs execute javascript just like other browsers do? Hoping someone can help.
When trying to get the value I'm looking for in Chrome, it comes up. However the value from phantomjs is blank.
2.3.0 :001 > require 'watir'
=> true
2.3.0 :002 > c = Watir::Browser.new
=> #<Watir::Browser:0x4598f2f5193003a8 url="data:," title="">
2.3.0 :003 > p = Watir::Browser.new :phantomjs
=> #<Watir::Browser:0x..fc337930306e55a98 url="about:blank" title="">
2.3.0 :004 > c.goto("http://www.walgreens.com/store/c/as-seen-on-tv/ID=359457-tier3")
=> "http://www.walgreens.com/store/c/as-seen-on-tv/ID=359457-tier3"
2.3.0 :005 > p.goto(c.url)
=> "https://www.walgreens.com/store/c/as-seen-on-tv/ID=359457-tier3"
2.3.0 :006 > c.element(:xpath => ".//*[#class='wag-itemscounttxt']/strong").text
=> "103"
2.3.0 :007 > p.element(:xpath => ".//*[#class='wag-itemscounttxt']/strong").text
=> ""
As you can see the phantomjs value is not present.

how to access properties returned by whois parser in ruby?

require 'rubygems'
require 'whois'
c = Whois::Client.new
r = c.lookup("seogroup.com")
puts r.admin_contacts
produces this:
#<struct Whois::Record::Contact id=nil, type=2, name="Marvin Russell", organization="SEO Group, LLC", address="222 W Ontario", city="Chicago", zip="60654", state="Illinois", country="United States", country_code=nil, phone="847-452-9902", fax=nil, email="marvin#seogroup.com", url=nil, created_on=nil, updated_on=nil>
How do I get at these properties like "state", "email" and "name" etc.
When I run your code I get back an array:
2.1.2 :013 > r.admin_contacts
=> [#<struct Whois::Record::Contact id=nil, type=2, name="Marvin Russell", organization="SEO Group, LLC", address="222 W Ontario", city="Chicago", zip="60654", state="Illinois", country="United States", country_code=nil, phone="847-452-9902", fax=nil, email="marvin#seogroup.com", url=nil, created_on=nil, updated_on=nil>]
And then introspecting a bit I see:
2.1.2 :014 > r.admin_contacts.class
=> Array
2.1.2 :015 > r.admin_contacts.length
=> 1
2.1.2 :016 > r.admin_contacts[0].class
=> Whois::Record::Contact
Which I then took a look at the available methods:
2.1.2 :017 > r.admin_contacts[0].methods
=> [:id, :id=, :type, :type=, :name...
And then pulled up the name and email:
2.1.2 :018 > r.admin_contacts[0][:name]
=> "Marvin Russell"
2.1.2 :019 > r.admin_contacts[0][:email]
=> "marvin#seogroup.com"

Finding out variable class and difference between Hash and Array

Is it possible to clearly identify a class of variable?
something like:
#users.who_r_u? #=>Class (some information)
#packs.who_r_u? #=> Array (some information)
etc.
Can someone provide clear short explanation of difference between Class, Hash, Array, Associated Array, etc. ?
You can use:
#users.class
Test it in irb:
1.9.3p0 :001 > 1.class
=> Fixnum
1.9.3p0 :002 > "1".class
=> String
1.9.3p0 :003 > [1].class
=> Array
1.9.3p0 :004 > {:a => 1}.class
=> Hash
1.9.3p0 :005 > (1..10).class
=> Range
Or:
1.9.3p0 :010 > class User
1.9.3p0 :011?> end
=> nil
1.9.3p0 :012 > #user = User.new
=> #<User:0x0000010111bfc8>
1.9.3p0 :013 > #user.class
=> User
These were only quick irb examples, hope it's enough to see the use of .class in ruby.
You could also use kind_of? to test wheter its receiver is a class, an array or anything else.
#users.kind_of?(Array) # => true
You can find these methods in Ruby document http://ruby-doc.org/core-1.9.3/Object.html
#user.class => User
#user.is_a?(User) => true
#user.kind_of?(User) => true
found helpful: <%= debug #users %>
A difference between Class and Hash? They are too different to even provide normal answer. Hash is basically an array with unique keys, where each key has its associated value. That's why it's also called associative array.
Here is some explanation:
array = [1,2,3,4]
array[0] # => 1
array[-1] # => 4
array[0..2] # => [1,2,3]
array.size # => 4
Check out more Array methods here: http://ruby-doc.org/core-1.9.3/Array.html
hash = {:foo => 1, :bar => 34, :baz => 22}
hash[:foo] # => 1
hash[:bar] # => 34
hash.keys # => [:baz,:foo,:bar]
hash.values # => [34,22,1]
hash.merge :foo => 3921
hash # => {:bar => 34,:foo => 3921,:baz => 22 }
Hash never keeps order of the elments you added to it, it just preserves uniqueness of keys, so you can easily retreive values.
However, if you do this:
hash.merge "foo" => 12
you will get
hash # => {:bar => 34, baz => 22, "foo" => 12, :foo => 2}
It created new key-value pair since :foo.eql? "foo" returns false.
For more Hash methods check out this: http://www.ruby-doc.org/core-1.9.3/Hash.html
Class object is a bit too complex to explain in short, but if you want to learn more about it, reffer to some online tutorials.
And remember, API is your friend.

replicate CSV.generate_line behaviour of ruby 1.8.7 in ruby 1.9.2

ruby 1.9 now uses fastercsv, but how do i replicate the generate_line behaviour of ruby 1.8.7 ?
ruby-1.8.7-p334 :010 > require 'csv'
=> true
ruby-1.8.7-p334 :010 > CSV.generate_line(["ab","cd"], "\t")
=> "ab\tcd"
ruby-1.9.2-p180 :002 > require 'csv'
=> true
ruby-1.9.2-p180 :007 > CSV.generate_line(["ab","cd"], :row_sep => ?\t)
=> "ab,cd\t"
Notice how \t is between the two array items in ruby 1.8.7 and at last in 1.9.2
You have to use col_sep instead. row_sep is the row separator:
CSV.generate_line(["ab","cd"], :col_sep => ?\t)
=> "ab\tcd\n"
or
CSV.generate_line(["ab","cd"], :col_sep => ?\t, :row_sep => '')
=> "ab\tcd"
You can find more details and additional options in the documentation.
CSV.generate_line(['a','b','c'],:col_sep=>"\t")

Transliteration with Iconv in Ruby

When I'm trying to transliterate a Cyrillic utf-8 string with
Iconv.iconv('ascii//ignore//translit', 'utf-8', string).to_s
(see questions/1726404/transliteration-in-ruby)
I'm getting everything but those symbols that have to be transliterated.
For example: 'r-строка' → 'r-' and 'Gévry' → 'Gvry'.
What's wrong?
Ruby 1.8.7 / Rails 2.3.5 / WSeven
require 'iconv'
p Iconv.iconv('ascii//translit//ignore', 'utf-8', 'Gévry') #=> ["Gevry"]
# not 'ascii//ignore//translit'
For Cyrillic the translit gem might work.
It seems the solution is too tricky for me. Problem solved using stringex gem.
Another way is to create custom translit by tr and gsub methods of String without using iconv.
# encoding: UTF-8
def russian_translit(text)
translited = text.tr('абвгдеёзийклмнопрстуфхэыь', 'abvgdeezijklmnoprstufhey\'')
translited = translited.tr('АБВГДЕЁЗИЙКЛМНОПРСТУФХЭ', 'ABVGDEEZIJKLMNOPRSTUFHEY\'')
translited = translited.gsub(/[жцчшщъюяЖЦЧШЩЪЮЯ]/,
'ж' => 'zh', 'ц' => 'ts', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch', 'ъ' => '', 'ю' => 'ju', 'я' => 'ja',
'Ж' => 'ZH', 'Ц' => 'TS', 'Ч' => 'CH', 'Ш' => 'SH', 'Щ' => 'SCH', 'Ъ' => '', 'Ю' => 'JU', 'Я' => 'JA')
return translited
end
p russian_translit("В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!")
#=> "V chaschah juga zhil by tsitrus? Da, no fal'shivyj ekzempljar!"

Resources