Class: Lurker::Service
- Inherits:
-
Object
- Object
- Lurker::Service
- Defined in:
- lib/lurker/service.rb
Overview
Services represent a group of Lurker API endpoints in a directory
Constant Summary collapse
- SUFFIX =
'.service.yml'
- DEFAULT_SCHEMA =
{ name: '', basePath: '', description: '', domains: {}, consumes: %w(application/x-www-form-urlencode application/json), produces: %w(application/json) }
Instance Attribute Summary collapse
- #documentation ⇒ Object
-
#opened_endpoints ⇒ Object
Returns the value of attribute opened_endpoints.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#service_dir ⇒ Object
readonly
Returns the value of attribute service_dir.
Class Method Summary collapse
Instance Method Summary collapse
- #base_path ⇒ Object
- #description ⇒ Object
- #discussion ⇒ Object
- #domains ⇒ Object
- #endpoint_paths ⇒ Object
- #endpoints ⇒ Object
- #fix_endpoint_path(path) ⇒ Object
-
#initialize(service_dir, service_name = nil) ⇒ Service
constructor
A new instance of Service.
- #name ⇒ Object
-
#open(verb, path, path_params = {}) ⇒ Object
Returns an Endpoint described by (verb, path) In scaffold_mode, it will return an EndpointScaffold an of existing file or create an empty EndpointScaffold.
- #path_for(verb, path) ⇒ Object
- #persist! ⇒ Object
- #persisted? ⇒ Boolean
- #request_media_types ⇒ Object
- #response_media_types ⇒ Object
- #service_filename ⇒ Object
- #service_path ⇒ Object
- #verify!(verb, path, request_params, extensions, response_status, response_params, successful = true) ⇒ Object
Constructor Details
#initialize(service_dir, service_name = nil) ⇒ Service
Returns a new instance of Service.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lurker/service.rb', line 22 def initialize(service_dir, service_name = nil) @opened_endpoints = [] @service_dir = File.(service_dir) @service_filename = service_name @schema = if persisted? && (schema = YAML.load_file(service_path)).is_a?(Hash) Lurker::Json::Schema.new(schema, uri: service_path) else Lurker::Json::Schema.new(DEFAULT_SCHEMA.merge(name: service_filename), uri: service_path) end end |
Instance Attribute Details
#documentation ⇒ Object
128 129 130 |
# File 'lib/lurker/service.rb', line 128 def documentation @documentation ||= schema.documentation end |
#opened_endpoints ⇒ Object
Returns the value of attribute opened_endpoints.
7 8 9 |
# File 'lib/lurker/service.rb', line 7 def opened_endpoints @opened_endpoints end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
5 6 7 |
# File 'lib/lurker/service.rb', line 5 def schema @schema end |
#service_dir ⇒ Object (readonly)
Returns the value of attribute service_dir.
5 6 7 |
# File 'lib/lurker/service.rb', line 5 def service_dir @service_dir end |
Class Method Details
.default_service ⇒ Object
18 19 20 |
# File 'lib/lurker/service.rb', line 18 def self.default_service new(Lurker.service_path) end |
Instance Method Details
#base_path ⇒ Object
99 100 101 102 103 104 105 106 |
# 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 ⇒ Object
108 109 110 |
# File 'lib/lurker/service.rb', line 108 def description schema['description'] end |
#discussion ⇒ Object
112 113 114 |
# File 'lib/lurker/service.rb', line 112 def discussion schema['discussion'] end |
#domains ⇒ Object
116 117 118 |
# File 'lib/lurker/service.rb', line 116 def domains schema['domains'] end |
#endpoint_paths ⇒ Object
68 69 70 |
# 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 ⇒ Object
72 73 74 75 76 |
# 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) ⇒ Object
91 92 93 |
# File 'lib/lurker/service.rb', line 91 def fix_endpoint_path(path) path.gsub(/:/, '__') end |
#name ⇒ Object
95 96 97 |
# File 'lib/lurker/service.rb', line 95 def name schema['name'] end |
#open(verb, path, path_params = {}) ⇒ Object
Returns an Endpoint described by (verb, path) In scaffold_mode, it will return an EndpointScaffold an of existing file
or create an empty EndpointScaffold
59 60 61 62 63 64 65 66 |
# 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) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# 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! ⇒ Object
45 46 47 48 |
# 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? ⇒ Boolean
33 34 35 |
# File 'lib/lurker/service.rb', line 33 def persisted? File.exist?(service_path) end |
#request_media_types ⇒ Object
120 121 122 |
# File 'lib/lurker/service.rb', line 120 def request_media_types schema['consumes'] end |
#response_media_types ⇒ Object
124 125 126 |
# File 'lib/lurker/service.rb', line 124 def response_media_types schema['produces'] end |
#service_filename ⇒ Object
41 42 43 |
# 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 ⇒ Object
37 38 39 |
# 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) ⇒ Object
50 51 52 53 54 |
# 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 |