-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add very basic (and untested) features
- Autenticate - List active and closed jobs - Get general information about a job
- Loading branch information
Showing
12 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
class ApplicationController < ActionController::API | ||
include ActionController::HttpAuthentication::Token::ControllerMethods | ||
before_action :require_valid_auth_token_and_set_session | ||
|
||
def require_valid_auth_token_and_set_session | ||
authenticate_or_request_with_http_token do |token, options| | ||
@session = Session.where(token: token).first | ||
@session.present? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class JobsController < ApplicationController | ||
def active | ||
render json: @session.active_jobs | ||
end | ||
|
||
def closed | ||
render json: @session.closed_jobs | ||
end | ||
|
||
def show | ||
render json: @session.job(request.fullpath) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class SessionsController < ApplicationController | ||
skip_before_action :require_valid_auth_token_and_set_session | ||
|
||
def create | ||
session = Session.where(email: session_params[:email]).first_or_initialize | ||
session.update(session_params) | ||
if session.valid? | ||
render json: {token: session.token} | ||
else | ||
render json: {errors: session.errors}, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def session_params | ||
params.permit(:email, :password) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
class Session < ApplicationRecord | ||
validates :email, :password, presence: true | ||
validates :email, :token, uniqueness: true | ||
validate :email_and_password_work | ||
|
||
before_save :generate_token, if: :password_changed? | ||
|
||
def generate_token | ||
self.token = SecureRandom.base64(32) | ||
end | ||
|
||
def active_jobs | ||
parse_jobs(get('/dashboard/active.json')) | ||
end | ||
|
||
def closed_jobs | ||
parse_jobs(get('/dashboard/closed.json')) | ||
end | ||
|
||
def job(job_url) | ||
parse_job(get(job_url + '.json')) | ||
end | ||
|
||
private | ||
|
||
def parse_job(gob_job) | ||
iteration = gob_job[:job_iteration] | ||
job = iteration[:job] | ||
iteration.slice( | ||
:active, :url | ||
).merge( | ||
job.slice( | ||
:title, | ||
:min_salary, :max_salary, | ||
:remote, :city, :country, | ||
:category_name, :tags, :perks, :modality, :seniority, | ||
:functions, :functions_headline, | ||
:benefits, :benefits_headline, | ||
:desirable, :desirable_headline, | ||
:unpublished | ||
).merge( | ||
public_url: job[:url], | ||
company_public_url: job[:company_profile], | ||
company_name: job[:company] | ||
) | ||
).merge( | ||
{ | ||
phases: parse_phases(iteration[:phases]) | ||
} | ||
) | ||
end | ||
|
||
|
||
def parse_phases(phases) | ||
phases.sort_by { |phase| phase[:position] }. map do |phase| | ||
phase.slice( | ||
:position, :title, :description, :kind, | ||
:job_applications_count, :job_applications_url | ||
) | ||
end | ||
end | ||
|
||
def parse_jobs(gob_jobs) | ||
gob_jobs[:job_iterations].map do |job_iteration| | ||
{url: job_iteration[:url], title: job_iteration[:job][:title]} | ||
end | ||
|
||
end | ||
|
||
def email_and_password_work | ||
get('/dashboard.json') | ||
rescue RestClient::ExceptionWithResponse => e | ||
errors.add(:password, "Invalid email or password") | ||
Rails.logger.error("Unexpected response: #{e.response}") | ||
end | ||
|
||
def get(path, params = {}) | ||
response = RestClient.get( | ||
"https://www.getonbrd.com" + path, | ||
params.merge(cookies: cookies) | ||
) | ||
JSON.parse(response, symbolize_names: true) | ||
end | ||
|
||
def cookies | ||
@cookies ||= cookies_from_email_and_password | ||
end | ||
|
||
def cookies_from_email_and_password | ||
response = RestClient.get('https://www.getonbrd.com/members/auth/login') | ||
page = Nokogiri::HTML(response.body) | ||
csrf_token = page.css('meta[name=csrf-token]').first['content'] | ||
response = RestClient.post( | ||
'https://www.getonbrd.com/members/auth/login', | ||
{ | ||
authenticity_token: csrf_token, | ||
team_member: { | ||
email: email, | ||
password: password, | ||
} | ||
}, | ||
{cookies: response.cookies} | ||
) | ||
raise RestClient::ExceptionWithResponse.new(response) | ||
rescue RestClient::Found => e | ||
return e.response.cookies | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
Rails.application.routes.draw do | ||
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html | ||
resources :sessions, only: [:create] | ||
resources :jobs, only: [] do | ||
collection do | ||
get 'active' | ||
get 'closed' | ||
end | ||
|
||
member do | ||
get '/processes/:id', to: 'jobs#show' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateSessions < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :sessions do |t| | ||
t.string :email | ||
t.string :password | ||
t.string :token | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This file is auto-generated from the current state of the database. Instead | ||
# of editing this file, please use the migrations feature of Active Record to | ||
# incrementally modify your database, and then regenerate this schema definition. | ||
# | ||
# This file is the source Rails uses to define your schema when running `rails | ||
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to | ||
# be faster and is potentially less error prone than running all of your | ||
# migrations from scratch. Old migrations may fail to apply correctly if those | ||
# migrations use external dependencies or application code. | ||
# | ||
# It's strongly recommended that you check this file into your version control system. | ||
|
||
ActiveRecord::Schema.define(version: 2019_11_03_034455) do | ||
|
||
create_table "sessions", force: :cascade do |t| | ||
t.string "email" | ||
t.string "password" | ||
t.string "token" | ||
t.datetime "created_at", precision: 6, null: false | ||
t.datetime "updated_at", precision: 6, null: false | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe JobsController, type: :controller do | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe SessionsController, type: :controller do | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Session, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |