Spent a lot of my time searching a way of ecrypting string in my app, but didn't find a right solution to use it in Sinatra. For example, I tried to 'require aes' gem (https://github.com/chicks/aes) also gibberish (https://github.com/defunkt/gibberish/blob/master/lib/gibberish.rb) gem but all time I see errors in browser.
I try to encrypt message field in my app:
require 'aes'
get '/auth/signup' do
user = User.new(url: Helpers.random, message: AES.encrypt("A super secret message", 'Here we go!'))
user.save
end
Please, help me how to solve this?!
When using Ruby gems you need to declare them up-front. The easiest way is:
gem 'aes'
require 'aes'
You can also use Bundler and declare them in a Gemfile like:
source 'https://rubygems.org/'
gem 'aes'
Then in your code:
require 'bundler/setup'
require 'aes'
Where Gemfile describes all your dependencies so you can easily reinstall them if necessary.
Once you've created the Gemfile you can do bundle install. If that has issues you'll be alerted. bundle check can verify everything's set up properly.
Related
Can't get Kaminari to work with Sinatra and Mongoid. I'm getting this error:
NoMethodError at /api/events
undefined method `page' for #<Mongoid::Criteria:0x007fccb7828c38>
Here is minimal code to get the error:
Gemfile
source "https://rubygems.org"
gem 'mongoid'
gem 'sinatra'
gem 'kaminari-mongoid'
gem 'kaminari-sinatra'
server.rb
require 'mongoid'
require 'sinatra'
class Event
include Mongoid::Document
end
get '/events' do
Event.desc(:id).page(params[:page]).per(10)
end
I have tried require 'kaminari', require 'kaminari-sinatra', require 'kaminari-mongoid', all to no avail (I get LoadErrors). I've also tried register Kaminari::Helpers::SinatraHelpers as mentioned here, which also failed.
I've followed the instructions in detail, and have scoured Google and StackOverflow to no avail. This answer didn't work. I can't help thinking I'm missing something easy; I'm not a Ruby veteran. My hunch is it's something with Bundler. Any idea?
I ran into the problem as well. Unfortunately, kaminari-mongoid has a rails dependency (you can look in the gemspec file here: https://github.com/kaminari/kaminari-mongoid/blob/master/kaminari-mongoid.gemspec). Therefore, it is not possible to use both kaminari-sinatra and kaminari-mongoid.
This solved my problem. https://github.com/ajsharp/mongoid-pagination. Add it to your Gemfile and install with Bundler.
In your app.rb file, require 'mongoid-pagination'
I noticed for some gems you must include it in the file where you want to use it like this require 'a_gem', but this is not always the case.
I am going to compose a gem by myself. What should I do if I do not want to add the require 'my_gem' to the .rb file when using it?
Usually, an application that is using a gem needs to require the gem:
require "my_awesome_gem"
MyAwesomeGem.do_something_great
However, if an application is using bundler, which defines the application's gem in a file called "Gemfile":
source 'http://rubygems.org'
gem 'my_awesome_gem'
then the application may invoke bundler in a way that automatically requires all of the gems specified in the Gemfile:
require "bundler"
Bundler.require
MyAwesomeGem.do_something_great
Rails projects use Bundler.require, so a Rails application will not need to explicitly require a gem in order to use it: Just add the gem to the Gemfile and go.
For more about Bundler.require, see the bundler documentation
For more about how Rails uses Bundler, see How Does Rails Handle Gems? by Justin Weiss.
This doesn't make sense. If you want to write a Gem and use it, it needs to be required somewhere.
This "somewhere" could be explicit in one of your scripts, it could be written in a Gemfile or it could be required by another script/gem that is required by your script.
If you write a gem, Ruby will not include it automatically in all your scripts.
Finally, if you publish it, should every single Ruby script on earth magically require it and execute your code?
I suppose that the project you have seen which didn't use require 'a_gem' was using a Gemfile.
So, I've created a GitHub to manage my latest Ruby project, and I want for it to utilize a couple of gems. On my PC, all I have to go is type
gem install "gemName"
and it loads it to my computer, and then all I have to do in my Ruby script is have
require "rubygems"
require "gemName"
How can I do this with GitHub? What I tried to do is create a subfolder from the main repository (called "RubyGems") and then in my main ruby script
require "/RubyGems/colorize"
require "/Rubygems/psych"
With the two gems (colorize and psych) in the "RubyGems" folder.
Is this the proper way to do this? Will this even work? What is the right way to do this? (Sorry, I'm kinda new to GitHub.)
A couple of things, unless you're using a really old version of Ruby (like 1.9) you don't need to require 'rubygems' because is already required by default, next I highly recommend you to get familiar with bundler.
Bundler is used for "bundling" the required gems you use, to so do you have to install the gem (gem install bundler) and then you create a Gemfile, like this:
source 'https://rubygems.org'
ruby '2.2.0'
gem 'colorize', git: 'https://github.com/fazibear/colorize.git'
gem 'psych'
Execute bundle install after, that will create Gemfile.lock file, make sure you push both files to your repository.
With that you would be able to bundle exec ./your-script.rb, assuming your script is something like this:
require 'psych'
require 'colorize'
# Here I do stuff with psych and colorize
I'm trying to use this this gem, but even though it shows up with a gem list after I install it, a require 'stopwords-filter' in irb results in LoadError: cannot load such file -- stopwords-filter.
To make sure that I was actually able to install and use gems, I also tried installing this gem and I can require it just fine. Everything works.
What am I missing about stopwords-filter?
Thanks!
Even though the gem is named stopwords-filter, the require statement is just stopwords:
require 'stopwords'
I have a gem called "something".
I would like to add pry as a development dependency when developing the gem. However I don't know how to load it.
If I have "require something" inside lib/something.rb , when I release the gem, it throws a LoadError, because pry is only a development dependency.
At the same time I don't want to keep adding and removing pry when I am committing code.
What is the best way to require pry only when developing the application, but not require it as a dependency for the gem?
You can use the add_development_dependency in the gemspec file. You'll still have to require it in your lib/something.rb file within a begin .. rescue LoadError block. (Edit 2, see below)
In your case, it will be something like the following:
spec.add_development_dependency 'pry', '~> 0.9.12.2'
The purpose of add_development_dependency is to separate the gems into dependencies that get installed when you execute gem install mygem vs development-only dependencies that are installed only when you execute gem install mygem --development.
Edit: #Pierre-Louis Gottfrois' solution
Modify the Gemfile directly and add a test group. This question describes the process. This does not appear to be a preferred solution according to Yehuda Katz.
Edit 2: begin require ... rescue LoadError is apparently a common practice for Ruby scripts, according to this Making Ruby Gems article.
I think I found a workaround for that.
If you configure bundler to use pry as your console with
$ bundle config console pry
Then pry is itself required and you don't need to explicitly require in your source files.
Plus, you get a history on pressing ' ↑ '.