openamplify 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/History CHANGED
@@ -1,4 +1,6 @@
1
- 0.2.0 - June 11, 2010
1
+ 0.2.1 - Jun 13, 2010
2
+ * added exceptions
3
+ 0.2.0 - Jun 11, 2010
2
4
  * added helpers to topic results
3
5
  * added post method
4
6
  0.1.1 - improved the notes
data/Rakefile CHANGED
@@ -10,7 +10,11 @@ begin
10
10
  gem.email = "rubyoncloud@gmail.com"
11
11
  gem.homepage = "http://github.com/gregmoreno/openamplify"
12
12
  gem.authors = ["Greg Moreno"]
13
+
13
14
  gem.add_dependency "json", "~>1.2"
15
+
16
+ gem.add_development_dependency "shoulda", "~> 2.11"
17
+
14
18
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
19
  end
16
20
  Jeweler::GemcutterTasks.new
@@ -21,9 +25,13 @@ end
21
25
  require 'rake/testtask'
22
26
  Rake::TestTask.new(:test) do |test|
23
27
  test.libs << 'lib' << 'test'
28
+ test.ruby_opts << "-rubygems"
24
29
  test.pattern = 'test/**/test_*.rb'
25
30
  test.verbose = true
26
31
  end
32
+ # Note to run a single test do:
33
+ # ruby -Ilib test/test_foo.rb
34
+
27
35
 
28
36
  begin
29
37
  require 'rcov/rcovtask'
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
+ :patch: 1
2
3
  :major: 0
3
- :minor: 2
4
4
  :build:
5
- :patch: 0
5
+ :minor: 2
data/lib/openamplify.rb CHANGED
@@ -7,16 +7,17 @@ module OpenAmplify
7
7
 
8
8
  class Client
9
9
  def initialize(options={})
10
- @options = { :base_url => API_URL, :method => :get }
10
+ @options = { :api_url => API_URL, :method => :get }
11
11
  @options.merge!(OpenAmplify.symbolize_keys(options))
12
12
  end
13
13
 
14
14
  def analyze_text(text)
