Module: RailsToPostman

Defined in:
lib/rails_to_postman.rb,
lib/rails_to_postman/main.rb,
lib/rails_to_postman/railtie.rb,
lib/rails_to_postman/version.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.export_routesObject



3
4
5
6
7
8
# File 'lib/rails_to_postman/main.rb', line 3

def self.export_routes
  load Rails.application.root.join('config/routes.rb') 
  resources = get_routes
  doc = render_postman_json_from(resources)
  save_to_file(doc)
end

.get_routesObject



22
23
24
25
26
27
28
29
30
# File 'lib/rails_to_postman/main.rb', line 22

def self.get_routes
  @_route_counter = 0
  routes = []
  Rails.application.routes.routes.each do |route|
    next if route.app.is_a?(ActionDispatch::Routing::Mapper::Constraints)
    routes << normalise_route(route)[0] 
  end
  grouped_routes(routes.compact)
end

.grouped_routes(routes) ⇒ Object



32
33
34
# File 'lib/rails_to_postman/main.rb', line 32

def self.grouped_routes(routes)
  routes.group_by { |r| r[:reqs][:controller] }
end

.normalise_route(route, path_prefix = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rails_to_postman/main.rb', line 10

def self.normalise_route(route, path_prefix = nil)
  route.verb.source.split('|').map do |verb|
    {
       :id   => @_route_counter+=1,
       :name => route.name,
       :verb => verb.gsub(/[$^]/, ''),
       :path => path_prefix.to_s + route.path.spec.to_s.sub('(.:format)', ''),
       :reqs => route.requirements
    }
  end
end

.render_postman_json_from(resources) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rails_to_postman/main.rb', line 36

def self.render_postman_json_from(resources)
  collection_id = Rails.application.class.parent.to_s.downcase
  collection_name = Rails.application.class.parent.to_s 
  


  postman_json = {
    id: collection_id,
    name: collection_name,
    timestamp: '1360664941208',
    order: [],
    requests: []
  }
  
  resources.each do |resource, routes|
    postman_json[:order] << "resource##{resource}"
    postman_json[:requests] << {
      collectionId: collection_id,
      id: "resource##{resource}",
      name: resource.upcase,
      description: "#{resource.capitalize} resources",
      url: "/#{resource}",
      method: "RESOURCES",
      headers: "",
      data: [],
      dataMode: "params",
      timestamp: 0,
      responses: [],
      version: 2
    }
    
    routes.each do |route|
      postman_json[:order] << "#{resource}##{route[:reqs][:action]}"
      postman_json[:requests] << {
        collectionId: collection_id,
        id: "#{resource}##{route[:reqs][:action]}",
        name: "#{route[:reqs][:action]}",
        description: "#{resource.capitalize} '#{route[:reqs][:action]}' method",
        url: "{{url}}#{route[:path]}?token={{token}}",
        method: route[:verb],
        headers: "",
        data: [],
        dataMode: "params",
        timestamp: 0,
        responses: [],
        version: 2
      }
    end
  end 
  postman_json
end

.save_to_file(doc) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/rails_to_postman/main.rb', line 88

def self.save_to_file(doc)
  file_path = "tmp/#{Rails.application.class.parent.to_s.downcase}_routes_for_postman.json"
  output_file = File.open( file_path, 'w') 
  output_file << doc.to_json
  output_file.close
  puts "Rails to Postman - routes exported to: '#{file_path}'" 
end