Using gems in Ruby *basic* - ruby

I'm a complete newbie.
I have a gem I want to install and use, let's say it's this one:
https://rubygems.org/gems/linkedindata/versions/0.0.22
I'm using cmd:
gem install linkedindata
#1 gem installed
After that I add it to my gemfile:
gemrat 'linkedindata'
#gem 'linkedindata', '0.0.22' added to your Gemfile.
Now to use linkedindata I have to create a new object and specify my search. So I do:
**test.rb**
l = LinkedinData.new(1, "c:/users/proxylist.txt", true, true)
searchTerms = ['First', 'Second', 'Third']
l.getByKeywords(searchTerms)
Now I run the test.rb from command prompt:
test.rb:1:in `<main>': undefined local variable or meth
od `linkedindata' for main:Object (NameError)
So, I obviously need to require the 'linkedindata' gem here. I added:
**test.rb**
require 'LinkedinData'
l = LinkedinData.new(1, "c:/users/proxylist.txt", true, true)
searchTerms = ['First', 'Second', 'Third']
l.getByKeywords(searchTerms)
I get the following error:
C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `re
quire': cannot load such file -- linkedin-scraper (LoadError)
from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require
.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/linkedindata-0.0.22/lib/linkedin
data.rb:1:in `<top (required)>'
from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require
.rb:128:in `require'
from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require
.rb:128:in `rescue in require'
from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require
.rb:39:in `require'
from C:/Users/test.rb:1:in `<main>'
Am I doing something fundamentally wrong here? Or is there a problem with this gem?
UPDATE
The problem was within the gem, see Doon's answer below.
require 'rubygems'
require 'bundler/setup'
require 'linkedindata'
l = LinkedinData.new(1, "proxylist.txt", true, true)
searchTerms = ['First', 'Second', 'Third']
l.getByKeywords(searchTerms)
++++ correct how the gem requires "linkedin_scraper" in linkedindata.rb
Next problem occurs
C:/Ruby22/lib/ruby/gems/2.2.0/gems/linkedindata-0.0.22/lib/linkedin.rb:6:in `<cl
ass:Profile>': uninitialized constant Linkedin::Profile::ProxyManager (NameError
)
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/linkedindata-0.0.22/lib/linkedin
.rb:5:in `<module:Linkedin>'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/linkedindata-0.0.22/lib/linkedin
.rb:4:in `<top (required)>'
from linkedinscrape.rb:4:in `require'
from linkedinscrape.rb:4:in `<main>'
Which obviously has to do with the proxy settings. The list:
**proxylist.txt**
220.248.224.242:8089
165.139.179.225:8080
54.207.114.172:3333
190.63.174.246:8081
The way I load it:
l = LinkedinData.new(1, "c:/users/proxylist.txt")

As you are using a Gemfile, you are using bundler. In your script you will need to include rubygems and bundler to make it work. Try this
require 'rubygems'
require 'bundler/setup'
require 'linkedindata'
l = LinkedinData.new(1, "c:/users/proxylist.txt", true, true)
searchTerms = ['First', 'Second', 'Third']
l.getByKeywords(searchTerms)
You have run bundle install, sorry not familiar with gemrat, so not sure how much it does for you.
if you are not actually using bundler, just remove require 'bundler/setup' from the code above, you will still need to require rubygems though in your script.
in looks like the gem itself is kind of broken in the way it declare dependencies, and the other gems that it relies on. I appear to be able to make it work as follows:
Gemfile:
source 'https://rubygems.org'
gem 'linkedin-scraper'
gem 'linkedindata'
gem 'generalscraper'
gem 'uploadconvert'
gem 'docsplit'
gem 'crack'
gem 'pry'
gem 'activesupport'
gem 'selenium-webdriver'
It also appears that it the linkedin-scraper needs to required as linkedin_scraper but not sure why that is. So editing.
{GEMPATH}/linkedindata-0.0.22/lib/linkedindata.rb to require 'linkedin_scraper' as opposed to require 'linkedin-scraper' seems to make it work.
So with the above changes and using bundler
require 'rubygems'
require 'bundler/setup'
require 'linkedindata'
l = LinkedinData.new(1, "proxylist.txt", true, true)
searchTerms = ['First', 'Second', 'Third']
l.getByKeywords(searchTerms)
now runs (I don't have a proxylist.txt so it blows up looking for it, but it doesn't get anymore library errors).

Related

How to fix srb irb update error using gem Roo? or any option how to skip roo from type checking?

I have data pipeline that needs to work with excel xls. I use gem roo for reading xlsx and Push to some API endpoint. I'm starting the project use gem sorbet. Execute srb init and it works. But when I add gem roo, it becomes error.
How can I tell sorbet to skip checking roo gem only? or how can I fix this error?
Here my Gemfile list.
# frozen_string_literal: true
# typed: ignore
source 'https://rubygems.org'
gem 'activesupport', '< 5.1'
gem 'aws-record', '~> 2'
gem 'aws-sdk-s3', '~> 1'
gem 'faraday'
gem 'pry'
gem 'roo', '2.7.1'
gem 'rspec', group: :development
gem 'rubocop', group: :development
gem 'sorbet', group: :development
gem 'sorbet-runtime'
gem 'uuidtools'
And this is the error traceback from sorbet
Generating: sorbet/config
Reusing existing config file: sorbet/config
Generating: sorbet/rbi/sorbet-typed/
Generating: sorbet/rbi/gems/
Traceback (most recent call last):
14: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/bin/srb-rbi:234:in `<main>'
13: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/bin/srb-rbi:196:in `main'
12: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/bin/srb-rbi:118:in `init'
11: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/bin/srb-rbi:229:in `block in make_step'
10: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/lib/gem-generator-tracepoint.rb:38:in `main'
9: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/lib/gem-generator-tracepoint/tracer.rb:70:in `trace'
8: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/lib/gem-generator-tracepoint.rb:39:in `block in main'
7: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/lib/require_everything.rb:19:in `require_everything'
6: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/lib/require_everything.rb:39:in `load_bundler'
5: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/lib/gem_loader.rb:579:in `require_all_gems'
4: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/lib/gem_loader.rb:579:in `each'
3: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/lib/gem_loader.rb:581:in `block in require_all_gems'
2: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/lib/gem_loader.rb:557:in `require_gem'
1: from /Users/ruby/.rvm/gems/ruby-2.5.1/gems/sorbet-0.4.4540/lib/gem_loader.rb:208:in `block in <class:GemLoader>'
/Users/ruby/.rvm/gems/ruby-2.5.1/gems/roo-2.7.1/lib/roo.rb:24:in `const_missing': Excel support has been extracted to roo-xls due to its dependency on the GPL'd spreadsheet gem. Install roo-xls to use Roo::Excel. (RuntimeError)
Here my lambda_function.rb
# frozen_string_literal: true
# typed: true
require 'aws-sdk-s3'
require 'json'
require 'pry'
require 'roo'
def lambda_handler(event:, context:)
response = {
statusCode: 200,
body: {
event: event['filename'],
context: context
}
}
response
end
event = {
event: {
operation: 'echo',
message: 'Hello world!',
filename: './penjualan_per_barang_190612145614.xlsx'
},
context: {}
}
lambda_handler(event)
I expect the srb init would return success. If there any help, it would be great.
This looks like a bug in Sorbet. I've created a fix for it here:
https://github.com/sorbet/sorbet/pull/1454
I'm not sure how you should work around it in the mean time other than not depend on 'roo'.

uninitialized constant ActiveRecord::ConnectionAdapters::ConnectionManagement

Im currently working on a sinatra app, and im having a trouble regarding postgresql connection to sinatra, im try to execute this command:
rake db:create
to create the database but it throws this error.
C:\Users\John\Documents\Registration_Sinatra>rake db:create
rake aborted!
NameError: uninitialized constant ActiveRecord::ConnectionAdapters::ConnectionManagement
C:/Users/John/Documents/Registration_Sinatra/app/app.rb:2:in `<top (required)>'
C:/Users/John/Documents/Registration_Sinatra/Rakefile:1:in `<top (required)>'
LoadError: cannot load such file -- sinatra/activerecord
C:/Users/John/Documents/Registration_Sinatra/app/app.rb:2:in `<top (required)>'
C:/Users/John/Documents/Registration_Sinatra/Rakefile:1:in `<top (required)>'
(See full trace by running task with --trace)
this is my app.rb
require 'sinatra'
require 'sinatra/activerecord'
require 'pg'
require './config/environments'
class RegistrationSinatra < ActiveRecord::Base
end
get '/' do
erb :index
end
this is my environments.rb
configure :development do
#DEFAULT_CONN = {database: 'development_registration_sinatra', user: 'postgres', password: 'secret123', host: 'localhost'}
db = URI.parse(ENV['DATABASE_URL'] || "postgres://#{#DEFAULT_CONN[:host]}/#{#DEFAULT_CONN[:database]}?user=#{#DEFAULT_CONN[:user]}")
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => #DEFAULT_CONN[:host],
:username => #DEFAULT_CONN[:user],
:password => #DEFAULT_CONN[:password],
:database => db.path[1..-1],
:encoding => 'utf8')
end
this is my gemfile
source 'https://rubygems.org'
ruby "2.2.2"
gem 'sinatra'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'tux'
gem 'pg'
and my Rakefile
require './app/app'
require 'sinatra/activerecord/rake'
hope you guys can pin point what's wrong with my sample app so i can progress thanks.
Here is the solution: https://github.com/janko-m/sinatra-activerecord/pull/66
In your Gemfile, add:
gem "activerecord", "< 5.0.0"
run bundle update and it will work.

undefined method `namespace' for main:Object (NoMethodError) - active record / rakefile

I'm attempting to run a basic Sinatra app. When I get to the 'rackup' step I get an error:
/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:3:in `<top (required)>': undefined method `namespace' for main:Object (NoMethodError)
It seems to be a scope issue in the Rake gem. I've had no luck findding an answer and I'm not quite sure what needs to be fixed. I did update all my gems in hopes that would help to no avail. Here is my code that might be contributing....
rakefile.rb
require "./frank"
require "sinatra/activerecord/rake"
config.ru
require_relative 'frank'
map('/welcomes') { run WelcomesController }
frank.rb
require 'sinatra/base'
require 'active_record'
require 'bcrypt'
Dir.glob('./{controllers,models}/*rb').each { |file| require file }
ENV['SINATRA_ENV'] ||= 'development'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => "db/#{ENV['SINATRA_ENV']}.sqlite"
)
spec_helper.rb
ENV['SINATRA_ENV'] = 'test'
require_relative '../frank'
require 'capybara'
require 'database_cleaner'
Capybara.app = Rack::Builder.parse_file(File.expand_path('../../config.ru',__FILE__)).first
RSpec.configure do |config|
config.include Capybara::DSL
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
Many thanks :)
This is below the primary error:
from /Users/stephaniedean/.rvm/gems/ruby-2.2.1/gems/sinatra-activerecord-2.0.6/lib/sinatra/activerecord/rake.rb:1:in `load'
from /Users/stephaniedean/.rvm/gems/ruby-2.2.1/gems/sinatra-activerecord-2.0.6/lib/sinatra/activerecord/rake.rb:1:in `<top (required)>'
So, it looks like sinatra-activerecord not just activerecord. I did try activerecord 3.2.17 that didn't work. Thanks for the suggestions.
this took up hours of my time before I found the solution:
http://aaronlerch.github.io/blog/sinatra-bundler-and-the-global-namespace/
https://github.com/sinatra/sinatra-contrib/issues/111
Gemfile
gem "sinatra", require: 'sinatra/base'
gem 'sinatra-activerecord', require: false
gem 'sinatra-contrib', require: false
Environment.rb
require 'bundler/setup'
require 'rake'
require 'sinatra'
require 'sinatra/reloader'
require 'sinatra/activerecord'
require 'sinatra/activerecord/rake'
make sure to
require 'rake'
before
require 'sinatra/activerecord/rake'

Issues with mysql2 with and ruby

Trying to get the gem mysql2 installed on ubuntu and I have tried all of the suggestions but I cannot get it to run. Here is the error in my application.
./bla.rb:65:in `post_init': undefined method `query' for nil:NilClass (NoMethodError)
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/em/timers.rb:51:in `call'
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/em/timers.rb:51:in `fire'
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `call'
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
from ./bla.rb:234:in `start_server'
from ./bin/minibardaemon:15
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:254:in `call'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:254:in `start_proc'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:263:in `call'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:263:in `start_proc'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:295:in `start'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/controller.rb:73:in `run'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons.rb:197:in `run_proc'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/cmdline.rb:109:in `call'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/cmdline.rb:109:in `catch_exceptions'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons.rb:196:in `run_proc'
I have installed all of the packages that are recommended and installed mysql2 via gem but still no luck.
libmysqlclient-dev
Is installed.
Im on Ubuntu.
# gem -v
1.3.7
# ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]
Installing the gems
# gem install mysql
Building native extensions. This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Installing ri documentation for mysql-2.8.1...
Installing RDoc documentation for mysql-2.8.1...
# gem install mysql2
Building native extensions. This could take a while...
Successfully installed mysql2-0.3.11
1 gem installed
Installing ri documentation for mysql2-0.3.11...
Installing RDoc documentation for mysql2-0.3.11...
def create_mysql2
begin
mysql2 = Mysql2::EM::Client.new(
:username => DBUSER,
:password => DBPASS,
:host => DBHOST,
:port => DBPORT,
:socket => DBSOCKET,
:database => DBNAME
)
return mysql2
rescue Mysql2::Error => exception
$stderr.puts "Mysql Error: #{ exception.message }"
EventMachine::stop_event_loop
end
end
...
begin
mysql2 = create_mysql2
rescue Exception => ex
p ex
end
# query the db every x seconds.
EventMachine::add_periodic_timer(QUERY_INTERVAL) do
defer1 = mysql2.query "SELECT * FROM table LIMIT #{QUERY_LIMIT}"
doesn't realy look like mysql2 gem issue, could you please show 65'th line of you bla.rb file? And probably some number of lines that surround it.
it actually looks like your mysql2 client gets uninitialized for some reason.
I would advice to check if something like this works:
require 'mysql2'
mysql_client = Mysql2::Client.new(:host => "localhost", :username => "root")
mysql_client.query('sql .. ')
if so, there's clearly an issue in your script

Simple use of EM::Synchrony#sync causes 'root fiber' FiberError -- my fault?

This program
require 'em-synchrony' ## v1.0.0
require 'em-hiredis' ## v0.1.0
module EventMachine
module Hiredis
class Client
def self.connect(host = 'localhost', port = 6379)
conn = new(host, port)
EM::Synchrony.sync conn.connect
conn
end
alias :old_method_missing :method_missing
def method_missing(sym, *args)
EM::Synchrony.sync old_method_missing(sym, *args)
end
end
end
end
EventMachine.synchrony do
redis = EM::Hiredis.connect
redis.set('foo', 'bar')
puts redis.get('foo')
EM.stop
end
dies like this
$ ruby /tmp/reddy.rb
/home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-synchrony-1.0.0/lib/em-synchrony.rb:58:in `yield': can't yield from root fiber (FiberError)
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-synchrony-1.0.0/lib/em-synchrony.rb:58:in `sync'
from /tmp/reddy.rb:16:in `method_missing'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/client.rb:119:in `select'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/client.rb:38:in `block in connect'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/event_emitter.rb:8:in `call'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/event_emitter.rb:8:in `block in emit'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/event_emitter.rb:8:in `each'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/event_emitter.rb:8:in `emit'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/connection.rb:15:in `connection_completed'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:179:in `run_machine'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:179:in `run'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-synchrony-1.0.0/lib/em-synchrony.rb:27:in `synchrony'
from /tmp/reddy.rb:22:in `<main>'
I find this deeply confusing. Why doesn't it work and am I at fault? If so, what can I do differently? Unless I've glossed over something, this is kosher, per the em-synchrony README.
I think your code can work if you find the correct version of em-hiredis it is trying to monkey patch, that is one problem with loose dependencies.
Here is a fully working code but based on the master branch of em-synchrony:
Gemfile:
source :rubygems
gem 'em-synchrony', :git => "git://github.com/igrigorik/em-synchrony.git"
gem 'em-hiredis', '~> 0.1.0'
test.rb:
require 'rubygems'
require 'bundler/setup'
require 'em-synchrony'
require 'em-synchrony/em-hiredis'
EventMachine.synchrony do
redis = EM::Hiredis.connect
redis.set('foo', 'bar')
puts redis.get('foo')
EM.stop
end
and then run it with:
$ bundle
$ ruby test.rb
Monkey patching is an inherently flawed way of patching gems unless you ensure the exact version of the gem you patched is used which is something em-synchrony should enforce or at least detect.

Resources