15
- Response.new(:base_url => @options[:base_url], :query => query.merge(:inputText => text),
15
+ validate
16
+ Response.new(:api_url => @options[:api_url], :query => query.merge(:inputText => text),
16
17
  :method => @options[:method])
17
18
  end
18
19
 
19
- %w(api_key analysis base_url method).each do |attr|
20
+ %w(api_key analysis api_url method).each do |attr|
20
21
  class_eval <<-EOS
21
22
  def #{attr}
22
23
  @options[:#{attr}]
@@ -47,7 +48,11 @@ module OpenAmplify
47
48
  end
48
49
 
49
50
  def request_url
50
- @request_url ||= compose_url(@options[:base_url], @options[:query])
51
+ @request_url ||= compose_url(@options[:api_url], @options[:query])
52
+ end
53
+
54
+ def reload
55
+ response
51
56
  end
52
57
 
53
58
  def each
@@ -96,6 +101,7 @@ module OpenAmplify
96
101
  end
97
102
 
98
103
  private
104
+
99
105
  def compose_url(path, params)
100
106
  path + '?' + URI.escape(params.collect{ |k, v| "#{k}=#{v}" }.join('&'))
101
107
  end
@@ -117,21 +123,26 @@ module OpenAmplify
117
123
  end
118
124
 
119
125
  def fetch_as_format(format)
120
- fetch(@options[:base_url], @options[:query].merge(:outputFormat => format), @options[:method])
126
+ fetch(@options[:api_url], @options[:query].merge(:outputFormat => format), @options[:method])
121
127
  end
122
128
 
123
129
  def fetch(path, params, method)
130
+ raise OpenAmplify::NotSupported unless [:get, :post].include?(method.to_sym)
124
131
  self.send(method, path, params)
125
132
  end
126
133
 
127
134
  def get(path, params)
128
- url = compose_url(path, params)
129
- Net::HTTP.get_response(URI.parse(url)).body
135
+ uri = URI.parse(compose_url(path, params))
136
+ response = Net::HTTP.get_response(uri)
137
+ self.class.validate(response)
138
+ response.body
130
139
  end
131
140
 
132
141
  def post(path, params)
133
- uri = URI::parse(path)
134
- Net::HTTP.post_form(uri, params).body
142
+ uri = URI::parse(path)
143
+ response = Net::HTTP.post_form(uri, params)
144
+ self.class.validate(response)
145
+ response.body
135
146
  end
136
147
 
137
148
  end # OpenAmplify::Response
@@ -146,3 +157,5 @@ module OpenAmplify
146
157
  end
147
158
 
148
159
  end # module OpenAmplify
160
+
161
+ require 'openamplify/validations'
@@ -0,0 +1,45 @@
1
+ module OpenAmplify
2
+
3
+ class Client
4
+
5
+ private
6
+
7
+ def validate
8
+ raise ArgumentError, "missing api key" if self.api_key.blank?
9
+ raise ArgumentError, "missing api url" if self.api_url.blank?
10
+ end
11
+ end
12
+
13
+
14
+ class NotAcceptable < StandardError; end
15
+ class NotSupported < StandardError; end
16
+ class Forbidden < StandardError; end
17
+
18
+
19
+ class Response
20
+ def self.validate(response)
21
+ case response.code.to_i
22
+ when 403
23
+ raise Forbidden, "(#{response.code}: #{response.message}) #{response.body}"
24
+ when 405
25
+ raise NotSupported, "(#{response.code}: #{response.message}) #{response.body}"
26
+ when 406
27
+ raise NotAcceptable, "(#{response.code}: #{response.message}) #{response.body}"
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ class String
35
+ def blank?
36
+ nil? || empty? || strip.empty?
37
+ end
38
+ end
39
+
40
+ class NilClass
41
+ def blank?
42
+ nil?
43
+ end
44
+ end
45
+
data/openamplify.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{openamplify}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Greg Moreno"]
12
- s.date = %q{2010-06-11}
12
+ s.date = %q{2010-06-13}
13
13
  s.description = %q{The OpenAmplify API reads text you supply and returns linguistic data explaining and classifying the content.}
14
14
  s.email = %q{rubyoncloud@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -24,13 +24,18 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION.yml",
26
26
  "lib/openamplify.rb",
27
- "openamplify.gemspec"
27
+ "lib/openamplify/validations.rb",
28
+ "openamplify.gemspec",
29
+ "test/test_openamplify.rb"
28
30
  ]
29
31
  s.homepage = %q{http://github.com/gregmoreno/openamplify}
30
32
  s.rdoc_options = ["--charset=UTF-8"]
31
33
  s.require_paths = ["lib"]
32
34
  s.rubygems_version = %q{1.3.7}
33
35
  s.summary = %q{Wrapper for the OpenAmplify API}
36
+ s.test_files = [
37
+ "test/test_openamplify.rb"
38
+ ]
34
39
 
35
40
  if s.respond_to? :specification_version then
36
41
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -38,11 +43,14 @@ Gem::Specification.new do |s|
38
43
 
39
44
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
45
  s.add_runtime_dependency(%q<json>, ["~> 1.2"])
46
+ s.add_development_dependency(%q<shoulda>, ["~> 2.11"])
41
47
  else
42
48
  s.add_dependency(%q<json>, ["~> 1.2"])
49
+ s.add_dependency(%q<shoulda>, ["~> 2.11"])
43
50
  end
44
51
  else
45
52
  s.add_dependency(%q<json>, ["~> 1.2"])
53
+ s.add_dependency(%q<shoulda>, ["~> 2.11"])
46
54
  end
47
55
  end
48
56
 
@@ -0,0 +1,91 @@
1
+ require 'test/unit'
2
+ require 'shoulda'
3
+ require 'openamplify'
4
+
5
+ class OpenAmplifyTest < Test::Unit::TestCase
6
+
7
+ context "Client instance" do
8
+
9
+ should "raise an error if api key is missing" do
10
+ assert_raises ArgumentError do
11
+ client = OpenAmplify::Client.new
12
+ client.analyze_text("sample")
13
+ end
14
+ end
15
+
16
+ should "not raise an error if api key is present" do
17
+ assert_nothing_raised do
18
+ client.analyze_text("sample")
19
+ end
20
+ end
21
+
22
+ should "raise an error if api key is not authorized" do
23
+ assert_raises OpenAmplify::Forbidden do
24
+ client = OpenAmplify::Client.new(:api_key => 'SOME_KEY')
25
+ client.analyze_text("sample").reload
26
+ end
27
+ end
28
+
29
+ should "raise an error if api_url is missing" do
30
+ assert_raises ArgumentError do
31
+ client.api_url = ''
32
+ client.analyze_text('sample')
33
+ end
34
+ end
35
+
36
+ should "not raise an error if api_url is present" do
37
+ assert_nothing_raised do
38
+ client.analyze_text('sample')
39
+ end
40
+ end
41
+
42
+ end # context "Client instance"
43
+
44
+ context "Response" do
45
+
46
+ should "raise an error if empty text" do
47
+ assert_raises OpenAmplify::NotAcceptable do
48
+ client.analyze_text('').reload
49
+ end
50
+ end
51
+
52
+ should "not raise an error if there is input text" do
53
+ assert_nothing_raised do
54
+ client.analyze_text('sample').reload
55
+ end
56
+ end
57
+
58
+ should "raise error if analyis is not supported" do
59
+ assert_raises OpenAmplify::NotSupported do
60
+ client.analysis = 'not supported'
61
+ client.analyze_text('sample').reload
62
+ end
63
+ end
64
+
65
+ end # Response
66
+
67
+ context "Request methods" do
68
+
69
+ should "raise an error if not supported" do
70
+ assert_raises OpenAmplify::NotSupported do
71
+ client.method = :delete
72
+ client.analyze_text('sample').reload
73
+ end
74
+ end
75
+
76
+ [:get, :post].each do |method|
77
+ should "not raise error for #{method}" do
78
+ assert_nothing_raised do
79
+ client.method = method
80
+ client.analyze_text('sample').reload
81
+ end
82
+ end
83
+ end
84
+
85
+ end # Request methods
86
+
87
+ def client
88
+ @client ||= OpenAmplify::Client.new(:api_key => ENV['OPEN_AMPLIFY_KEY'])
89
+ end
90
+
91
+ end # OpenAmplifyTest
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openamplify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Greg Moreno
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-11 00:00:00 -07:00
18
+ date: 2010-06-13 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -33,6 +33,21 @@ dependencies:
33
33
  version: "1.2"
34
34
  type: :runtime
35
35
  version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 21
45
+ segments:
46
+ - 2
47
+ - 11
48
+ version: "2.11"
49
+ type: :development
50
+ version_requirements: *id002
36
51
  description: The OpenAmplify API reads text you supply and returns linguistic data explaining and classifying the content.
37
52
  email: rubyoncloud@gmail.com
38
53
  executables: []
@@ -50,7 +65,9 @@ files:
50
65
  - Rakefile
51
66
  - VERSION.yml
52
67
  - lib/openamplify.rb
68
+ - lib/openamplify/validations.rb
53
69
  - openamplify.gemspec
70
+ - test/test_openamplify.rb
54
71
  has_rdoc: true
55
72
  homepage: http://github.com/gregmoreno/openamplify
56
73
  licenses: []
@@ -85,5 +102,5 @@ rubygems_version: 1.3.7
85
102
  signing_key:
86
103
  specification_version: 3
87
104
  summary: Wrapper for the OpenAmplify API
88
- test_files: []
89
-
105
+ test_files:
106
+ - test/test_openamplify.rb