Rake in Ruby: undefined method `namespace' for main:Object (NoMethodError) - ruby

I'm currently trying to run a Rake task in a Ruby project (No Rails). What I am trying to accomplish is to run a method from a file within my Ruby project. However I get the following error:
undefined method `namespace' for main:Object (NoMethodError)
I created a folder task that holds a test.rb file. Before I had it as test.rake but I think this was incorrect. I also created a Rakefile pointing to task/test.rb
For redeability, I'm using namespace: although I'll be honest I'm not sure if I even need it.
#Rakefile
task :default => [:test]
task :test do
ruby 'task/test.rb'
end
Here is the task.test.rb
require './src/lambda_function.rb'
class KMS
def initialize
end
def decrypt(key)
return "some password"
end
end
class SNS
def initialize
end
end
TEST_FORM_ID=123
namespace :test do
namespace :lambda do
desc 'Run the Lambda process function'
task :process do
LambdaFunctions::LambdaHandler.process(box_api: BoxApi.new,
form: TEST_FORM_ID,
sns: SNS.new,
kms: KMS.new)
end
end
end
What I'm I doing wrong?

Related

How to reference a function in a different rake file

I want to call a function that is in another rake file.
Rake File 1:
task :build => [:some_other_tasks] do
foo
end
def foo(type = :debug)
# ...
end
Rake File 2:
require_relative 'path_to_rake_file_1'
task :foo2 => [:some_other_tasks] do
foo
end
I am currently getting a no such file to load error despite confirming the path is absolutely correct.
Instead of defining methods inside rake files and sharing them among rake tasks, it is best practice to create a RakeHelper module and include it in your rake file. So, you could have something like:
rake_helper.rb
module RakeHelper
def self.foo
end
end
task1.rake
include RakeHelper
task :build => [:some_other_tasks] do
RakeHelper.foo
end
task2.rake
include RakeHelper
task :foo2 => [:some_other_tasks] do
RakeHelper.foo
end

Rake: ArgumentError: unknown keywords: when passing method parameters

I'm working on a pure Ruby application where I'm trying to create a Rake task. I have a method in the file src/lambda_function.rb that is as follows:
def self.process(event:, context: nil, box_api: BoxApi.new, form: nil, sns: SNS.new, kms: KMS.new)
begin
# verify request came from fromstack from headers
verify_webhook_req(event)
# parse data
submission = JSON.parse(event["body"])
form_id = submission.fetch("FormID").strip()
submission_id = submission.fetch("UniqueID").strip()
As you can see from the above snippet the function takes in the following parameters:
event:, context:, box_api:, form:, sns:, kms: So in the rake task I pass the following:
require './src/lambda_function.rb'
require 'rake'
require 'pry'
include Rake::DSL
class KMS
def initialize
end
def decrypt(key)
return 'some password'
end
end
class SNS
def initialize
end
end
namespace :test do
namespace :lambda do
desc 'Run the Lambda process function'
task :process do
TEST_FORM_ID=3353951
LambdaFunctions::LambdaHandler.process(box_api: BoxApi.new,
form: TEST_FORM_ID,
sns: SNS.new,
kms: KMS.new)
end
end
end
But calling this rake task throws an error:
rake aborted!
ArgumentError: unknown keywords: box_api, form
How come it doesn't recognize form and box_api. At first, I thought that maybe I was missing a hash to pass in the arguments. {box_api: BoxApi.new, form: ....}` this didn't work either.
Why is throwing the error?
I was calling a method in a different class which had different parameters.
class WebhookHandler
def self.process(event:, context: nil, box_api: BoxApi.new, form: nil, sns: SNS.new, kms: KMS.new)
begin
# verify request came from fromstack from headers
verify_webhook_req(event)

Can't call module method

I'm writing a Chef cookbook to deploy and application and create users. It doesn't have an API, and uses an odd hashing method, so I've written a short library module for it. I've included only the makeSalt() method below for the sake of brevity.
module Foo_packagist
module Password
def makeSalt(len=31)
require 'securerandom'
return Digest.hexencode(SecureRandom.random_bytes((len*6/8.0).ceil)).to_i(16).to_s(36)[0..len-1]
end
end
end
The trouble is that in every Chef run I get:
NoMethodError
-------------
undefined method `makeSalt' for Foo_packagist::Password:Module
and debugging in chef-shell I get:
chef (12.4.0)> puts ::Foo_packagist::Password.instance_methods()
makeSalt
encodePassword
chef (12.4.0)> puts ::Foo_packagist::Password.makeSalt()
NoMethodError: undefined method `makeSalt' for Foo_packagist::Password:Module
chef (12.4.0)> puts ::Foo_packagist::Password::makeSalt()
NoMethodError: undefined method `makeSalt' for Foo_packagist::Password:Module
What is the right way to call this method?
Change that to def self.makeSalt. That's the Ruby syntax for a module-level method.
Try this ->
module Foo_packagist
module Password
def self.makeSalt(len=31)
require 'securerandom'
return Digest.hexencode(SecureRandom.random_bytes((len*6/8.0).ceil)).to_i(16).to_s(36)[0..len-1]
end
end
end
Then to call it would be this->
Foo_packagist::Password.makeSalt()

Why do I get the error uninitialized constant Stuff::HTTParty?

I have the HTTParty gem on my system and I can use it from within rails.
Now I want to use it standalone.
I am trying:
class Stuff
include HTTParty
def self.y
HTTParty.get('http://www.google.com')
end
end
Stuff.y
but I get
$ ruby test_httparty.rb
test_httparty.rb:2:in `<class:Stuff>': uninitialized constant Stuff::HTTParty (NameError)
from test_httparty.rb:1:in `<main>'
07:46:52 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker 73845718_get_method
$
You have to require 'httparty':
require 'httparty'
class Stuff
include HTTParty
# ...
end
Its all because of the include which exists with in the class
If you include a class with a module, that means you're "bringing in" the module's methods as instance methods.
If you need more clarity on include and require
I request you to refer to this wonderful SO Posting
What is the difference between include and require in Ruby?
Here is an example which I have taken from the same posting
module A
def say
puts "this is module A"
end
end
class B
include A
end
class C
extend A
end
B.say => undefined method 'say' for B:Class
B.new.say => this is module A
C.say => this is module A
C.new.say => undefined method 'say' for C:Class

How to access "global" (constant?) capistrano variables from classes? (ruby)

So my deploy.rb script in capistrano starts like this, which I guess is pretty normal:
require 'capistrano/ext/multistage'
require 'nokogiri'
require 'curb'
require 'json'
# override capistrano defaults
set :use_sudo, false
set :normalize_asset_timestamps, false
# some constant of mine
set :my_constant, "foo_bar"
Later, I can access my constant in functions or tasks within namespaces, like:
namespace :mycompany do
def some_function()
run "some_command #{my_constant}"
end
desc <<-DESC
some task description
DESC
task :some_task do
run "some_command #{my_constant}"
end
end
However, if I use the constant in a class, like this:
namespace :mycompany do
class SomeClass
def self.some_static_method()
run "some_command #{my_constant}"
end
end
end
It fails with:
/config/deploy.rb:120:in `some_static_method': undefined local variable or method `my_constant' for #<Class:0x000000026234f8>::SomeClass (NameError)
What am I doing wrong??
Thanks
The deploy.rb file is instance_evaled, this means it's being executed inside the context of an object, and as such anything you declare will be available until you leave that context. As soon as you create a class that provides a new context.
In order to access the original context you have to pass the object (self) to the class method.
namespace :mycompany do
class SomeClass
def self.some_static_method(cap)
run "some_command #{cap.fetch(:my_constant)}"
end
end
SomeClass.some_static_method(self)
end
Although I really don't understand why you are declaring a class like this, it's an odd place for it.

Resources