Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
Start of logos + securities
Browse files Browse the repository at this point in the history
  • Loading branch information
Shpigford committed Jan 16, 2024
1 parent 622cc94 commit 854b0ad
Show file tree
Hide file tree
Showing 26 changed files with 421 additions and 2 deletions.
17 changes: 17 additions & 0 deletions app/controllers/api/logos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Api::LogosController < ApiController
skip_before_action :authenticate, only: [:security]

Check failure on line 2 in app/controllers/api/logos_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 2 in app/controllers/api/logos_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

def search
kind = params[:kind] || "security"
query = params[:q] || ""
end

def security
symbol = params[:symbol]
security = Security.find_by("lower(symbol) = ?", symbol.downcase)

unless security
render json: { error: 'Not Found' }, status: :not_found

Check failure on line 14 in app/controllers/api/logos_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
end
end
end
65 changes: 65 additions & 0 deletions app/controllers/securities_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class SecuritiesController < ApplicationController
before_action :set_security, only: %i[ show edit update destroy ]

# GET /securities or /securities.json
def index
@securities = Security.limit(10).order(:name)
end

# GET /securities/1 or /securities/1.json
def show
end

Check failure on line 12 in app/controllers/securities_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
# GET /securities/1/edit
def edit
end

# POST /securities or /securities.json
def create
@security = Security.new(security_params)

respond_to do |format|
if @security.save
format.html { redirect_to security_url(@security), notice: "Security was successfully created." }
format.json { render :show, status: :created, location: @security }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @security.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /securities/1 or /securities/1.json
def update
respond_to do |format|
if @security.update(security_params)
format.html { redirect_to security_url(@security), notice: "Security was successfully updated." }
format.json { render :show, status: :ok, location: @security }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @security.errors, status: :unprocessable_entity }
end
end
end

# DELETE /securities/1 or /securities/1.json
def destroy
@security.destroy!

respond_to do |format|
format.html { redirect_to securities_url, notice: "Security was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_security
@security = Security.find(params[:id])
end

# Only allow a list of trusted parameters through.
def security_params
params.require(:security).permit(:logos, :exchange_id, :name, :symbol, :legal_name, :links, :color, :description)
end
end
2 changes: 2 additions & 0 deletions app/helpers/api/logos_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Api::LogosHelper
end
2 changes: 2 additions & 0 deletions app/helpers/securities_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SecuritiesHelper
end
1 change: 1 addition & 0 deletions app/models/exchange.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class Exchange < ApplicationRecord
has_many :transactions, as: :transactable, dependent: :destroy
belongs_to :parent, class_name: "Exchange", foreign_key: "operating_mic_code", primary_key: "mic_code", optional: true
has_many :children, class_name: "Exchange", foreign_key: "operating_mic_code", primary_key: "mic_code"
has_many :securities, dependent: :destroy
end
5 changes: 5 additions & 0 deletions app/models/security.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Security < ApplicationRecord
belongs_to :exchange

has_many_attached :logos
end
4 changes: 4 additions & 0 deletions app/views/api/logos/search.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.meta do
json.credits_used 1
json.credits_remaining @api_key.user.balance
end

Check failure on line 4 in app/views/api/logos/search.json.jbuilder

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingEmptyLines: Final newline missing.
Empty file.
2 changes: 1 addition & 1 deletion app/views/pages/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<h2 class="m-0 mt-3 text-2xl font-medium">Your API Key</h2>
<pre class="my-4 text-base"><code><%= current_user.api_keys.first.key %></code></pre>
<p class="mt-0 text-sm text-gray-500">Remember that your API key is a secret!. If someone gains access to your API key, they could potentially incur charges on your behalf.</p>
<p class="mt-0 text-sm text-gray-500">Remember that your API key is a secret! If someone gains access to your API key, they could potentially incur charges on your behalf.</p>
<!--
<h2 class="m-0 my-4 text-2xl font-medium">Pricing</h2>
<p class="text-sm font-normal text-gray-500 ">Pricing is based on usage. On a base level, we charge $0.000015 per character of content you send in for processing. That's obviously borderline useless for figuring out spending, though. 🙃</p>
Expand Down
57 changes: 57 additions & 0 deletions app/views/securities/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<%= form_with(model: security, class: "contents") do |form| %>
<% if security.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(security.errors.count, "error") %> prohibited this security from being saved:</h2>

<ul>
<% security.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="my-5">
<%= form.label :logos %>
<%= form.file_field :logos, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :exchange_id %>
<%= form.text_field :exchange_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :name %>
<%= form.text_field :name, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :symbol %>
<%= form.text_field :symbol, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :legal_name %>
<%= form.text_field :legal_name, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :links %>
<%= form.text_field :links, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :color %>
<%= form.text_field :color, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :description %>
<%= form.text_area :description, rows: 4, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>
47 changes: 47 additions & 0 deletions app/views/securities/_security.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div id="<%= dom_id security %>">
<p class="my-5">
<strong class="block mb-1 font-medium">Logos:</strong>
<%= link_to security.logos.filename, security.logos if security.logos.attached? %>
</p>

<p class="my-5">
<strong class="block mb-1 font-medium">Exchange:</strong>
<%= security.exchange.name %>
</p>

<p class="my-5">
<strong class="block mb-1 font-medium">Name:</strong>
<%= security.name %>
</p>

<p class="my-5">
<strong class="block mb-1 font-medium">Symbol:</strong>
<%= security.symbol %>
</p>

<p class="my-5">
<strong class="block mb-1 font-medium">Legal name:</strong>
<%= security.legal_name %>
</p>

<p class="my-5">
<strong class="block mb-1 font-medium">Links:</strong>
<%= security.links %>
</p>

<p class="my-5">
<strong class="block mb-1 font-medium">Color:</strong>
<%= security.color %>
</p>

<p class="my-5">
<strong class="block mb-1 font-medium">Description:</strong>
<%= security.description %>
</p>

<% if action_name != "show" %>
<%= link_to "Show this security", security, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Edit this security", edit_security_path(security), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<hr class="mt-6">
<% end %>
</div>
3 changes: 3 additions & 0 deletions app/views/securities/_security.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.extract! security, :id, :logos, :exchange_id, :name, :symbol, :legal_name, :links, :color, :description, :created_at, :updated_at
json.url security_url(security, format: :json)
json.logos url_for(security.logos)
8 changes: 8 additions & 0 deletions app/views/securities/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing security</h1>

<%= render "form", security: @security %>

<%= link_to "Show this security", @security, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to securities", securities_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
13 changes: 13 additions & 0 deletions app/views/securities/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="w-full">
<% if notice.present? %>
<p class="inline-block px-3 py-2 mb-5 font-medium text-green-500 rounded-lg bg-green-50" id="notice"><%= notice %></p>
<% end %>

<div class="flex items-center justify-between">
<h1 class="text-4xl font-bold">Securities</h1>
</div>

<div id="securities" class="min-w-full">
<%= render @securities %>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/securities/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @securities, partial: "securities/security", as: :security
15 changes: 15 additions & 0 deletions app/views/securities/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>

<%= render @security %>

<%= link_to "Edit this security", edit_security_path(@security), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to "Destroy this security", security_path(@security), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
<%= link_to "Back to securities", securities_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/securities/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "securities/security", security: @security
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
namespace :api, path: nil do
resources :exchanges, only: [ :index, :show ]
resource :user, only: [ :show ]
get "logo/search" => "logos#search"
get "logo/security/:symbol" => "logos#security"
end
end

resources :securities

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20240116161107_create_securities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateSecurities < ActiveRecord::Migration[7.2]
def change
create_table :securities, id: :uuid do |t|
t.references :exchange, null: false, foreign_key: true, type: :uuid
t.string :name
t.string :symbol
t.string :legal_name
t.jsonb :links, default: {}
t.string :color
t.text :description

t.timestamps
end
end
end
16 changes: 15 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions lib/tasks/data.rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
namespace :data do
desc "Import NASDAQ symbols"
task import_nasdaq_symbols: :environment do
require "csv"

csv = CSV.parse(File.read(Rails.root.join("storage/data/nasdaq.csv")), headers: true)

csv.each do |row|
exchange = Exchange.find_by(mic_code: "XNAS")
puts "Importing #{row["Symbol"]}"
Security.find_or_create_by(symbol: row["Symbol"], exchange_id: exchange.id).update(
legal_name: row["Name"]
)
end
end

desc "Import exchanges"
task import_exchanges: :environment do
# https://www.iso20022.org/market-identifier-codes
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/api/logos_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class Api::LogosControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
Loading

0 comments on commit 854b0ad

Please sign in to comment.