How do I add a gem to a group from within a Thor task? - thor

I'm in a Thor task where I'd like to add gems to the assets group in my gem file (see below).
The gem_group command (known from standard Rails App Templates) doesn't seem work in a Thor task (see error at bottom).
How could i best add a gem to a group in the gem file from within a Thor task?
Any thought welcome - thanks.
class ThorBuild < Thor
desc "add_gems", "Adds the gems I need"
def add_gems
puts "Running add_gems"
gem 'mongoid', '~> 3.0.15'
gem 'bson_ext'
gem 'less-rails'
gem 'devise'
gem_group :assets do
gem 'therubyracer', '= 0.10.0', :platforms => :ruby
gem 'twitter-bootstrap-rails'
end
end
end
The output looks like this ...
Running add_gems
/home/ubuntu/sites/ThorBuild.thor:12:in `add_gems': undefined method `gem_group' for #<Thor::Sandbox::ThorBuild:0x000000007b5110> (NoMethodError)
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/task.rb:27:in `run'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/invocation.rb:120:in `invoke_task'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor.rb:275:in `dispatch'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/base.rb:425:in `start'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/runner.rb:36:in `method_missing'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/task.rb:29:in `run'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/task.rb:126:in `run'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/invocation.rb:120:in `invoke_task'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor.rb:275:in `dispatch'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/base.rb:425:in `start'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/bin/thor:6:in `<top (required)>'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/bin/thor:19:in `load'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/bin/thor:19:in `<main>'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `eval'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `<main>'

Related

How to configure Ocotshark (connection pooling) to work with Ruby 3 and Rails 6.1?

I’m using Rails 6.1.4.4, Octoshark 0.3.0, and just upgraded to Ruby 3.0.2. Now when starting Rails console, I get
/Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1043:in `establish_connection': wrong number of arguments (given 2, expected 1) (ArgumentError)
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/octoshark-0.3.0/lib/octoshark/active_record_extensions.rb:5:in `establish_connection'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/connection_handling.rb:52:in `establish_connection'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/railtie.rb:222:in `block (2 levels) in <class:Railtie>'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:71:in `class_eval'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:61:in `with_execution_control'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:66:in `execute_hook'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:43:in `block in on_load'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:42:in `each'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:42:in `on_load'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/railtie.rb:217:in `block in <class:Railtie>'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:32:in `instance_exec'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:32:in `run'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:61:in `block in run_initializers'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:228:in `block in tsort_each'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:431:in `each_strongly_connected_component_from'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:349:in `block in each_strongly_connected_component'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `each'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `call'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `each_strongly_connected_component'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:226:in `tsort_each'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:205:in `tsort_each'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:60:in `run_initializers'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/application.rb:391:in `initialize!'
from /Users/myuser/Documents/workspace/cfs/cfs-web/config/environment.rb:5:in `<top (required)>'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:332:in `require'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:332:in `block in require'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:299:in `load_dependency'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:332:in `require'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/application.rb:367:in `require_environment!'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command/actions.rb:28:in `require_environment!'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command/actions.rb:15:in `require_application_and_environment!'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/commands/console/console_command.rb:101:in `perform'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command/base.rb:69:in `perform'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command.rb:48:in `invoke'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
I have this configured in config/initializers/octoshark.rb
CONN_MANAGER = Octoshark::ConnectionPoolsManager.new({
readonly: Rails.application.config.database_configuration['readonly'],
third_party_readonly: {
adapter: 'sqlserver',
username: APP_CONFIG.third_party_read_secondary[Rails.env].username,
password: APP_CONFIG.third_party_read_secondary[Rails.env].password,
host: APP_CONFIG.third_party_read_secondary[Rails.env].host,
database: APP_CONFIG.third_party_read_secondary[Rails.env].database
}
})
class ActiveRecord::Base
def self.connection
CONN_MANAGER.current_or_default_connection
end
end
Not sure what other changes to the above or other upgrades I would need to make to get my connection pooling configured properly and things loaded correctly.
Edit: Per the suggestion given, I created a file, lib/core_extensions/octoshark/connection_handler.rb, with this
module CoreExtensions
module Octoshark
module ConnectionHandler
def establish_connection(config, **parameters)
Octoshark::ConnectionPoolsManager.reset_connection_managers!
super(config, **parameters)
end
end
end
end
and then a file, config/initializers/ext.rb, with this
Dir.glob(Rails.root.join('lib/ext/**/*.rb')).sort.each do |filename|
require filename
end
but am still getting the error.
That has to do with how keyword arguments are handled in ruby 3.0 and how octoshark is passing its arguments to .establish_connection.
So, I think you could monkey-patch the gem method or work with a fork, where the parameters are explicitely passed to the active-record method:
module Octoshark
module ConnectionHandler
def establish_connection(config, **parameters)
Octoshark::ConnectionPoolsManager.reset_connection_managers!
super(config, **parameters)
end
end
end

