Class: Fdoc::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/fdoc/service.rb

Overview

Services represent a group of Fdoc API endpoints in a directory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_dir, scaffold_mode = Fdoc.scaffold_mode?) ⇒ Service

Returns a new instance of Service.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fdoc/service.rb', line 12

def initialize(service_dir, scaffold_mode = Fdoc.scaffold_mode?)
  @service_dir = File.expand_path(service_dir)
  service_path = Dir["#{@service_dir}/*.fdoc.service"].first
  @schema = if service_path
    YAML.load_file(service_path)
  elsif scaffold_mode
    schema = {
      'name'        => '???',
      'basePath'    => '???',
      'description' => '???'
    }

    Dir.mkdir(service_dir) unless Dir.exist?(service_dir)
    service_path = "#{service_dir}/???.fdoc.service"
    File.open(service_path, "w") { |file| YAML.dump(schema, file) }

    schema
  else
    {}
  end
end

Instance Attribute Details

#meta_serviceObject

Returns the value of attribute meta_service.



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

def meta_service
  @meta_service
end

#service_dirObject (readonly)

Returns the value of attribute service_dir.



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

def service_dir
  @service_dir
end

Class Method Details

.default_serviceObject



8
9
10
# File 'lib/fdoc/service.rb', line 8

def self.default_service
  new(Fdoc.service_path)
end

.verify!(verb, path, request_params, response_params, response_status, successful) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/fdoc/service.rb', line 34

def self.verify!(verb, path, request_params, response_params,
                 response_status, successful)
  service = Fdoc::Service.new(Fdoc.service_path)
  endpoint = service.open(verb, path)
  endpoint.consume_request(request_params, successful)
  endpoint.consume_response(response_params, response_status, successful)
  endpoint.persist! if endpoint.respond_to?(:persist!)
end

Instance Method Details

#base_pathObject



83
84
85
86
87
88
89
90
# File 'lib/fdoc/service.rb', line 83

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

#descriptionObject



92
93
94
# File 'lib/fdoc/service.rb', line 92

def description
  @schema['description']
end

#discussionObject



96
97
98
# File 'lib/fdoc/service.rb', line 96

def discussion
  @schema['discussion']
end

#endpoint_pathsObject



56
57
58
# File 'lib/fdoc/service.rb', line 56

def endpoint_paths
  Dir["#{service_dir}/**/*.fdoc"]
end

#endpointsObject



60
61
62
63
64
# File 'lib/fdoc/service.rb', line 60

def endpoints
  endpoint_paths.map do |path|
    Fdoc::Endpoint.new(path, self)
  end
end

#nameObject



79
80
81
# File 'lib/fdoc/service.rb', line 79

def name
  @schema['name']
end

#open(verb, path, scaffold_mode = Fdoc.scaffold_mode?) ⇒ 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


46
47
48
49
50
51
52
53
54
# File 'lib/fdoc/service.rb', line 46

def open(verb, path, scaffold_mode = Fdoc.scaffold_mode?)
  endpoint_path = path_for(verb, path)

  if scaffold_mode
    Fdoc::EndpointScaffold.new(endpoint_path, self)
  else
    Fdoc::Endpoint.new(endpoint_path, self)
  end
end

#path_for(verb, path) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fdoc/service.rb', line 66

def path_for(verb, path)
  flat_path   = File.join(@service_dir, "#{path}-#{verb.to_s.upcase}.fdoc")
  nested_path = File.join(@service_dir, "#{path}/#{verb.to_s.upcase}.fdoc")

  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