Sha256: 1ac4ee413d1a953db5c28f8f476f340b24986052159fc405c454c158acdeefcd

Contents?: true

Size: 1.37 KB

Versions: 13

Compression:

Stored size: 1.37 KB

Contents

require 'json'
require 'rspec_api_docs/formatter/renderer/json_renderer/name'
require 'rspec_api_docs/formatter/renderer/json_renderer/resource_serializer'

module RspecApiDocs
  module Renderer
    class JSONRenderer
      attr_reader :resources

      def initialize(resources)
        @resources = resources
      end

      def render
        FileUtils.mkdir_p output_file.dirname

        File.open(output_file, 'w') do |f|
          f.write JSON.pretty_generate(output) + "\n"
        end
      end

      private

      def output
        resources.map do |resource|
          recursive_format_hash ResourceSerializer.new(resource).to_h
        end
      end

      def recursive_format_hash(hash)
        case hash
        when Hash
          Hash[
            hash.map do |key, v|
              [
                key.is_a?(Symbol) && key =~ /\A[a-z]/ ? lower_camel_case(key.to_s).to_sym : key,
                recursive_format_hash(v),
              ]
            end
          ]
        when Enumerable
          hash.map { |value| recursive_format_hash(value) }
        else
          hash
        end
      end

      def lower_camel_case(string)
        string = string.split('_').collect(&:capitalize).join
        string[0].downcase + string[1..-1]
      end

      def output_file
        Pathname.new(RspecApiDocs.configuration.output_dir) + 'index.json'
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rspec-api-docs-1.1.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-1.0.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.14.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.13.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.12.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.11.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.10.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.9.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.8.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.7.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.6.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.5.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb
rspec-api-docs-0.4.0 lib/rspec_api_docs/formatter/renderer/json_renderer.rb