Getting NoMethodError when using jdbc sqlserver adapter with jRuby on Rails

I have a jRuby on Rails application hosted on IIS 8 on Windows Server 2012 R2 (I know, I must be insane). After about 2 weeks of painful programming I have a working application but I'm trying to configure it to use SQL Server 2012 Express via the Microsoft JDBC SQL Server driver instead of the default sqlite3 database. So far, this is not going well.
After configuring my database.yml file I tried to run: bundle exec rake db:migrate RAILS_ENV=production --traceI am met with the error rake aborted! NoMethodError: undefined method 'type' for "nvarchar(255)":StringA full trace of my error can be seen below:
rake db:migrate
C:\inetpub\wwwroot\rails>bundle exec rake db:migrate RAILS_ENV=production --trac
e
io/console not supported; tty will not be manipulated
io/console not supported; tty will not be manipulated
NOTE: ActiveRecord 4.2 is not (yet) fully supported by AR-JDBC, please help us f
inish 4.2 support - check http://bit.ly/jruby-42 for starters
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
rake aborted!
NoMethodError: undefined method `type' for "nvarchar(255)":String
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/a
ttribute_methods/time_zone_conversion.rb:64:in `create_time_zone_conversion_attr
ibute?'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/a
ttribute_methods/time_zone_conversion.rb:53:in `inherited'
org/jruby/RubyProc.java:271:in `call'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/a
ttribute_decorators.rb:61:in `matching'
org/jruby/RubyArray.java:2470:in `select'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/a
ttribute_decorators.rb:60:in `matching'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/a
ttribute_decorators.rb:56:in `decorators_for'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/a
ttribute_decorators.rb:47:in `apply'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/a
ttribute_decorators.rb:29:in `add_user_provided_columns'
org/jruby/RubyArray.java:2414:in `map'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/a
ttribute_decorators.rb:28:in `add_user_provided_columns'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/a
ttributes.rb:93:in `columns'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/a
ttributes.rb:98:in `columns_hash'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/q
uerying.rb:41:in `find_by_sql'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/r
elation.rb:638:in `exec_queries'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/r
elation.rb:514:in `load'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/r
elation.rb:243:in `to_a'
C:0:in `map'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/m
igration.rb:844:in `get_all_versions'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/m
igration.rb:985:in `migrated'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/m
igration.rb:990:in `ran?'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/m
igration.rb:967:in `runnable'
org/jruby/RubyArray.java:2640:in `reject!'
org/jruby/RubyArray.java:2611:in `reject'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/m
igration.rb:967:in `runnable'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/m
igration.rb:952:in `migrate'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/m
igration.rb:820:in `up'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/m
igration.rb:798:in `migrate'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/t
asks/database_tasks.rb:137:in `migrate'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/activerecord-4.2.2/lib/active_record/r
ailties/databases.rake:44:in `(root)'
org/jruby/RubyProc.java:271:in `call'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/task.rb:240:in `e
xecute'
org/jruby/RubyArray.java:1613:in `each'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/task.rb:235:in `e
xecute'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/task.rb:179:in `i
nvoke_with_call_chain'
C:/jruby-1.7.22/lib/ruby/1.9/monitor.rb:211:in `mon_synchronize'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/task.rb:172:in `i
nvoke_with_call_chain'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/task.rb:165:in `i
nvoke'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/application.rb:15
0:in `invoke_task'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/application.rb:10
6:in `top_level'
org/jruby/RubyArray.java:1613:in `each'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/application.rb:10
6:in `top_level'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/application.rb:11
5:in `run_with_threads'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/application.rb:10
0:in `top_level'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/application.rb:78
:in `run'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/application.rb:17
6:in `standard_exception_handling'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/lib/rake/application.rb:75
:in `run'
C:/jruby-1.7.22/lib/ruby/gems/shared/gems/rake-10.4.2/bin/rake:33:in `(root)'
org/jruby/RubyKernel.java:1059:in `load'
C:\jruby-1.7.22\bin\rake:23:in `(root)'
Tasks: TOP => db:migrate
database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
adapter: sqlserver
database: smdb
url: jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;databaseName=smdb;user=sa;password=password123;
Gemfile
source 'https://rubygems.org'
ruby '1.9.3', :engine => 'jruby', :engine_version => '1.7.22'
gem 'rails', '4.2.2'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sass-rails', '5.0.2'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.1.0'
gem 'coffee-script-source', '1.8.0'
gem 'jquery-rails', '4.0.3'
gem 'turbolinks', '2.3.0'
gem 'jbuilder', '2.2.3'
gem 'faker', '1.4.2'
gem 'tzinfo-data', '1.2015.6'
gem 'sdoc', '0.4.0', group: :doc
group :development, :test do
gem 'activerecord-jdbcsqlite3-adapter', '1.3.17'
gem 'spring', '1.1.3'
end
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end
group :production do
gem 'trinidad', '1.4.6'
gem 'deprecated', '3.0.1'
gem 'activerecord-jdbcmssql-adapter', '1.3.17'
end
And finally my two Database Migration files:
20150805101650_create_servers.rb
class CreateServers < ActiveRecord::Migration
def change
create_table :servers do |t|
t.string :server_name
t.string :application
t.string :server_role
t.string :team_contact
t.string :individual_contact
t.string :business_owner
t.string :vendor
t.string :vendor_contact
t.string :main_doc
t.string :main_win
t.timestamps null: false
end
end
end
20150805105953_add_index_to_server_name.rb
class AddIndexToServerName < ActiveRecord::Migration
def change
add_index :servers, :server_name, unique: true
end
end
I know that the instance is being hit as I was previously met with an authentication error which has gone since I added the username and password to the jdbc connection string
Solution
Downgraded to Rails 4.1.0 which seems to be more compatible with JDBC Adapter. Ran a rake db:migrate RAILS_ENV=production which resulted in another error. The error was resolved by removing config.active_record.raised_in_transactional_callbacks = true from application.rb
AR-JDBC does not handle Rails 4.2 esp. with databases such as MS-SQL, there's the warning :
NOTE: ActiveRecord 4.2 is not (yet) fully supported by AR-JDBC, please help us finish 4.2 support - check http://bit.ly/jruby-42 for starters
... SQLServer support needs your help/support!

