-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #874
- Loading branch information
Artur Beljajev
committed
Jan 16, 2019
1 parent
9d8479e
commit f6709be
Showing
53 changed files
with
1,607 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Api | ||
module V1 | ||
class AuctionsController < BaseController | ||
before_action :authenticate, except: :index | ||
|
||
def index | ||
render json: Auction.started.map { |auction| serializable_hash(auction) } | ||
end | ||
|
||
def show | ||
auction = Auction.find_by(uuid: params[:uuid]) | ||
render json: serializable_hash(auction) | ||
end | ||
|
||
def update | ||
auction = Auction.find_by(uuid: params[:uuid]) | ||
auction.update!(updatable_params) | ||
|
||
if auction.no_bids? | ||
auction.mark_as_no_bids | ||
elsif auction.payment_received? | ||
auction.mark_as_payment_received | ||
elsif auction.payment_not_received? | ||
auction.mark_as_payment_not_received | ||
end | ||
|
||
render json: serializable_hash_for_update_action(auction) | ||
end | ||
|
||
private | ||
|
||
def updatable_params | ||
return unless Auction.statuses.values.include?(params[:status]) | ||
params.permit(:status) | ||
end | ||
|
||
def serializable_hash(auction) | ||
{ id: auction.uuid, domain: auction.domain, status: auction.status } | ||
end | ||
|
||
def serializable_hash_for_update_action(auction) | ||
hash = serializable_hash(auction) | ||
hash[:registration_code] = auction.registration_code if auction.payment_received? | ||
hash | ||
end | ||
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,18 @@ | ||
require 'rails5_api_controller_backport' | ||
|
||
module Api | ||
module V1 | ||
class BaseController < ActionController::API | ||
private | ||
|
||
def authenticate | ||
ip_allowed = allowed_ips.include?(request.remote_ip) | ||
head :unauthorized unless ip_allowed | ||
end | ||
|
||
def allowed_ips | ||
Rails.configuration.auction.api_allowed_ips.split(',').map(&:strip) | ||
end | ||
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
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,60 @@ | ||
class Auction < ActiveRecord::Base | ||
enum status: { | ||
started: 'started', | ||
awaiting_payment: 'awaiting_payment', | ||
no_bids: 'no_bids', | ||
payment_received: 'payment_received', | ||
payment_not_received: 'payment_not_received', | ||
domain_registered: 'domain_registered' | ||
} | ||
|
||
PENDING_STATUSES = [statuses[:started], | ||
statuses[:awaiting_payment], | ||
statuses[:payment_received]].freeze | ||
private_constant :PENDING_STATUSES | ||
|
||
def self.sell(domain_name) | ||
create!(domain: domain_name.to_s, status: statuses[:started]) | ||
end | ||
|
||
def self.pending(domain_name) | ||
find_by(domain: domain_name.to_s, status: PENDING_STATUSES) | ||
end | ||
|
||
def mark_as_no_bids | ||
DNS::DomainName.new(domain).update_whois | ||
end | ||
|
||
def mark_as_payment_received | ||
self.status = self.class.statuses[:payment_received] | ||
generate_registration_code | ||
save! | ||
end | ||
|
||
def mark_as_payment_not_received | ||
self.status = self.class.statuses[:payment_not_received] | ||
|
||
transaction do | ||
save! | ||
restart | ||
end | ||
end | ||
|
||
def domain_registrable?(registration_code = nil) | ||
payment_received? && registration_code_matches?(registration_code) | ||
end | ||
|
||
private | ||
|
||
def generate_registration_code | ||
self.registration_code = SecureRandom.hex | ||
end | ||
|
||
def restart | ||
self.class.create!(domain: domain, status: self.class.statuses[:started]) | ||
end | ||
|
||
def registration_code_matches?(code) | ||
registration_code == code | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Concerns::Domain::Releasable | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def release_domains | ||
releasable_domains.each do |domain| | ||
domain.release | ||
yield domain if block_given? | ||
end | ||
end | ||
|
||
private | ||
|
||
def releasable_domains | ||
if release_to_auction | ||
where('delete_at < ? AND ? != ALL(coalesce(statuses, array[]::varchar[]))', | ||
Time.zone.now, | ||
DomainStatus::SERVER_DELETE_PROHIBITED) | ||
else | ||
where('delete_at < ? AND ? != ALL(coalesce(statuses, array[]::varchar[])) AND' \ | ||
' ? != ALL(COALESCE(statuses, array[]::varchar[]))', | ||
Time.zone.now, | ||
DomainStatus::SERVER_DELETE_PROHIBITED, | ||
DomainStatus::DELETE_CANDIDATE) | ||
end | ||
end | ||
end | ||
|
||
included do | ||
mattr_accessor :release_to_auction | ||
self.release_to_auction = Rails.configuration.domains.release_to_auction | ||
end | ||
|
||
def release | ||
if release_to_auction | ||
domain_name.sell_at_auction | ||
destroy! | ||
else | ||
discard | ||
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,64 @@ | ||
require 'observer' | ||
|
||
module DNS | ||
# Namespace is needed, because a class with the same name is defined by `domain_name` gem, | ||
# a dependency of `actionmailer`, | ||
class DomainName | ||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
def available? | ||
!unavailable? | ||
end | ||
|
||
def available_with_code?(code) | ||
pending_auction.domain_registrable?(code) | ||
end | ||
|
||
def unavailable? | ||
at_auction? || awaiting_payment? | ||
end | ||
|
||
def unavailability_reason | ||
if at_auction? | ||
:at_auction | ||
elsif awaiting_payment? | ||
:awaiting_payment | ||
end | ||
end | ||
|
||
def sell_at_auction | ||
Auction.sell(self) | ||
update_whois | ||
end | ||
|
||
def at_auction? | ||
pending_auction&.started? | ||
end | ||
|
||
def awaiting_payment? | ||
pending_auction&.awaiting_payment? | ||
end | ||
|
||
def pending_registration? | ||
pending_auction&.payment_received? | ||
end | ||
|
||
def update_whois | ||
Whois::Record.refresh(self) | ||
end | ||
|
||
def to_s | ||
name | ||
end | ||
|
||
private | ||
|
||
attr_reader :name | ||
|
||
def pending_auction | ||
Auction.pending(self) | ||
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
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.