class Lurker::Service

Services represent a group of Lurker API endpoints in a directory

Constants

DEFAULT_SCHEMA
SUFFIX

Attributes

documentation[W]
opened_endpoints[RW]
schema[R]
service_dir[R]

Public Class Methods

default_service() click to toggle source
# File lib/lurker/service.rb, line 18
def self.default_service
  new(Lurker.service_path)
end
new(service_dir, service_name = nil) click to toggle source
# File lib/lurker/service.rb, line 22
def initialize(service_dir, service_name = nil)
  @opened_endpoints = []
  @service_dir = File.expand_path(service_dir)
  @service_filename = service_name
  @schema = if persisted? && (schema = YAML.load_file(service_path)).is_a?(Hash)
    Lurker::Json::Schema.new(schema.stringify_keys, uri: service_path)
  else
    Lurker::Json::Schema.new(DEFAULT_SCHEMA.merge('name' => service_filename), uri: service_path)
  end
end

Public Instance Methods

base_path() click to toggle source
# File lib/lurker/service.rb, line 99
def base_path
  base_path = @schema['basePath']
  if base_path && !base_path.end_with?('/')
    base_path + '/'
  else
    base_path || '/'
  end
end
description() click to toggle source
# File lib/lurker/service.rb, line 108
def description
  schema['description']
end
discussion() click to toggle source
# File lib/lurker/service.rb, line 112
def discussion
  schema['discussion']
end
documentation() click to toggle source
# File lib/lurker/service.rb, line 128
def documentation
  @documentation ||= schema.documentation
end
domains() click to toggle source
# File lib/lurker/service.rb, line 116
def domains
  schema['domains']
end
endpoint_paths() click to toggle source
# File lib/lurker/service.rb, line 68
def endpoint_paths
  Dir["#{service_dir}/**/*.json"] + Dir["#{service_dir}/**/*.json.yml"] + Dir["#{service_dir}/**/*.json.yml.erb"]
end
endpoints() click to toggle source
# File lib/lurker/service.rb, line 72
def endpoints
  @endpoints ||= endpoint_paths.map do |path|
    Lurker::Endpoint.new(path, {}, self)
  end.select(&:indexed?).compact
end
fix_endpoint_path(path) click to toggle source
# File lib/lurker/service.rb, line 91
def fix_endpoint_path(path)
  path.gsub(/:/, '__')
end
name() click to toggle source
# File lib/lurker/service.rb, line 95
def name
  schema['name']
end
open(verb, path, path_params={}) click to toggle source

Returns an Endpoint described by (verb, path) In scaffold_mode, it will return an EndpointScaffold an of existing file

or create an empty EndpointScaffold
# File lib/lurker/service.rb, line 59
def open(verb, path, path_params={})
  endpoint_path = path_for(verb, path)
  endpoint_fname = Dir["#{endpoint_path}*"].first

  Lurker::Endpoint.new(endpoint_fname || endpoint_path, path_params, self).tap do |ep|
    @opened_endpoints << ep
  end
end
path_for(verb, path) click to toggle source
# File lib/lurker/service.rb, line 78
def path_for(verb, path)
  flat_path   = fix_endpoint_path(File.join(@service_dir, "#{path}-#{verb.to_s.upcase}.json.yml"))
  nested_path = fix_endpoint_path(File.join(@service_dir, "#{path}/#{verb.to_s.upcase}.json.yml"))

  if File.exist?(flat_path)
    flat_path
  elsif File.exist?(nested_path)
    nested_path
  else # neither exists, default to flat_path
    flat_path
  end
end
persist!() click to toggle source
# File lib/lurker/service.rb, line 45
def persist!
  Lurker::Json::Writer.write(schema, service_path) unless File.exist?(service_path)
  @opened_endpoints.each { |ep| ep.persist! if ep.respond_to?(:persist!) }
end
persisted?() click to toggle source
# File lib/lurker/service.rb, line 33
def persisted?
  File.exist?(service_path)
end
request_media_types() click to toggle source
# File lib/lurker/service.rb, line 120
def request_media_types
  schema['consumes']
end
response_media_types() click to toggle source
# File lib/lurker/service.rb, line 124
def response_media_types
  schema['produces']
end
service_filename() click to toggle source
# File lib/lurker/service.rb, line 41
def service_filename
  @service_filename ||= Pathname.new(Dir["#{service_dir}/*#{SUFFIX}"].first.to_s).basename.to_s.gsub(SUFFIX, '').presence || 'application'
end
service_path() click to toggle source
# File lib/lurker/service.rb, line 37
def service_path
  @service_path ||= "#{service_dir}/#{service_filename}#{SUFFIX}"
end
verify!(verb, path, request_params, extensions, response_status, response_params, successful=true) click to toggle source
# File lib/lurker/service.rb, line 50
def verify!(verb, path, request_params,
            extensions, response_status, response_params, successful=true)
  endpoint = open(verb, path, extensions)
  endpoint.consume!(request_params, response_params, response_status, successful)
end