Sha256: c84fe7f32e4ca19b0488de96b0e9118c3b78f5a19d21aed0ff5ebec6f30bda9e

Contents?: true

Size: 580 Bytes

Versions: 3

Compression:

Stored size: 580 Bytes

Contents

require "synonymous/entry"

module Synonymous
  class Response
    attr_reader :suggestions, :entries

    def initialize(word, response)
      data = JSON.parse(response.body)

      if data.all? { |entry| entry.is_a?(Hash) }
        entries = data.map { |result| Entry.new(result) }
        @entries, suggestions = entries.partition { |entry| entry.headword.to_s == word }
        @suggestions = suggestions.map { |entry| entry.headword.to_s }
      else
        @entries = []
        @suggestions = data
      end
    end

    def success?
      entries.any?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
synonymous-0.3.0 lib/synonymous/response.rb
synonymous-0.2.0 lib/synonymous/response.rb
synonymous-0.1.0 lib/synonymous/response.rb