I'm developing an application using Visual Ruby. In that I'm fetching a date from a dropdown like below:
check_to_in_1 = #builder.get_object("cellrenderertext7")
then I split this date using the split method:
date_split = check_to_in_1.text.to_s.split("/")
I do this split because I want to convert the date from String to DateTime format, after splitting i print the values like below:
puts "#{date_split[2]}" # => 05
puts "#{date_split[1]}" # => 10
puts "#{date_split[0]}" # => 2013
Now I passed this value to the DateTime.new method to convert it to DateTime:
check_to_in_time_converted = DateTime.new(date_split[0],
date_split[1], date_split[2])
Now here I got this error:
C:/Users/abhiram/visualruby/examples/fedena/bin/SendAbsentees.rb:213:in `new': undefined method `div' for "05":String
from C:/Users/abhiram/visualruby/examples/fedena/bin/SendAbsentees.rb:213:in `button1_clicked'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/gtk2-1.2.1-x86-mingw32/lib/gtk2/base.rb:95:in `call'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/gtk2-1.2.1-x86-mingw32/lib/gtk2/base.rb:95:in `block in __connect_signals__'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `call'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `main'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `show_window'
from C:/Users/abhiram/visualruby/examples/fedena/bin/SendAbsentees.rb:99:in `show'
from C:/Users/abhiram/visualruby/examples/fedena/bin/Control.rb:36:in `button2_clicked'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/gtk2-1.2.1-x86-mingw32/lib/gtk2/base.rb:95:in `call'
I don't know what to do from here, could anyone help me to come out of this?
As you can read in the calls stack trace, DateTime.new is sending the method div to the string "05" that is not defined:
[...]/bin/SendAbsentees.rb:213:in `new': undefined method `div' for "05":String
That is because DateTime.new expects integers as arguments. You have to convert to integers the elements of date_split before passing them to DateTime.new:
DateTime.new(*date_split.map(&:to_i))
Even better you can do it without splitting the string, using DateTime.strptime instead of DateTime.new, like this:
DateTime.strptime(check_to_in_1.text.to_s, '%Y/%m/%d')
# => #<DateTime: 2013-05-10T00:00:00+00:00 ((2456423j,0s,0n),+0s,2299161j)>
I assumed your dates are in the format YEAR/MONTH/DAY, if instead they are in the format YEAR/DAY/MONTH you just have to swap %m and %d in the second argument to strptime.
Related
I am trying to use ParameterType as Transform was removed in cucumber from version 3.0 I have the below feature with the step definition and parameter type. Is there any problem in my ParameterType as it is throwing error as follows:
Feature File:
Scenario: Learn how to write transformations
When I print the date for today
Step Definition
When('I print the date for {today}') do |today|
puts today
end
Here is the ParameterType am using:
ParameterType(
name: 'today',
regexp: /[\w]+/,
transformer: Date.today
)
Error am getting:
When I print the date for date
features/wb_test.feature:11
undefined method `map' for nil:NilClass (NoMethodError)
c:\jruby-9.2.7.0\bin\cucumber:23:in `
'
if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('cucumber', 'cucumber', version)
else
gem "cucumber", version
Please let me know if am doing anything wrong in the above steps.
When I run your code I get this error:
Scenario: Learn how to write transformations
When I print the date for today
wrong number of arguments (given 1, expected 0) (ArgumentError)
./features/support/parameter_types.rb:16:in `block in <top (required)>'
features/file.feature:15:in `When I print the date for today'
This indicates that the transformer needs to be a function. I got it to work by changing your ParameterType definition to:
ParameterType(
name: 'today',
regexp: /[\w]+/,
transformer: ->(str) {Date.today}
)
I have been developing a backend with padrino ruby framework and I would like to build a cron job.
This is what I have done.
gem 'whenever', :require => false
wheneverize .
inside schedule.rb
every 1.minute do
rake "cronjob"
end
/tasks/cronjob.rake
Here I added my custom tasks. and this will be to long to add here.
So I wrote only error happening parts
performances = Performance.left_join(:slots, id: :slot_id).where(Sequel.~(status: ModelA::Api.settings.pending),Sequel[:slots][:from]>oneweekbefore,Sequel[:slots][:to]<onemonthafter+1.day)
....
begin
data = {}
data[:from] = "** <postmaster#**.mailgun.org>"
data[:to] = email
data[:subject] = subject
data[:html] = render 'mails/sendemailbasedontime',:locals => { :data => localdata }
RestClient.post GigabitArtist::Api.settings.mailgun_domain, data
rescue => exception
puts exception.inspect
end
end
I got these errors:
SEQUEL DEPRECATION WARNING: Passing multiple arguments as filter
arguments when not using a conditions specifier
([#:!=, #args=>[:status,
"pending"]>, #:>,
#args=>[#"slots",
#column=>:from>, Sat, 02 Dec 2017]>, #:<, #args=>[#"slots",
#column=>:to>, Wed, 10 Jan 2018]>]) is deprecated and will be removed
in Sequel 5. Pass the arguments to separate filter methods or use
Sequel.& to combine them.
/Users/whitesnow/.rvm/gems/ruby-2.4.1/gems/sequel-4.46.0/lib/sequel/dataset/query.rb:1296:in
filter_expr'
/Users/whitesnow/.rvm/gems/ruby-2.4.1/gems/sequel-4.46.0/lib/sequel/dataset/query.rb:1249:in
add_filter'
/Users/whitesnow/.rvm/gems/ruby-2.4.1/gems/sequel-4.46.0/lib/sequel/dataset/query.rb:1034:in
where'
/Volumes/Data/Work/RBP/GAB/tasks/cronjob.rake:12:inblock in '
/Users/whitesnow/.rvm/gems/ruby-2.4.1#global/gems/rake-12.0.0/lib/rake/task.rb:250:in
block in execute'
/Users/whitesnow/.rvm/gems/ruby-2.4.1#global/gems/rake-12.0.0/lib/rake/task.rb:250:in
each'
/Users/whitesnow/.rvm/gems/ruby-2.4.1#global/gems/rake-12.0.0/lib/rake/task.rb:250:in
execute'
/Users/whitesnow/.rvm/gems/ruby-2.4.1#global/gems/rake-12.0.0/lib/rake/task.rb:194:in
block in invoke_with_call_chain'
/Users/whitesnow/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/monitor.rb:214:in
mon_synchronize'
/Users/whitesnow/.rvm/gems/ruby-2.4.1#global/gems/rake-12.0.0/lib/rake/task.rb:187:in
invoke_with_call_chain'
I think errors are from sequel querying and
data[:html] = render 'mails/sendemailbasedontime',:locals => { :data => localdata }
ofcourse, this query was tested in other .rb file and I tested with raw sql. for example, I tested this tasks inside get request hander of test.rb controller. and it does work well
I would like to know if I can use render function inside task.
I searched all day for this problem with no success.
Any advice will be big help for me.
Thank you very much.
As the deprecation warning states, you are passing multiple arguments to a filter method. The simplest fix would be to call a filter method separately for each argument:
performances = Performance.
left_join(:slots, id: :slot_id).
exclude(status: ModelA::Api.settings.pending).
where(Sequel[:slots][:from]>oneweekbefore).
where(Sequel[:slots][:to]<onemonthafter+1.day)
I have a gem I have created that wraps Git as a key:value store (dictionary/hash). The source is here.
The way it works in the process referenced is as follows:
run the function set containing a key and a value argument
hash these with git, have the key point at the hash
return the key if this operation is successful and it is added to the global dictionary holing keys and hashes
Now, if I call something like
db.set('key', {some: 'value'})
# => 'key'
and then try to retrieve this,
db.get('key')
Psych::SyntaxError: (<unknown>): did not find expected node content while parsing a flow node at line 1 column 2
from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse'
from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse_stream'
from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:318:in `parse'
from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:245:in `load'
from /home/bobby/.rvm/gems/ruby-2.2.1/gems/gkv-0.2.1/lib/gkv/database.rb:21:in `get'
from (irb):6
from /home/bobby/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'
Now, if I set the key as that same dictionary, but as a string:
db.set('key', "{some: 'value'}")
# => 'key'
db.get('key')
# => {"key"=>"value"}
db.get('key').class
=> Hash
The operation that is performing the git operations' and wrapping them to a kv store source is:
...
def get(key)
if $ITEMS.keys.include? key
YAML.load(Gkv::GitFunctions.cat_file($ITEMS[key].last))
else
raise KeyError
end
end
def set(key, value)
update_items(key, value.to_s)
key
end
...
And the get_items function being referenced here's source is:
...
def update_items(key, value)
if $ITEMS.keys.include? key
history = $ITEMS[key]
history << Gkv::GitFunctions.hash_object(value.to_s)
$ITEMS[key] = history
else
$ITEMS[key] = [Gkv::GitFunctions.hash_object(value.to_s)]
end
end
end
...
hash_object and cat_object simple wrap git hash-object and git cat-file in a method writing the input to a tmpfile, git adding it, and then erasing the tempfile.
I'm really at a loss as to why this works with strings but not true dictionaries. It results in the exact same error if you use the old hashrocket syntax as well:
db.set('a', {:key => 'value'})
=> "a"
db.get('a')
# => Psych::SyntaxError: (<unknown>): did not find expected node content while parsing a flow node at line 1 column 2
from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse'
from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse_stream'
from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:318:in `parse'
from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:245:in `load'
from /home/bobby/.rvm/gems/ruby-2.2.1/gems/gkv-0.2.1/lib/gkv/database.rb:21:in `get'
from (irb):6
from /home/bobby/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'
Any ideas?
In your get method you call YAML.load, but in your set method you use .to_s. This means that the YAML parser is trying to read an arbitrary string as if it were YAML. For symmetry YAML.dump should be used in the set method instead.
I've created a pull request with the changes.
I'm trying to read local image file, properly encode it and post to Tumbrl. According to the Tumblr API I can pass a parameter data which is Array (URL-encoded binary contents) Limit: 5 MB
I've tested my code with http://api.tumblr.com/v2/blog/#{BLOG}/info request. It is working. But I can't post a photo. Here is my code:
require 'oauth'
require 'oauth/consumer'
require 'open-uri'
require 'active_support'
CONSUMER = 'foo'
SECRET = 'foo'
TOKEN = 'foo'
TOKEN_SECRET = 'foo'
BLOG = 'foo'
consumer=OAuth::Consumer.new(CONSUMER, SECRET, {:site=>"http://tumblr.com"})
access_token = OAuth::AccessToken.new(consumer, TOKEN, TOKEN_SECRET)
# Here I tried one of two lines:
# data = Base64.encode64(IO.binread('./resized')) #first try
data = URI::encode(IO.binread('./resized')) #second try
# response = access_token.get "http://api.tumblr.com/v2/blog/#{BLOG}/info?api_key=#{CONSUMER}"
# puts response
response=access_token.post "http://api.tumblr.com/v2/blog/#{BLOG}/post?api_key=#{CONSUMER}&type=photo&data=#{data}&link=http://ya.ru&"
puts response
1st try:
% ruby ./w_oauth.rb
/usr/lib/ruby/1.9.1/uri/common.rb:176:in `split': bad URI(is not URI?): http://api.tumblr.com/v2/blog/foo/post?api_key=foo&type=photo&data=/9j/4AAQSkZJRgABAQEASABIAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4w (URI::InvalidURIError)
ICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gODAK/9sAQwAGBAUG
BQQGBgUGBwcGCAoQCgoJCQoUDg8MEBcUGBgXFBYWGh0lHxobIxwWFiAsICMm
(!!!long piece of image data skipped!!!)
FI/16HfTbyHPWurqdE+TGH4wx2js5SKQb+6b4bIj3aurqCrEtcXrf/4yf/dS
DLet/wCzEB6sa6uoomxJN2eaQj5mkYuerQj611dQM7Fx/wDLF/8AbXV1dTA/
/9k=
&link=http://ya.ru&
from /usr/lib/ruby/1.9.1/uri/common.rb:211:in `parse'
from /usr/lib/ruby/1.9.1/uri/common.rb:747:in `parse'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/tokens/access_token.rb:7:in `request'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/tokens/access_token.rb:47:in `post'
from ./w_oauth.rb:23:in `<main>'
2nd try:
% ruby ./w_oauth.rb
/var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:14:in `force_encoding': can't modify frozen String (RuntimeError)
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:14:in `rescue in escape'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:12:in `escape'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:43:in `block (2 levels) in normalize'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:42:in `collect'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:42:in `block in normalize'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:37:in `map'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:37:in `normalize'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/request_proxy/base.rb:98:in `normalized_parameters'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/request_proxy/base.rb:113:in `signature_base_string'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/signature/base.rb:77:in `signature_base_string'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/signature/hmac/base.rb:12:in `digest'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/signature/base.rb:65:in `signature'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/signature.rb:23:in `sign'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/client/helper.rb:45:in `signature'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/client/helper.rb:75:in `header'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/client/net_http.rb:91:in `set_oauth_header'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/client/net_http.rb:30:in `oauth!'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/consumer.rb:224:in `sign!'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/consumer.rb:188:in `create_signed_request'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/consumer.rb:159:in `request'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/tokens/consumer_token.rb:25:in `request'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/tokens/access_token.rb:12:in `request'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/tokens/access_token.rb:47:in `post'
from ./w_oauth.rb:23:in `<main>'
UPD: ./resized is a proper JPEG file:
% file ./resized
./resized: JPEG image data, JFIF standard 1.01, comment: "CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 80"
URI encoding is not enough. You also need to encode: , / ? : # & = + $ #.
Try:
URI.escape(IO.binread('./resized'), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
I have following CSV data:
10,11,12.34
I can parse this using CSV from the standard library, and have the values converted from strings to numbers:
require 'csv'
CSV.parse( "10,11,12.34" )
=> [["10", "11", "12.34"]]
CSV.parse( "10,11,12.34", {:converters => [:integer,:integer,:float]} )
=> [[10, 11, 12.34]]
I don't want to convert column 1, I'd just like that left as a string. My guess was I could omit a value from the converters array, but that didn't work:
CSV.parse( "10,11,12.34", {:converters => [nil,:integer,:float]} )
NoMethodError: undefined method `arity' for nil:NilClass
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:2188:in `convert_fields'
from org/jruby/RubyArray.java:1614:in `each'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:2187:in `convert_fields'
from org/jruby/RubyArray.java:2332:in `collect'
from org/jruby/RubyEnumerator.java:190:in `each'
from org/jruby/RubyEnumerator.java:404:in `with_index'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:2186:in `convert_fields'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:1923:in `shift'
from org/jruby/RubyKernel.java:1408:in `loop'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:1825:in `shift'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:1767:in `each'
from org/jruby/RubyEnumerable.java:391:in `to_a'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:1778:in `read'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:1365:in `parse'
from (irb):25:in `evaluate'
In fact I haven't been able to find any way of specifying that I'd like the first column to be left unconverted. Any suggestions?
Update
I think I misunderstood the design intention for :converters. It's not a 1:1 mapping by column, but a list of converters to be applied (I think) to all values. I'm not sure, the docs aren't too clear. So the more general question is: How do I convert some columns in my CSV, and not others?
The documentation says these options aren't specified per column, but are instead a list of converters that will be applied to all columns.
Example:
CSV.parse("10,11,13,12.34", { :converters => [lambda{|s|s.to_s + 'x'}] })
# => [["10x", "11x", "13x", "12.34x"]]
Since the CSV module is eager to convert everything it can, you may as well shift back any columns you want using .to_s or use the :unconverted_fields option to save the original values and allow access to them.