openamplify 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History CHANGED
@@ -0,0 +1,5 @@
1
+ 0.2.0 - June 11, 2010
2
+ * added helpers to topic results
3
+ * added post method
4
+ 0.1.1 - improved the notes
5
+ 0.1.0 - initial release
@@ -36,6 +36,12 @@ monitoring the tone of forum threads.
36
36
  # 'response' works like a Hash
37
37
  puts response['Topics']
38
38
 
39
+ # or use the shortcuts
40
+ response.top_topics
41
+ response.proper_nouns
42
+ response.locations
43
+ response.domains
44
+
39
45
  === Output Format
40
46
 
41
47
  In case you need a different format, OpenAmplify supports XML, JSON, RDF, CSV.
@@ -68,6 +74,15 @@ The different options and explanations are available at http://community.openamp
68
74
  response['Topics'] # => should be another big Hash of key-value pairs
69
75
  response['Demographics'] # => nil
70
76
 
77
+ === POST method
78
+
79
+ By default, GET is used. If you need to analyze lots of text, use POST
80
+
81
+ client = OpenAmplify::Client.new(:api_key => API_KEY, :method => :post)
82
+
83
+ # or
84
+ client.method = :post
85
+
71
86
  === Request URL
72
87
 
73
88
  In case you are wondering what the request URL looks like:
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 1
3
+ :minor: 2
4
4
  :build:
5
- :patch: 1
5
+ :patch: 0
@@ -6,16 +6,17 @@ module OpenAmplify
6
6
  API_URL = "http://portaltnx20.openamplify.com/AmplifyWeb_v20/AmplifyThis"
7
7
 
8
8
  class Client
9
- def initialize(options)
10
- @options = { :base_url => API_URL }
9
+ def initialize(options={})
10
+ @options = { :base_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
+ Response.new(:base_url => @options[:base_url], :query => query.merge(:inputText => text),
16
+ :method => @options[:method])
16
17
  end
17
18
 
18
- %w(api_key analysis base_url).each do |attr|
19
+ %w(api_key analysis base_url method).each do |attr|
19
20
  class_eval <<-EOS
20
21
  def #{attr}
21
22
  @options[:#{attr}]
@@ -49,50 +50,64 @@ module OpenAmplify
49
50
  @request_url ||= compose_url(@options[:base_url], @options[:query])
50
51
  end
51
52
 
52
- def response
53
- @response ||= get_response
54
- end
55
-
56
- # Make this class behave like a Hash
57
- # TODO: Add more methods
58
-
59
53
  def each
60
54
  response.each do |k, v|
61
55
  yield(k, v)
62
56
  end
63
57
  end
64
-
65
- ['has_key?', '==', '[]', 'fetch', 'empty?', 'include?', 'inspect',
66
- 'key?', 'keys', 'length', 'member?', 'merge', 'merge!'
67
- ].each do |method|
68
- class_eval <<-EOS
69
- def #{method}(*args)
70
- response.send('#{method}', *args)
71
- end
72
- EOS
58
+
59
+ def method_missing(name, *args, &block)
60
+ response.send(name, *args, &block)
73
61
  end
74
- ## Hash methods end here
75
-
62
+
76
63
  # Support the different formats. Note this would entail another request
77
64
  # to openamplify
78
65
  %w(xml json rdf csv oas signals pretty).each do |format|
79
66
  class_eval <<-EOS
80
67
  def to_#{format}
81
- get('#{format}')
68
+ fetch_as_format(:#{format})
82
69
  end
83
70
  EOS
84
71
  end
85
72
 
86
- private
73
+ def top_topics
74
+ items = response && response['Topics']['TopTopics']
75
+ end
87
76
 
77
+ def proper_nouns
78
+ items = response && response['Topics']['ProperNouns']
79
+
80
+ return items if items.is_a?(Array)
81
+
82
+ # I'm not sure if this is the default behavior if
83
+ # only a single item is found, or if it is a bug
84
+ # TODO: check other helpers as well
85
+ if items.is_a?(Hash)
86
+ return [ items['TopicResult'] ]
87
+ end
88
+ end
89
+
90
+ def locations
91
+ response && response['Topics']['Locations']
92
+ end
93
+
94
+ def domains
95
+ response && response['Topics']['Domains']
96
+ end
97
+
98
+ private
88
99
  def compose_url(path, params)
89
100
  path + '?' + URI.escape(params.collect{ |k, v| "#{k}=#{v}" }.join('&'))
90
101
  end
102
+
103
+ def response
104
+ @response ||= fetch_response
105
+ end
91
106
 
92
- def get_response
93
- response = get('json')
107
+ def fetch_response
108
+ response = fetch_as_format(:json)
94
109
  result = JSON.parse(response)
95
-
110
+
96
111
  if analysis = @options[:query][:analysis]
97
112
  name = analysis.sub(/./){ |s| s.upcase }
98
113
  result["ns1:#{name}Response"]["#{name}Return"]
@@ -101,12 +116,24 @@ module OpenAmplify
101
116
  end
102
117
  end
103
118
 
104
- def get(format)
105
- params = @options[:query]
106
- url = compose_url(@options[:base_url], params.merge(:outputFormat => format))
119
+ def fetch_as_format(format)
120
+ fetch(@options[:base_url], @options[:query].merge(:outputFormat => format), @options[:method])
121
+ end
122
+
123
+ def fetch(path, params, method)
124
+ self.send(method, path, params)
125
+ end
126
+
127
+ def get(path, params)
128
+ url = compose_url(path, params)
107
129
  Net::HTTP.get_response(URI.parse(url)).body
108
130
  end
109
131
 
132
+ def post(path, params)
133
+ uri = URI::parse(path)
134
+ Net::HTTP.post_form(uri, params).body
135
+ end
136
+
110
137
  end # OpenAmplify::Response
111
138
 
112
139
  private
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{openamplify}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
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-05}
12
+ s.date = %q{2010-06-11}
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 = [
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: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
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-05 00:00:00 -07:00
18
+ date: 2010-06-11 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency