Sha256: 1abebe2839a239795ee0019cfd690d368c3734e346bd1ec33486db3bf92ea663

Contents?: true

Size: 1.78 KB

Versions: 5

Compression:

Stored size: 1.78 KB

Contents

# Copyright (C) 2013  Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

require "json"

module ChupaText
  module Formatters
    class JSON
      def initialize(output)
        @output = output
        @formatted = {}
      end

      def format_start(data)
        format_headers(data, @formatted)
        @formatted["texts"] = []
      end

      def format_extracted(data)
        text = {}
        format_headers(data, text)
        text["body"] = data.body
        @formatted["texts"] << text
      end

      def format_finish(data)
        @output << ::JSON.pretty_generate(@formatted)
        @output << "\n"
      end

      private
      def format_headers(data, target)
        format_header("mime-type", data.mime_type, target)
        format_header("uri",       data.uri,       target)
        format_header("size",      data.size,      target)
        data.attributes.each do |name, value|
          format_header(name, value, target)
        end
      end

      def format_header(name, value, target)
        return if value.nil?
        target[name] = value
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
chupa-text-1.0.4 lib/chupa-text/formatters/json.rb
chupa-text-1.0.3 lib/chupa-text/formatters/json.rb
chupa-text-1.0.2 lib/chupa-text/formatters/json.rb
chupa-text-1.0.1 lib/chupa-text/formatters/json.rb
chupa-text-1.0.0 lib/chupa-text/formatters/json.rb