class WestfieldSwagger::ApiSpecification
Attributes
env_version[R]
request[R]
version[R]
Public Class Methods
new(version, context, request, env_prefix = "")
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 8 def initialize(version, context, request, env_prefix = "") @version = version @context = context @request = request @env_version = "#{env_prefix}#{version}" end
Public Instance Methods
read()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 17 def read read_json || read_directory || read_yaml || swagger_specifications_not_found end
swagger_api_host()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 21 def swagger_api_host return ENV['API_HOST_URL'] if ENV['API_HOST_URL'].present? # Do not specify the port if it's standard HTTP/HTTPS return request.host if [80, 443].include?(request.port) request.host_with_port end
Private Instance Methods
add_info_block(doc)
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 52 def add_info_block(doc) doc['info']['x-last_modified_at'] = last_modified_at doc['info']['license'] = { name: 'Apache 2 License', url: 'http://www.apache.org/licenses/LICENSE-2.0' } doc['info']['contact'] = { name: 'Westfield Support', url: 'http://www.westfieldstatus.com', email: 'help@westfieldsupport.com' } end
combined_swagger_files()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 115 def combined_swagger_files data = individual_swagger_files.map do |filepath| read_from_file(filepath) end unsorted_data = data.inject({}) { |hash, item| hash.deep_merge(item) } deeply_sort_hash(unsorted_data) end
convert_to_json(swagger_document:)
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 61 def convert_to_json(swagger_document:) swagger_document['host'] ||= swagger_api_host add_info_block(swagger_document) JSON.pretty_generate(swagger_document) rescue StandardError => e message = "Unable to parse API specification for version '#{version}'" if Rails.env.development? || Rails.env.test? raise $!, "#{self.class.parent_name}: #{message} Error: #{e}", $!.backtrace end fail SpecificationParseError, message end
directory_path(wildcard: '')
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 107 def directory_path(wildcard: '') Rails.root.join('lib', 'swagger', version, wildcard) end
file_path(extension:)
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 99 def file_path(extension:) Rails.root.join('lib', 'swagger', "#{env_version}.#{extension}") end
individual_swagger_files()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 81 def individual_swagger_files Dir.glob(directory_path(wildcard: '**/*.yml')) end
last_modified_at()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 38 def last_modified_at return most_recent_file_modification_date.utc.iso8601 if swagger_directory_exists? path_to_swagger_file = nil if swagger_file_exists?(extension: 'json') path_to_swagger_file = file_path(extension: 'json') elsif swagger_file_exists?(extension: 'yml') path_to_swagger_file = file_path(extension: 'yml') end File.mtime(path_to_swagger_file).utc.iso8601 if path_to_swagger_file end
most_recent_file_modification_date()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 34 def most_recent_file_modification_date individual_swagger_files.map {|file| File.mtime(file) }.sort.last end
read_directory()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 85 def read_directory convert_to_json(swagger_document: combined_swagger_files) if swagger_directory_exists? end
read_from_file(filepath)
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 111 def read_from_file(filepath) YAML.load(ERB.new(File.read(filepath)).result(@context)) end
read_json()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 77 def read_json File.read(file_path(extension: 'json')) if swagger_file_exists?(extension: 'json') end
read_yaml()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 89 def read_yaml if swagger_file_exists?(extension: 'yml') convert_to_json(swagger_document: read_from_file(file_path(extension: 'yml'))) end end
swagger_directory_exists?()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 103 def swagger_directory_exists? File.directory?(directory_path) end
swagger_file_exists?(extension:)
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 95 def swagger_file_exists?(extension:) File.file?(file_path(extension: extension)) end
swagger_specifications_not_found()
click to toggle source
# File lib/westfield_swagger/api_specification.rb, line 73 def swagger_specifications_not_found fail SpecificationMissing, "API Specification for version '#{version}' does not exist." end