openamplify 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -92,3 +92,6 @@ In case you are wondering what the request URL looks like:
92
92
  If someday, OpenAmplify decides to change their API URL:
93
93
 
94
94
  client.base_url = 'http://newurl'
95
+
96
+ == Testing
97
+ rake test OPEN_AMPLIFY_KEY=YOUR_KEY
@@ -1,5 +1,5 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 2
3
3
  :major: 0
4
4
  :build:
5
5
  :minor: 2
@@ -12,9 +12,8 @@ module OpenAmplify
12
12
  end
13
13
 
14
14
  def analyze_text(text)
15
- validate
16
- Response.new(:api_url => @options[:api_url], :query => query.merge(:inputText => text),
17
- :method => @options[:method])
15
+ OpenAmplify.validate_client!(self)
16
+ Response.new(self, :query => query.merge(:inputText => text), :method => @options[:method])
18
17
  end
19
18
 
20
19
  %w(api_key analysis api_url method).each do |attr|
@@ -28,27 +27,54 @@ module OpenAmplify
28
27
  end
29
28
  EOS
30
29
  end
30
+
31
+ def fetch(params, method)
32
+ raise OpenAmplify::NotSupported unless [:get, :post].include?(method.to_sym)
33
+ Client::send(method, self.api_url, params)
34
+ end
35
+
36
+ def self.compose_url(path, params)
37
+ path + '?' + URI.escape(params.collect{ |k,v| "#{k}=#{v}" }.join('&'))
38
+ end
39
+
31
40
 
32
41
  private
33
42
 
43
+
34
44
  def query
35
45
  q = { :apiKey => @options[:api_key] }
36
46
  q.merge!(:analysis => @options[:analysis]) if @options[:analysis]
37
47
  q
38
48
  end
39
49
 
50
+ def self.get(path, params)
51
+ uri = URI.parse(compose_url(path, params))
52
+ response = Net::HTTP.get_response(uri)
53
+ OpenAmplify.validate_response!(response)
54
+ response.body
55
+ end
56
+
57
+ def self.post(path, params)
58
+ uri = URI::parse(path)
59
+ response = Net::HTTP.post_form(uri, params)
60
+ OpenAmplify.validate_response!(response)
61
+ response.body
62
+ end
63
+
40
64
  end # OpenAmplify::Client
41
65
 
66
+
42
67
  # Contains the response from OpenAmplify
43
68
  class Response
44
69
  include Enumerable
45
70
 
46
- def initialize(options)
71
+ def initialize(client, options)
72
+ @client = client
47
73
  @options = options
48
74
  end
49
75
 
50
76
  def request_url
51
- @request_url ||= compose_url(@options[:api_url], @options[:query])
77
+ @request_url ||= Client.compose_url(@client.api_url, @options[:query])
52
78
  end
53
79
 
54
80
  def reload
@@ -100,12 +126,12 @@ module OpenAmplify
100
126
  response && response['Topics']['Domains']
101
127
  end
102
128
 
103
- private
104
-
105
- def compose_url(path, params)
106
- path + '?' + URI.escape(params.collect{ |k, v| "#{k}=#{v}" }.join('&'))
129
+ def styles
130
+ response && response['Styles']
107
131
  end
108
132
 
133
+ private
134
+
109
135
  def response
110
136
  @response ||= fetch_response
111
137
  end
@@ -123,26 +149,7 @@ module OpenAmplify
123
149
  end
124
150
 
125
151
  def fetch_as_format(format)
126
- fetch(@options[:api_url], @options[:query].merge(:outputFormat => format), @options[:method])
127
- end
128
-
129
- def fetch(path, params, method)
130
- raise OpenAmplify::NotSupported unless [:get, :post].include?(method.to_sym)
131
- self.send(method, path, params)
132
- end
133
-
134
- def get(path, params)
135
- uri = URI.parse(compose_url(path, params))
136
- response = Net::HTTP.get_response(uri)
137
- self.class.validate(response)
138
- response.body
139
- end
140
-
141
- def post(path, params)
142
- uri = URI::parse(path)
143
- response = Net::HTTP.post_form(uri, params)
144
- self.class.validate(response)
145
- response.body
152
+ @client.fetch(@options[:query].merge(:outputFormat => format), @options[:method])
146
153
  end
147
154
 
148
155
  end # OpenAmplify::Response
@@ -1,34 +1,27 @@
1
1
  module OpenAmplify
2
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
3
  class NotAcceptable < StandardError; end
15
4
  class NotSupported < StandardError; end
16
5
  class Forbidden < StandardError; end
17
6
 
18
7
 
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
8
+ def self.validate_client!(client)
9
+ raise ArgumentError, "missing api key" if client.api_key.blank?
10
+ raise ArgumentError, "missing api url" if client.api_url.blank?
11
+ end
12
+
13
+ def self.validate_response!(response)
14
+ case response.code.to_i
15
+ when 403
16
+ raise Forbidden, "(#{response.code}: #{response.message}) #{response.body}"
17
+ when 405
18
+ raise NotSupported, "(#{response.code}: #{response.message}) #{response.body}"
19
+ when 406
20
+ raise NotAcceptable, "(#{response.code}: #{response.message}) #{response.body}"
29
21
  end
30
22
  end
31
23
 
24
+
32
25
  end
33
26
 
34
27
  class String
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{openamplify}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
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-13}
12
+ s.date = %q{2010-06-14}
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 = [
@@ -62,9 +62,12 @@ class OpenAmplifyTest < Test::Unit::TestCase
62
62
  end
63
63
  end
64
64
 
65
+ #pending "show the request url"
66
+
65
67
  end # Response
66
68
 
67
69
  context "Request methods" do
70
+
68
71
 
69
72
  should "raise an error if not supported" do
70
73
  assert_raises OpenAmplify::NotSupported do
@@ -85,7 +88,11 @@ class OpenAmplifyTest < Test::Unit::TestCase
85
88
  end # Request methods
86
89
 
87
90
  def client
88
- @client ||= OpenAmplify::Client.new(:api_key => ENV['OPEN_AMPLIFY_KEY'])
91
+ @client ||= OpenAmplify::Client.new(:api_key => api_key)
92
+ end
93
+
94
+ def api_key
95
+ ENV['OPEN_AMPLIFY_KEY']
89
96
  end
90
97
 
91
98
  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: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
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-13 00:00:00 -07:00
18
+ date: 2010-06-14 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency