mirror of
https://github.com/internetee/registry.git
synced 2025-08-06 01:35:10 +02:00
Simple implementation of retained domains API endpoint
This needed a new name, there are several classes of database object to be included in that endpoint. Currently, there is one list ordered by name, with each object containing status and ascii name for convenience. Can be converted to multiple fields (reserved and blocked separately). Also contains total count. Includes CORS preflight which seems to be a known problem for Rails in the past.
This commit is contained in:
parent
a2bd9a7cc0
commit
18ce853420
4 changed files with 113 additions and 0 deletions
51
app/models/retained_domains.rb
Normal file
51
app/models/retained_domains.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
# Hiding the queries behind its own class will allow us to include disputed or
|
||||
# auctioned domains without meddling up with controller logic.
|
||||
class RetainedDomains
|
||||
RESERVED = 'reserved'.freeze
|
||||
BLOCKED = 'blocked'.freeze
|
||||
|
||||
attr_reader :domains
|
||||
|
||||
def initialize
|
||||
@domains = gather_domains
|
||||
end
|
||||
|
||||
def gather_domains
|
||||
blocked_domains = BlockedDomain.order(name: :desc).all
|
||||
reserved_domains = ReservedDomain.order(name: :desc).all
|
||||
|
||||
domains = blocked_domains.to_a.union(reserved_domains.to_a)
|
||||
|
||||
domains.sort_by(&:name)
|
||||
end
|
||||
|
||||
def to_jsonable
|
||||
domains.map { |el| domain_to_json(el) }
|
||||
end
|
||||
|
||||
def domain_to_json(domain)
|
||||
# Smelly, but ActiveRecord objects are weird and do not respond
|
||||
# to usual syntax:
|
||||
# case a
|
||||
# when Array then "foo"
|
||||
# when Hash then "bar"
|
||||
# else "baz"
|
||||
# end
|
||||
status = case domain.class.to_s
|
||||
when 'ReservedDomain' then RESERVED
|
||||
when 'BlockedDomain' then BLOCKED
|
||||
end
|
||||
|
||||
punycode = SimpleIDN.to_ascii(domain.name)
|
||||
|
||||
{
|
||||
name: domain.name,
|
||||
status: status,
|
||||
punycode_name: punycode
|
||||
}
|
||||
end
|
||||
|
||||
def count
|
||||
domains.count
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue