How do I use ActiveRecord Encryption in a Sinatra app? - ruby

I have a Sinatra app which runs ActiveRecord by using the activerecord and sinatra-activerecord gems. I would like to use ActiveRecord encryption, but the ActiveRecord Encryption guide assumes that I have a Rails app and specifies how to add the required encryption keys to the Rails credentials file. As this is not a Rails app, how do I specify an alternate keystore for ActiveRecord to use?
Specifically the error I am seeing is:
ActiveRecord::Encryption::Errors::Configuration:
key_derivation_salt is not configured. Please configure it via credential active_record_encryption.key_derivation_salt or by setting config.active_record.encryption.key_derivation_salt
So I think that it is running correctly, I just don't know how to configure it without the Rails credentials file.

Try this:
ActiveRecord::Encryption.configure(
primary_key: xxx,
deterministic_key: yyy,
key_derivation_salt: zzz
)
Looks undocumented, but found in the source code.

Related

Moodle with Ruby On Rails

Currently, I am working on a rails application which requires the user to be registered automatically in the moodle. I searched for it and found these gems,
moodle-api: https://github.com/getsmarter/moodle-api
moodle_rb: https://github.com/jobready/moodle-rb/
moodle: https://github.com/robertboloc/moodle
But, nothing seems to work for me. I even searched on youtube but found nothing with the rails.
However, using the moodle_rb gem I was able to create an object which returned me the sites info using the sites_info function. Other than that I am unable to use any of the web services of the moodle. I have created an external service and added some functions like auth_email_get_signup_settings and auth_email_signup_user which accepts none parameters.
May anyone guides me through this? or even a minor help would also be great.
I am using a token to create the object.
Thanks in advance.
Can you show us how you tried to configure your gems? For example, according to the docs the moddle-api gem requires a configuration like:
Moodle::Api.configure do|c|
c.host = 'http://my_moodle_instance.com'
c.token = 'mytoken'
end
If you did configure the gem like the above, can you show some of the code that you tried to call once the gem was installed and configured?

Making a Rack CLI

I'm trying to make a framework similar to Rails, but purely focused on GraphQL. Once nice feature of Rails is that it provides a CLI interface and a config.ru for Rack. Therefore, you can call rackup or you can call bin/rails server and the Rails app will run. I managed to mimic this functionality by putting the Rack app into a separate file (config/application.rb), which I import in config.ru and in the CLI, then instantiate and run.
However, I have an issue with Rack middleware. Since Rack middleware appears to just magically work when you run use MyMiddleware with an instantiated Rack app, I'm not really sure how I can do this in both config.ru and in my CLI. Right now it looks like I need to instantiate the app in a separate location, add the middleware, then hand it over to config.ru or the CLI. Which, I could do, but it feels like there has to be a way to attach middleware in a cleaner way. For instance, can I require config.ru in some way and then run it? Or can I attach middleware before I instantiate the app?
config.ru is just a ruby file, it's loaded by Rails as part of running each command. You can require it yourself as normal if that's what you'd like to do.
If you want to really figure out how Rails does it, the config loading is buried in this part of the Rails CLI:
https://github.com/rails/rails/blob/3cac5fe94f0f81b4263cfa03d4822c05a55eb49c/railties/lib/rails/application.rb

Testing a rack app

I have a rack based gem, where the user defines routes, and they are then processed by the gem. I an trying to figure out how to test this setup. Testing methods directly impacted by the creation of routes doesn't work, because they are obviously not defined yet, because no app has been created. Is there a solution to this? I am currently using RSpec, and I would really like it if there is an RSpec solution to this.

NameError: undefined local variable or method `broadcast_messages' for #<RailsStdoutLogging::StdoutLogger:0x007f4d099fb6b0>

Title error when accessing ActiveRecord model only on heroku with rails 5.0.0-beta1. Works on production env heroku. Problem persists anytime Rails.logger is set to STDOUT wether between gem 'rails_stdout_logging' or in config/environment/production.rb
I've reviewed implementation and see broadcast_messages as a method created by the accessor macro. I'm at a loss and am looking for suggestions to troubleshoot.
Turns out heroku's rails_stdout_logging gem which is included as part of heroku's rails_12factor gem has a bug with rails 5.0.0-beta1.
StdoutLogger inherits from stdlib's Logger rather than ActiveSupport's Logger via < ::Logger. I've got a workaround on my fork but it injects an ActiveSupport dependency when not using rails. I'm new to ruby so I don't know best practice to make a more eloquent fix.
See https://github.com/MattWalston/rails_stdout_logging.

How to create heroku based Sinatra apps

I am trying to create Sinatra based heroku app without any luck?
To answer your question: Yes!
For reference: http://docs.heroku.com/rack#sinatra
The sinatra application should be as you always do but on the root of your application you should include a config file named config.ru
It basically says:
require 'application' run
Sinatra::Application
There is a new, slightly different procedure for deploying Sinatra (and other Ruby) apps on Heroku/Cedar which involves "foreman" (and a Procfile)... see getting started guide for Ruby:
http://devcenter.heroku.com/articles/ruby

Resources