-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix rub el hizb * kalimat search * sync api * include action view rendering for v3
- Loading branch information
1 parent
f77231b
commit 6b45989
Showing
53 changed files
with
2,710 additions
and
215 deletions.
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
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,4 +1,5 @@ | ||
module Api::V3 | ||
class ApiController < ApplicationController | ||
include ActionView::Rendering | ||
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
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
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,63 @@ | ||
module Kalimat | ||
class SearchController < ApplicationController | ||
include ActionController::Renderers::All | ||
use_renderers :json | ||
|
||
before_action :init_kalimat_api_client | ||
before_action :authorize_api_access | ||
|
||
def search | ||
render json: @api.search(search_params) | ||
end | ||
|
||
def suggest | ||
render json: @api.suggest(suggest_params) | ||
end | ||
|
||
protected | ||
def authorize_api_access | ||
qdc_client = qdc_api_client | ||
|
||
if qdc_client.blank? | ||
render_bad_request("Bad request. Api key is missing.") | ||
elsif qdc_client.rate_limited? | ||
render_request_error("too many requests", 429) | ||
else | ||
qdc_client.track_api_call(query: query) if query.present? | ||
end | ||
end | ||
|
||
def init_kalimat_api_client | ||
@api = KalimatApi.new(qdc_api_client) | ||
end | ||
|
||
def qdc_api_client | ||
@api_client ||= ApiClient.where(active: true).find_by(api_key: request.headers['api-key']) | ||
end | ||
|
||
def query | ||
params[:query].strip.presence if params[:query] | ||
end | ||
|
||
def search_params | ||
params.permit( | ||
:query, | ||
:exact_match, | ||
:include_text, | ||
:page, | ||
:per_page | ||
).compact_blank | ||
end | ||
|
||
def suggest_params | ||
params.permit( | ||
:query, | ||
:debug, | ||
:highlight, | ||
:include_text, | ||
:page, | ||
:per_page | ||
).compact_blank | ||
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,40 @@ | ||
# == Schema Information | ||
# Schema version: 20230313013539 | ||
# | ||
# Table name: api_clients | ||
# | ||
# id :bigint not null, primary key | ||
# active :boolean default(TRUE) | ||
# api_key :string not null | ||
# current_period_ends_at :datetime | ||
# current_period_requests_count :integer | ||
# name :string | ||
# requests_count :integer | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_api_clients_on_active (active) | ||
# index_api_clients_on_api_key (api_key) | ||
# | ||
class ApiClient < ApplicationRecord | ||
def self.sync(attrs) | ||
client = ApiClient.where(id: attrs[:id]).first_or_initialize | ||
client.attributes = attrs | ||
|
||
client.save(validate: false) | ||
client | ||
end | ||
|
||
def rate_limited? | ||
return false if internal_api? | ||
|
||
current_period_requests_count.to_i > request_quota.to_i | ||
end | ||
|
||
def track_api_call(query:) | ||
list = Kredis.list("api_client:#{id}-requests") | ||
list.append(Oj.dump({ query: query, timestamp: Time.now.to_i })) | ||
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 @@ | ||
# == Schema Information | ||
# Schema version: 20230313013539 | ||
# | ||
# Table name: api_client_request_stats | ||
# | ||
# id :bigint not null, primary key | ||
# date :date | ||
# requests_count :integer default(0) | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# api_client_id :integer | ||
# | ||
# Indexes | ||
# | ||
# index_api_client_request_stats_on_api_client_id (api_client_id) | ||
# index_api_client_request_stats_on_date (date) | ||
# | ||
class ApiClientRequestStat < ApplicationRecord | ||
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
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
Oops, something went wrong.