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'
Instance Attribute Summary collapse
-
#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
- #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.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/lurker/service.rb', line 13 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::Schema.new(schema) else Lurker::Schema.new({ 'name' => service_filename, 'basePath' => '', 'description' => '', 'domains' => {} }) end end |
Instance Attribute Details
#opened_endpoints ⇒ Object
Returns the value of attribute opened_endpoints.
6 7 8 |
# File 'lib/lurker/service.rb', line 6 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
9 10 11 |
# File 'lib/lurker/service.rb', line 9 def self.default_service new(Lurker.service_path) end |
Instance Method Details
#base_path ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/lurker/service.rb', line 100 def base_path base_path = @schema['basePath'] if base_path && !base_path.end_with?('/') base_path + '/' else base_path end end |
#description ⇒ Object
109 110 111 |
# File 'lib/lurker/service.rb', line 109 def description @schema['description'] end |
#discussion ⇒ Object
113 114 115 |
# File 'lib/lurker/service.rb', line 113 def discussion @schema['discussion'] end |
#domains ⇒ Object
117 118 119 |
# File 'lib/lurker/service.rb', line 117 def domains schema['domains'] end |
#endpoint_paths ⇒ Object
69 70 71 |
# File 'lib/lurker/service.rb', line 69 def endpoint_paths Dir["#{service_dir}/**/*.json"] + Dir["#{service_dir}/**/*.json.yml"] + Dir["#{service_dir}/**/*.json.yml.erb"] end |
#endpoints ⇒ Object
73 74 75 76 77 |
# File 'lib/lurker/service.rb', line 73 def endpoints @endpoints ||= endpoint_paths.map do |path| Lurker::Endpoint.new(path, {}, self) end.select(&:indexed?).compact end |
#fix_endpoint_path(path) ⇒ Object
92 93 94 |
# File 'lib/lurker/service.rb', line 92 def fix_endpoint_path(path) path.gsub(/:/, '__') end |
#name ⇒ Object
96 97 98 |
# File 'lib/lurker/service.rb', line 96 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
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lurker/service.rb', line 56 def open(verb, path, path_params={}) endpoint_path = path_for(verb, path) endpoint_fname = Dir["#{endpoint_path}*"].first endpoint = if endpoint_fname.present? Lurker::Endpoint.new(endpoint_fname, path_params, self) else Lurker::EndpointScaffold.new(endpoint_path, path_params, self) end @opened_endpoints << endpoint endpoint end |
#path_for(verb, path) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/lurker/service.rb', line 79 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
41 42 43 44 |
# File 'lib/lurker/service.rb', line 41 def persist! schema.write_to(service_path) unless File.exists?(service_path) @opened_endpoints.each { |e| e.persist! if e.respond_to? :persist! } end |
#persisted? ⇒ Boolean
29 30 31 |
# File 'lib/lurker/service.rb', line 29 def persisted? File.exist?(service_path) end |
#service_filename ⇒ Object
37 38 39 |
# File 'lib/lurker/service.rb', line 37 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
33 34 35 |
# File 'lib/lurker/service.rb', line 33 def service_path @service_path ||= "#{service_dir}/#{service_filename}#{SUFFIX}" end |
#verify!(verb, path, request_params, extensions, response_status, response_params, successful = true) ⇒ Object
46 47 48 49 50 |
# File 'lib/lurker/service.rb', line 46 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 |