Class: Naptime::REST

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

Constant Summary collapse

@@crud =
{
  'Create' => {
    'http' => 'POST',
    'return' => 'id',
    'requires' => false,
    'body' => true
  },
  'Read' => {
    'http' => 'GET',
    'return' => false,
    'requires' => 'id',
    'body' => false
  },
  'Update' => {
    'http' => 'POST',
    'return' => false,
    'requires' => 'id',
    'body' => true
  },
  'Delete' => {
    'http' => 'DELETE',
    'return' => false,
    'requires' => 'id',
    'body' => false
  }
}

Instance Method Summary collapse

Constructor Details

#initialize(fileName) ⇒ REST

Returns a new instance of REST.



34
35
36
37
38
39
40
41
42
43
# File 'lib/naptime.rb', line 34

def initialize(fileName)

  @rest = Hash.new
  @restPath = Hash.new

  file = File.open(fileName, "rb")

  self.traverse(File.basename(fileName,".*"), JSON.parse(file.read), '/')

end

Instance Method Details

#outputObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/naptime.rb', line 61

def output

  @rest.each do |noun, payload|

    @@crud.each do |action, info|
      path = @restPath[noun]['path'] + noun.to_s
      id = info['requires'] == 'id' ? '/[id]' : ''

      apiUri = "#{info['http']} #{path}#{id}\n"
      apiPayload = payload.to_json + "\n" if info['body']

      print apiUri
      print apiPayload
      print "\n"

    end
  end
end

#traverse(noun, desc, path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/naptime.rb', line 45

def traverse(noun, desc, path)

  @rest[noun.to_sym] = Hash.new
  @restPath[noun.to_sym] = Hash.new
  @restPath[noun.to_sym]['path'] = path

  desc.each do |x,y|
    if y.respond_to?('each')
      self.traverse(x, y, path + noun + '/[id]/')
    else
      @rest[noun.to_sym][x] ||= y
    end
  end

end