gingerice 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6087ed695413b5137a1bbb35d6c1c83a0bad3f3e
4
- data.tar.gz: 41acc2f5e49bc2552b7836abd17effdf5629d647
3
+ metadata.gz: 61b0c6b84bf3a4b75773816e81164865fae668c1
4
+ data.tar.gz: b396823b9d4551782b9f0e4bfb2b5343eef351d8
5
5
  SHA512:
6
- metadata.gz: a5a6e945f6fe1150fe33ea6cff165975c199f198e9560918472f7b5c68a3527b72617f372403266ba79dace2115c5c1e18802e656f494029d52efb58610f8583
7
- data.tar.gz: 3e16022c40bd5ccdbe91913e1ac3b8f5e95de6883f2868c46ec4da4d3026ce5c89081d0b4aa9204d43a9cd344da14524c7918ea4a04b9a132304142bbff8f141
6
+ metadata.gz: 25f1bcbbb7ff8afef7873339bc4ea24531cb6777440b9b2f54e6f6b3288c45fdf7badbec86a5c92b854ff3c2d3e75e46270a9f59ac7ee6ecb60ffa72bd3d0de4
7
+ data.tar.gz: 81f866843f30574453518fee138aec29737e09c0a3d75bb79d3b4f0370157ff80c683ce288b69df6c2e5265dd5f3ced47392fb0ad87b175a0577ed2629b88049
data/bin/gingerice CHANGED
@@ -4,6 +4,10 @@ $:.unshift(File::join(File::dirname(File::dirname(__FILE__)), "lib"))
4
4
 
5
5
  require 'gingerice/command'
6
6
 
7
- command = Gingerice::Command.new(ARGV)
8
- command.execute
7
+ begin
8
+ command = Gingerice::Command.new(ARGV)
9
+ command.execute
10
+ rescue Exception => e
11
+ puts e.message
12
+ end
9
13
 
data/lib/gingerice.rb CHANGED
@@ -1,2 +1 @@
1
- require 'gingerice/version'
2
1
  require 'gingerice/parser'
@@ -1,7 +1,6 @@
1
1
  require 'optparse'
2
2
  require 'awesome_print'
3
- require 'gingerice/parser'
4
- require 'gingerice/version'
3
+ require 'gingerice'
5
4
 
6
5
  module Gingerice
7
6
  class Command
@@ -13,10 +12,34 @@ module Gingerice
13
12
  @args << '-h' if @args.empty?
14
13
 
15
14
  @options = Gingerice::Parser.default_options.merge({ :verbose => false })
16
- @args_parser = args_parser
15
+ @args_parser = parse_args
17
16
  end
18
17
 
19
- def args_parser
18
+ def execute
19
+ if options.has_key?(:show)
20
+
21
+ case options[:show]
22
+ when :help
23
+ puts args_parser
24
+ when :version
25
+ puts "Gingerice: #{Gingerice::VERSION}"
26
+ end
27
+
28
+ else
29
+ parser_opts = options.select { |k, _| Parser.default_options.keys.include?(k) }
30
+ parser = Parser.new(parser_opts)
31
+ response = parser.parse(args.last)
32
+
33
+ if options[:verbose]
34
+ ap response
35
+ else
36
+ puts response["result"]
37
+ end
38
+ end
39
+ end
40
+
41
+ protected
42
+ def parse_args
20
43
  OptionParser.new do |opt|
21
44
  opt.banner = 'Usage: gingerice [options] "some texts"'
22
45
 
@@ -51,29 +74,6 @@ module Gingerice
51
74
  opt.parse!(args)
52
75
  end
53
76
  end
54
-
55
- def execute
56
- if options.has_key?(:show)
57
-
58
- case options[:show]
59
- when :help
60
- puts args_parser
61
- when :version
62
- puts "Gingerice: #{Gingerice::VERSION}"
63
- end
64
-
65
- else
66
- parser_opts = options.select { |k, _| Parser.default_options.keys.include?(k) }
67
- parser = Parser.new(parser_opts)
68
- response = parser.parse(args.last)
69
-
70
- if options[:verbose]
71
- ap response
72
- else
73
- puts response["result"]
74
- end
75
- end
76
- end
77
77
  end
78
78
  end
79
79
 
@@ -0,0 +1,5 @@
1
+ module Gingerice
2
+ class Error < StandardError; end
3
+ class ConnectionError < Error; end
4
+ class ParseError < Error; end
5
+ end
@@ -1,6 +1,8 @@
1
1
  require 'open-uri'
2
2
  require 'addressable/uri'
3
3
  require 'json'
4
+ require 'gingerice/version'
5
+ require 'gingerice/error'
4
6
 
5
7
  module Gingerice
6
8
  class Parser
@@ -10,32 +12,29 @@ module Gingerice
10
12
  DEFAULT_LANG = 'US'
11
13
 
12
14
  attr_accessor :lang, :api_key, :api_version, :api_endpoint
15
+ attr_reader :text, :raw_response, :result
13
16
 
14
17
  def initialize(options = {})
15
18
  merge_options(options).each do |key, value|
16
19
  send("#{key}=", value)
17
20
  end
21
+
22
+ @result = ''
23
+ @corrections = []
18
24
  end
19
25
 
20
26
  def parse(text)
21
- uri = Addressable::URI.parse(api_endpoint)
22
- uri.query_values = request_params.merge({ 'text' => text })
23
-
24
- begin
25
- open(uri) do |stream|
26
- response_processor(text, stream.read)
27
- end
28
- rescue Exception => e
29
- raise StandardError, e.message
30
- end
27
+ @text = text
28
+ perform_request
29
+ process_response
31
30
  end
32
31
 
33
32
  def self.default_options