"End of file reached" error OR Zlib::BufError using HTTParty

I am experiencing two different errors - depending on whether I use Httparty in my Ruby app or I use it from the command line.
URL I am trying to access: http://apiv2.ahrefs.com/?token=$INSERT_VALID_AHREFS_API_TOKEN&from=backlinks&output=json&mode=domain&target=blog.ahrefs.com&limit=1000
HTTP method: GET
When I do so directly in bundle console I get Zlib::BufError:
HTTParty.get "http://apiv2.ahrefs.com/?token=$INSERT_VALID_AHREFS_API_TOKEN&from=backlinks_new_lost&output=json&mode=domain&target=blog.ahrefs.com&limit=1000"
Zlib::BufError: buffer error
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:357:in `finish'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:357:in `finish'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:262:in `ensure in inflater'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:262:in `inflater'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:274:in `read_body_0'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:201:in `read_body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:226:in `body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:163:in `reading_body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1414:in `block in transport_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1405:in `catch'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1405:in `transport_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1378:in `request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1371:in `block in request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:853:in `start'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1369:in `request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty/request.rb:98:in `perform'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:539:in `perform_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:475:in `get'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:577:in `get'
from (irb):3
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/cli/console.rb:38:in `run'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/cli.rb:287:in `console'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/vendor/thor/command.rb:27:in `run'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/vendor/thor/invocation.rb:121:in `invoke_command'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/vendor/thor.rb:363:in `dispatch'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/vendor/thor/base.rb:440:in `start'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/cli.rb:9:in `start'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/bin/bundle:20:in `block in <top (required)>'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/friendly_errors.rb:5:in `with_friendly_errors'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/bin/bundle:20:in `<top (required)>'
from /Users/medic/.rbenv/versions/2.1.2/bin/bundle:23:in `load'
from /Users/medic/.rbenv/versions/2.1.2/bin/bundle:23:in `<main>'irb(main):004:0>
Same error occurs for the following command:
Net::HTTP.get(URI('http://apiv2.ahrefs.com/?token=$AHREFS_API_TOKEN&from=backlinks_new_lost&output=json&mode=domain&target=blog.ahrefs.com&limit=1000'))
When I do it from the command line:
httparty "http://apiv2.ahrefs.com/?token=$INSERT_VALID_AHREFS_API_TOKEN&from=backlinks_new_lost&output=json&mode=domain&target=blog.ahrefs.com&limit=1000"
/Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/protocol.rb:153:in `read_nonblock': end of file reached (EOFError)
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/protocol.rb:153:in `rbuf_fill'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/protocol.rb:104:in `read'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:316:in `ensure in read_chunked'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:316:in `read_chunked'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:276:in `block in read_body_0'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:269:in `inflater'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:274:in `read_body_0'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:201:in `read_body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:226:in `body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:163:in `reading_body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1414:in `block in transport_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1405:in `catch'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1405:in `transport_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1378:in `request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1371:in `block in request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:853:in `start'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1369:in `request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty/request.rb:98:in `perform'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:539:in `perform_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:475:in `get'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:577:in `get'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/bin/httparty:90:in `<top (required)>'
from /Users/medic/.rbenv/versions/2.1.2/bin/httparty:23:in `load'
from /Users/medic/.rbenv/versions/2.1.2/bin/httparty:23:in `<main>'
The problem disappears when I reduce the limit query parameter to limit=10.
My assumption is that this is because the server returns a chunked response, if there is enough data.
I tested the URL with curl and it worked without a hitch. That's why I am assuming it is a problem in HTTParty

