class Gingerice::Parser
Constants
- DEFAULT_LANG
- GINGER_API_ENDPOINT
- GINGER_API_KEY
- GINGER_API_VERSION
Attributes
api_endpoint[RW]
api_key[RW]
api_version[RW]
lang[RW]
output[RW]
raw_response[R]
result[R]
text[R]
Public Class Methods
default_options()
click to toggle source
# File lib/gingerice/parser.rb, line 41 def self.default_options { :api_endpoint => GINGER_API_ENDPOINT, :api_version => GINGER_API_VERSION, :api_key => GINGER_API_KEY, :lang => DEFAULT_LANG, :output => :verbose } end
new(options = {})
click to toggle source
# File lib/gingerice/parser.rb, line 17 def initialize(options = {}) merge_options(options).each do |key, value| send("#{key}=", value) end @result = '' @corrections = [] end
Public Instance Methods
parse(text)
click to toggle source
# File lib/gingerice/parser.rb, line 26 def parse(text) @text = text perform_request case output when :verbose process_response when :simple process_response result when :count process_count_response end end
Protected Instance Methods
merge_options(options)
click to toggle source
# File lib/gingerice/parser.rb, line 52 def merge_options(options) options.select! do |key, _| Parser.default_options.include?(key) end Parser.default_options.merge(options) end
perform_request()
click to toggle source
# File lib/gingerice/parser.rb, line 60 def perform_request uri = Addressable::URI.parse(api_endpoint) uri.query_values = request_params.merge({ 'text' => text }) begin open(uri) do |stream| @raw_response = stream.read end rescue Exception => _ raise ConnectionError, "ERROR: Couldn't connect to API endpoint (#{api_endpoint})" end end
process_count_response()
click to toggle source
# File lib/gingerice/parser.rb, line 73 def process_count_response begin json_data = JSON.parse(raw_response) json_data.fetch('LightGingerTheTextResult', []).length rescue Exception => _ raise ParseError, "ERROR: We receive invalid JSON format!" end end
process_response()
click to toggle source
# File lib/gingerice/parser.rb, line 82 def process_response begin json_data = JSON.parse(raw_response) i = 0 json_data.fetch('LightGingerTheTextResult', []).each do |data| process_suggestions(i, data) i = data['To']+1 end if i < text.length @result += text[i..-1] end { 'text' => text, 'result' => result, 'corrections' => @corrections } rescue Exception => _ raise ParseError, "ERROR: We receive invalid JSON format!" end end
process_suggestions(i, data)
click to toggle source
# File lib/gingerice/parser.rb, line 108 def process_suggestions(i, data) from = data['From'] to = data['To'] if i <= from @result += text[i..from-1] unless from.zero? suggestion = data['Suggestions'].first if suggestion suggestion_text = suggestion['Text'] @result += suggestion_text definition = suggestion['Definition'] definition = nil if definition.respond_to?(:empty?) && definition.empty? @corrections << { 'text' => text[from..to], 'correct' => suggestion_text, 'definition' => definition, 'start' => from, 'length' => to - from + 1 } end end end
request_params()
click to toggle source
# File lib/gingerice/parser.rb, line 134 def request_params { 'lang' => lang, 'apiKey' => api_key, 'clientVersion' => api_version } end