How can I modify the 'oauth/token' controller of Gitlab source code? - ruby-on-rails

Recently, I build a self-managed Gitlab instance via gitlab-development-kit.
I used the database migration to added a payload(string) column to the Users table. And I want to add the 'payload' to the response of requests to '/oauth/token'.
I did a full text search for the string '/oauth/token' in Gitlab's source code and it seems like there are only two ruby file including the string:
You can find these two file here: application.rb and routes.rb
I'm new to ruby and ruby on rails. I'm confused about the via: :options since in the ruby on rails documents, the value of the via always be all or post/get
My point is if the via here is options, how does the /oauth/token api receive a POST or GET request?

Related

Is there a way to generate an interactive documentation for Rails APIs?

I mean something like the interactive documentation that generate FastApi, but for a Rails API. I know there is rswag, but to use this I need to write the Rspec files with the description of my endpoints in others files and then generate the json or xml for to be interpreted by rswag-ui or something like that. And what about if a have my tests already all with minitest? Rewrite it for Rspec?
But Is there a short way to do this? Like, write the method name and description on my controller for to be read for an OAS generator? And don't care about update my OAS JSON file every time I change some parameters for my controller, a route or a response because it will be updated automatically. And of course including the UI in the project.
Already exists some magical tool like this? Or a project in initial state trying to do something similar?
i used Apipie that generates swagger file and local documentation as you can see in its demo. You basically write ruby code to generate documentation.

Initializing object within devolopment.rb envirorment using gem within rails mountable engine

Completely lost.
I'm following this sample app tutorial https://www.wepay.com/developer/resources/wefarm-tutorial
which seems like a simple tutorial to follow except I'm building it inside a rails engine. I'm currently attempting to follow the tutorial and initialize the new object.
initialize a new WePay object. add these variables to config/development.rb:
wefarm / config / environments / development.rb
App specific information
CLIENT_ID = 32636
CLIENT_SECRET = "180c800c62"
USE_STAGE = true
WEPAY = WePay.new(CLIENT_ID, CLIENT_SECRET, USE_STAGE)
The issue I think I'm having is the gem is within my core engine allong with user's and the rest of the application and I'm adding these lines of code into the empty shell app. How would I make sure my engine uses this in development I'm also assuming I will come across this issue again when I'm set for production.
In another question a user specified I'm putting this code into the wrong area if I'm using an engine it should be in the initializer folder, but within the documents they just specify putting the code within the config/environments file so where/how exactly do I translate this over to an engine. If it goes into the initializer folder how would I make that file to just include the code specified?
Any help would be amazingly helpful.
Ps. The client Id and secret our just test information
In Rails 4 engine, define the app specific information in secrets.yml
Specifying secrete keys and ids according to environment will solve environment related configuration for keys.
Example :
development:
CLIENT_ID: 123
Make engine configurable
Example in config/initializer/wefarm.rb
Weform.CLIENT_ID = Rails.application.secrets.CLIENT_ID
In lib/engine.rb (engine)
mattr_accessor :CLIENT_ID
Now you can access client_id in engine as WeForm.CLIENT_ID
For more information Rails Engine

Stripe API key missing Rails

We're facing a problem with Stripe API keys. What I've done so far is:
set environment/*.rb to utilise appropriate Stripe keys using constants
created a config/initializers/stripe.rb with the line Stripe.api_key = STRIPE_SECRET
using the rails console, both STRIPE_PUBLIC and STRIPE_SECRET constants are set and visible. STRIPE_PUBLIC => "pk_test_xxxxxxxxx"
However, once these things are in place, making a call to Stripe's API using the browser results in:
Stripe::AuthenticationError in some_controller#some_action
No API key provided. Set your API key using "Stripe.api_key = <API-KEY>". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email support#stripe.com if you have any questions.
Using the web-console gem, we can tell that STRIPE_SECRET and STRIPE_PUBLIC are nil in the website. But every time we run rails console we get our keys from our constants.
I fixed my issue. The terminal running rails server had been opened since before the environment variables had been set.
Therefore, running source ~/.bashrc (for me) or using a new terminal worked for me.
Thank you, sincerely, to everyone who tried to help!
I think the issue you are encountering is happening because if the way you are setting the key.
You cannot expect something you set using the rails console to be available to the web application. The Rails console started up as its own unique process that does not share anything with the web process started and accessed by the browser.
If you want the key to be available to the web application try setting it as an environment variable and reading it accordingly in your stripe.rb file
Try restarting the rails server to make these values available to the rails app since the changes were made in the initializers directory.

automatically generate API for existing database

Is there any way to automatically generate Rails API for existing database? I have mysql DB from php project and I need to generate REST API for it.
using rails 4, you could use scaffold, taking care to keep the data models exactly like the old database (ie. same table and column names), then remove the migrations that are generated with the scaffold.
let's say you have a table called Posts, with columns: subject and body.
You could run:
rails g scaffold post subject:string body:text
Then remove the migration from db/migrate.
Now assuming you set up the rails app to access the database correctly via config/database.yml, you should have a json API all set up and ready to use, since rails scaffold generates index.json.jbuilder and show.json.jbuilder for each resource that you scaffold.
You might have to edit the application controller to allow external API requests, but this should only matter if you plan on POSTing to your API:
if you do need to POST, then change this line in the top of your app/controllers/application_controller.rb:
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
to this:
protect_from_forgery with: :null_session
GET requests should work without modifying the application controller.
Forgot to mention, to access these resources, you will use ?format=json as a parameter, so:
http://localhost:3000/posts?format=json
or
http://localhost:3000/posts/1?format=json
Will return the json response for all posts or a single post.
you can use AutomaticApiRest [automaticapirest.info], This project was created by myself last year. Basically, this tool allows to create an API REST of your DB (MySQL) in seconds.
Features
Creation a powerful API REST of your MySQL Data Base in Seconds.
Management of the API in situ, it is not neccesary an extra data base.
Private tables and fields.
Custom queries.
Installation
Download the source or clone the repo.
git clone https://github.com/GeekyTheory/Automatic-API-REST
Place it in /var/www/YourWebPage/ (for Apache).
Open the file config.php and complete all the fields with the server credencials.
Go to domain.com/AutomaticaApiRest
There is a Node.js alternative in which you can use any SQL database(MySQL, MSSQL, SQL Server, Aurora, PostgreSQL, and SQLite). I'm one of the core committers of that project.
Install the CLI tool(xc-cli) globally from npm using the following command:
npm install -g xc-cli
Then you can create the project simply using following command:
xc new <project_name>
Note : It would prompt for database connection details, just enter the connection details and it would do the rest of the code generation. And you have to install GUI tool to work the code generation(Download it from here: https://github.com/xgenecloud/xc-desktop-app/releases)
For more info refer GitHub repo: https://github.com/xgenecloud/xgenecloud
And for documentation refer: http://docs.xgenecloud.com/

Able to update an API documentation automatically

I'm working on a web site which provides API for external developers.
Whenever the API is updated (i.e. modified parameters, new end points, etc), I have to update the documentation manually.
So I'm looking at how this process can be be done automatically. Here is what I hope to acheive: once my source code is updated and committed to my source control, some sort of an API Registry will be updated. So my documentation can be an app which refers to the registry and shows the viewers with the latest API.
My site is based on Ruby on Rails.
RDoc is automatically generated based on your source code, and it comes default with Rails. Your code needs to be well-documented, but if it is, you can just run
bundle exec rake doc:app
and your app will generate documentation accessible from doc/app/index.html. I would look at that and see if such documentation fits your needs. Links in the "Class and Module Index" sidebar contain lists of all of your methods with expandable views of the source code for them.
I use a custom rake task that places all of that documentation in the Rails public folder for access directly from my website and allows me to add more than one custom page. I imagine it wouldn't be that hard for you to do something similar that only parsed your API files.
Links:
RDoc Syntax Help
Project on Github

Resources