Sha256: c2ce27e30bbc2c3e25eef408e172dfd45702226a8011787e3e3f74cfa9d719f8

Contents?: true

Size: 1007 Bytes

Versions: 2

Compression:

Stored size: 1007 Bytes

Contents

module LHC::Formats
  class JSON < LHC::Format
    include LHC::BasicMethodsConcern

    def self.request(options)
      options[:format] = new
      super(options)
    end

    def format_options(options)
      options[:headers] ||= {}
      no_content_type_header!(options)
      options[:headers]['Content-Type'] = 'application/json; charset=utf-8'
      no_accept_header!(options)
      options[:headers]['Accept'] = 'application/json; charset=utf-8'
      options
    end

    def as_json(input)
      parse(input, Hash)
    end

    def as_open_struct(input)
      parse(input, OpenStruct)
    end

    def to_body(input)
      if input.is_a?(String)
        input
      else
        input.to_json
      end
    end

    def to_s
      'json'
    end

    def to_sym
      to_s.to_sym
    end

    private

    def parse(input, object_class)
      ::JSON.parse(input, object_class: object_class)
    rescue ::JSON::ParserError => e
      raise LHC::ParserError.new(e.message, input)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lhc-10.1.2 lib/lhc/formats/json.rb
lhc-10.1.1 lib/lhc/formats/json.rb