Class: Calamum::DocParser

Inherits:
Object
  • Object
show all
Defined in:
lib/calamum/doc_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ DocParser

Returns a new instance of DocParser.



4
5
6
7
# File 'lib/calamum/doc_parser.rb', line 4

def initialize(definition)
  @resources = Hash.new
  @definition = definition
end

Instance Attribute Details

#definitionObject

Returns the value of attribute definition.



2
3
4
# File 'lib/calamum/doc_parser.rb', line 2

def definition
  @definition
end

#resourcesObject

Returns the value of attribute resources.



2
3
4
# File 'lib/calamum/doc_parser.rb', line 2

def resources
  @resources
end

Instance Method Details

#get_authenticationObject



21
22
23
# File 'lib/calamum/doc_parser.rb', line 21

def get_authentication
  @definition['authentication']
end

#get_descriptionObject



25
26
27
# File 'lib/calamum/doc_parser.rb', line 25

def get_description
  @definition['description']
end

#get_errorsObject



29
30
31
# File 'lib/calamum/doc_parser.rb', line 29

def get_errors
  @definition['errors']
end

#get_nameObject



13
14
15
# File 'lib/calamum/doc_parser.rb', line 13

def get_name
  @definition['name']
end

#get_resourcesObject



33
34
35
36
# File 'lib/calamum/doc_parser.rb', line 33

def get_resources
  resources = @definition['resources'].kind_of?(String) ? get_seperate_resources : @definition['resources']
  Calamum::Config[:sort]? resources.sort : resources
end

#get_seperate_resourcesObject



38
39
40
41
42
43
44
45
46
# File 'lib/calamum/doc_parser.rb', line 38

def get_seperate_resources
  extension = File.extname(Calamum::Config[:source])
  path = File.expand_path("#{@definition['resources']}", File.dirname(Calamum::Config[:source]))
  case extension
  when '.json' then Dir["#{path}/*#{extension}"].map { |f| Yajl.load File.read(f) }.flatten.reduce({}, :merge)
  when '.yml' then Dir["#{path}/*#{extension}"].map { |f| YAML.load File.read(f) }.flatten.reduce({}, :merge)
  else raise 'unknown source file extension'
  end
end

#get_urlObject



9
10
11
# File 'lib/calamum/doc_parser.rb', line 9

def get_url
  @definition['url']
end

#get_versionObject



17
18
19
# File 'lib/calamum/doc_parser.rb', line 17

def get_version
  @definition['version']
end

#load_resourcesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/calamum/doc_parser.rb', line 48

def load_resources
  get_resources.each do |name, methods|
    list = []
    methods.each do |resource|
      if validate_resource?(resource)
        list << Calamum::Resource.new(resource)
        puts_info "#{resource['action']}: #{resource['uri']}"
      else
        puts_warning "Resource #{resource['action']}: #{resource['uri']} has incorrect definition"
      end
    end
    @resources[name] = list if list.any?
  end
  @resources
end

#validate_resource?(resource) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/calamum/doc_parser.rb', line 64

def validate_resource?(resource)
  resource['uri'] && resource['action'] && resource['description'] && %{GET POST PUT DELETE}.include?(resource['action'].upcase)
end