Class: Lurker::Service

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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
28
29
# File 'lib/lurker/service.rb', line 13

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)
  else
    Lurker::Json::Schema.new(
      'name' => service_filename,
      'basePath' => '',
      'description' => '',
      'domains' => {},
      'consumes' => %w(application/x-www-form-urlencode application/json),
      'produces' => %w(application/json)
    )
  end
end

Instance Attribute Details

#opened_endpointsObject

Returns the value of attribute opened_endpoints.



6
7
8
# File 'lib/lurker/service.rb', line 6

def opened_endpoints
  @opened_endpoints
end

#schemaObject (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/lurker/service.rb', line 5

def schema
  @schema
end

#service_dirObject (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_serviceObject



9
10
11
# File 'lib/lurker/service.rb', line 9

def self.default_service
  new(Lurker.service_path)
end

Instance Method Details

#base_pathObject



97
98
99
100
101
102
103
104
# File 'lib/lurker/service.rb', line 97

def base_path
  base_path = @schema['basePath']
  if base_path && !base_path.end_with?('/')
    base_path + '/'
  else
    base_path
  end
end

#descriptionObject



106
107
108
# File 'lib/lurker/service.rb', line 106

def description
  @schema['description']
end

#discussionObject



110
111
112
# File 'lib/lurker/service.rb', line 110

def discussion
  @schema['discussion']
end

#domainsObject



114
115
116
# File 'lib/lurker/service.rb', line 114

def domains
  schema['domains']
end

#endpoint_pathsObject



66
67
68
# File 'lib/lurker/service.rb', line 66

def endpoint_paths
  Dir["#{service_dir}/**/*.json"] + Dir["#{service_dir}/**/*.json.yml"] + Dir["#{service_dir}/**/*.json.yml.erb"]
end

#endpointsObject



70
71
72
73
74
# File 'lib/lurker/service.rb', line 70

def endpoints
  @endpoints ||= endpoint_paths.map do |path|
    Lurker::Endpoint.new(path, {}, self)
  end.select(&:indexed?).compact
end

#fix_endpoint_path(path) ⇒ Object



89
90
91
# File 'lib/lurker/service.rb', line 89

def fix_endpoint_path(path)
  path.gsub(/:/, '__')
end

#nameObject



93
94
95
# File 'lib/lurker/service.rb', line 93

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


57
58
59
60
61
62
63
64
# File 'lib/lurker/service.rb', line 57

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



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lurker/service.rb', line 76

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



43
44
45
46
# File 'lib/lurker/service.rb', line 43

def persist!
  Lurker::Json::Writter.write(schema, service_path) unless File.exist?(service_path)
  @opened_endpoints.each { |ep| ep.persist! if ep.respond_to?(:persist!) }
end

#persisted?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/lurker/service.rb', line 31

def persisted?
  File.exist?(service_path)
end

#request_media_typesObject



118
119
120
# File 'lib/lurker/service.rb', line 118

def request_media_types
  schema['consumes']
end

#response_media_typesObject



122
123
124
# File 'lib/lurker/service.rb', line 122

def response_media_types
  schema['produces']
end

#service_filenameObject



39
40
41
# File 'lib/lurker/service.rb', line 39

def service_filename
  @service_filename ||= Pathname.new(Dir["#{service_dir}/*#{SUFFIX}"].first.to_s).basename.to_s.gsub(SUFFIX, '').presence || 'application'
end

#service_pathObject



35
36
37
# File 'lib/lurker/service.rb', line 35

def service_path
  @service_path ||= "#{service_dir}/#{service_filename}#{SUFFIX}"
end

#verify!(verb, path, request_params, extensions, response_status, response_params, successful = true) ⇒ Object



48
49
50
51
52
# File 'lib/lurker/service.rb', line 48

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