Mongoid Habtm relationship with specified class_name

I have two very simple classes:
class Contact
include Mongoid::Document
has_and_belongs_to_many :organizations, :class_name => 'Organization'
end
and
class Organization
include Mongoid::Document
has_and_belongs_to_many :relations, :class_name => 'Contact'
end
and here is the error log upon validating / saving:
>> org = Organization.new
#<Organization _id: 5228ae3c1d41c8678c000001, relation_ids: nil, _type: "Organization">
>> org.valid?
org.valid?
NoMethodError: undefined method `has_key?' for #<Mongoid::Criteria:0x00000008d32bd0>
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/criteria.rb:501:in `method_missing'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/relations/referenced/many.rb:413:in `block in method_missing'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/scopable.rb:238:in `with_scope'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/relations/referenced/many.rb:412:in `method_missing'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/validatable.rb:72:in `read_attribute_for_validation'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:151:in `block in validate'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:150:in `each'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:150:in `validate'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:283:in `_callback_before_145'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:377:in `_run__4324115989737504907__validate__callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/interceptable.rb:132:in `run_callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations.rb:373:in `run_validations!'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations/callbacks.rb:106:in `block in run_validations!'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__4324115989737504907__validation__callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/interceptable.rb:132:in `run_callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations/callbacks.rb:106:in `run_validations!'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations.rb:314:in `valid?'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/validatable.rb:98:in `valid?'
from (irb):3
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from /home/muichkine/src/white/bin/rails:4:in `require'
from /home/muichkine/src/white/bin/rails:4:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
>> contact = Contact.new
contact = Contact.new
#<Contact _id: 5228ae441d41c8678c000002, organization_ids: nil, _type: "Contact">
>> contact.valid?
true
as you can see, validating a Contact works while validating an Organization does not. Now, I did try to add :inverse_of options on both sides but the error remains. Any input is appreciated.
I use:
mongoid (4.0.0 38de2e9) with Rails 4 / Ruby 2.0.0-p247
So the problem is the name of the second has_and_belongs_to_many . If you change it to anything else it should work. Anyways thats a short solution for you, you should also file a issue on monogid, about that situation. I am pretty much sure this is happening because relations is a internal method on the Mongoid Document.

Ruby 2.0 Create Multipart Form Data

I have written RUBY code for creating multipart form data request. It is working perfectly in ruby 1.9.3, but it is raising error in ruby 2.0 patch 247 and patch 0.
The error is,
/home/mohanrajr/Ruby/multipart/multipart_stream.rb:16:in `read': wrong number of arguments (2 for 1) (ArgumentError)
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/generic_request.rb:205:in `copy_stream'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/generic_request.rb:205:in `send_request_with_body_stream'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/generic_request.rb:132:in `exec'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1404:in `block in transport_request'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1403:in `catch'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1403:in `transport_request'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1376:in `request'
from /home/mohanrajr/Ruby/multipart/multipart.rb:38:in `block in post'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:852:in `start'
from /home/mohanrajr/Ruby/multipart/multipart.rb:38:in `post'
from multipart_post.rb:4:in `<main>'
Thanks for advance.

Resources