34
33
  {
35
- :api_endpoint => Gingerice::Parser::GINGER_API_ENDPOINT,
36
- :api_version => Gingerice::Parser::GINGER_API_VERSION,
37
- :api_key => Gingerice::Parser::GINGER_API_KEY,
38
- :lang => Gingerice::Parser::DEFAULT_LANG
34
+ :api_endpoint => GINGER_API_ENDPOINT,
35
+ :api_version => GINGER_API_VERSION,
36
+ :api_key => GINGER_API_KEY,
37
+ :lang => DEFAULT_LANG
39
38
  }
40
39
  end
41
40
 
@@ -48,37 +47,67 @@ module Gingerice
48
47
  Parser.default_options.merge(options)
49
48
  end
50
49
 
51
- def response_processor(text, content)
52
- data = JSON.parse(content)
53
- i = 0
54
- result = ''
55
- corrections = []
56
-
57
- data.fetch('LightGingerTheTextResult', []).each do |r|
58
- from = r['From']
59
- to = r['To']
60
-
61
- if i <= from
62
- result += text[i..from-1] unless from.zero?
63
- result += r['Suggestions'][0]['Text']
64
-
65
- corrections << {
66
- 'text' => text[from..to],
67
- 'correct' => r['Suggestions'][0]['Text'],
68
- 'definition' => r['Suggestions'][0]['Definition'],
69
- 'start' => from,
70
- 'length' => to.to_i - from.to_i + 1
71
- }
72
- end
50
+ def perform_request
51
+ uri = Addressable::URI.parse(api_endpoint)
52
+ uri.query_values = request_params.merge({ 'text' => text })
73
53
 
74
- i = to+1
54
+ begin
55
+ open(uri) do |stream|
56
+ @raw_response = stream.read
57
+ end
58
+ rescue Exception => _
59
+ raise ConnectionError, "ERROR: Couldn't connect to API endpoint (#{api_endpoint})"
75
60
  end
61
+ end
76
62
 
77
- if i < text.length
78
- result += text[i..-1]
63
+ def process_response
64
+ begin
65
+ json_data = JSON.parse(raw_response)
66
+
67
+ i = 0
68
+
69
+ json_data.fetch('LightGingerTheTextResult', []).each do |data|
70
+ process_suggestions(i, data)
71
+
72
+ i = data['To']+1
73
+ end
74
+
75
+ if i < text.length
76
+ @result += text[i..-1]
77
+ end
78
+
79
+ {
80
+ 'text' => text,
81
+ 'result' => result,
82
+ 'corrections' => @corrections
83
+ }
84
+ rescue Exception => _
85
+ raise ParseError, "ERROR: We receive invalid JSON format!"
79
86
  end
87
+ end
88
+
89
+ def process_suggestions(i, data)
90
+ from = data['From']
91
+ to = data['To']
92
+
93
+ if i <= from
94
+ @result += text[i..from-1] unless from.zero?
95
+ @result += data['Suggestions'][0]['Text']
96
+
97
+ definition = data['Suggestions'][0]['Definition']
80
98
 
81
- { 'text' => text, 'result' => result, 'corrections' => corrections}
99
+ if definition.respond_to? :empty?
100
+ definition = nil if definition.empty?
101
+ end
102
+
103
+ @corrections << {
104
+ 'text' => text[from..to],
105
+ 'correct' => data['Suggestions'][0]['Text'],
106
+ 'definition' => definition,
107
+ 'start' => from,
108
+ 'length' => to - from + 1
109
+ }
110
+ end
82
111
  end
83
112
 
84
113
  def request_params
@@ -1,3 +1,3 @@
1
1
  module Gingerice
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
@@ -21,6 +21,7 @@ module Kernel
21
21
  end
22
22
 
23
23
  require 'test/unit'
24
+ require 'gingerice'
24
25
  require 'gingerice/command'
25
26
 
26
27
  class TestGingerice < Test::Unit::TestCase
@@ -60,11 +61,6 @@ class TestGingerice < Test::Unit::TestCase
60
61
  assert_equal 5, result['corrections'].first['length']
61
62
  end
62
63
 
63
- def test_exceptions
64
- exception = assert_raise(StandardError) { @custom_parser.parse('Hllo') }
65
- assert_equal 'getaddrinfo: Name or service not known', exception.message
66
- end
67
-
68
64
  def test_command_simple_output
69
65
  output = capture_stdout do
70
66
  command = Gingerice::Command.new(["Edwards will be sck yesterday"])
@@ -124,5 +120,19 @@ class TestGingerice < Test::Unit::TestCase
124
120
 
125
121
  assert_equal "ID", options[:lang]
126
122
  end
123
+
124
+ def test_request_exceptions
125
+ exception = assert_raise(Gingerice::ConnectionError) { @custom_parser.parse('Hllo') }
126
+ assert_equal "ERROR: Couldn't connect to API endpoint (http://example.id/)", exception.message
127
+ end
128
+
129
+ def test_parse_error_exceptions
130
+ invalid_parser = Gingerice::Parser.new({
131
+ api_endpoint: 'http://subosito.com/',
132
+ })
133
+
134
+ exception = assert_raise(Gingerice::ParseError) { invalid_parser.parse('Hllo') }
135
+ assert_equal 'ERROR: We receive invalid JSON format!', exception.message
136
+ end
127
137
  end
128
138
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gingerice
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alif Rachmawadi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-30 00:00:00.000000000 Z
11
+ date: 2013-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,6 +113,7 @@ files:
113
113
  - gingerice.gemspec
114
114
  - lib/gingerice.rb
115
115
  - lib/gingerice/command.rb
116
+ - lib/gingerice/error.rb
116
117
  - lib/gingerice/parser.rb
117
118
  - lib/gingerice/version.rb
118
119
  - test/test_gingerice.rb