openamplify 0.2.3 → 0.3.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/.gitignore +3 -0
- data/Gemfile +4 -0
- data/History +4 -0
- data/README.rdoc +61 -46
- data/Rakefile +64 -54
- data/TODO +1 -0
- data/bin/openamplify +49 -0
- data/lib/openamplify.rb +13 -158
- data/lib/openamplify/analysis/context.rb +67 -0
- data/lib/openamplify/client.rb +82 -0
- data/lib/openamplify/configuration.rb +54 -0
- data/lib/openamplify/connection.rb +28 -0
- data/lib/openamplify/error.rb +20 -0
- data/lib/openamplify/request.rb +35 -0
- data/lib/openamplify/response/raise_client_error.rb +28 -0
- data/lib/openamplify/response/raise_server_error.rb +23 -0
- data/lib/openamplify/version.rb +3 -0
- data/openamplify.gemspec +19 -49
- data/test/fixtures/input.txt +2 -0
- data/test/helper.rb +14 -0
- data/test/openamplify/analysis_test.rb +102 -0
- data/test/openamplify/client_test.rb +67 -0
- data/test/openamplify/configuration_test.rb +38 -0
- data/test/openamplify/error_test.rb +10 -0
- metadata +98 -73
- data/VERSION.yml +0 -5
- data/lib/openamplify/validations.rb +0 -38
- data/test/test_openamplify.rb +0 -98
@@ -0,0 +1,67 @@
|
|
1
|
+
module OpenAmplify
|
2
|
+
|
3
|
+
module Analysis
|
4
|
+
|
5
|
+
class Context
|
6
|
+
VALID_OPTIONS_KEYS = OpenAmplify::Configuration::VALID_OPTIONS_KEYS + [:input]
|
7
|
+
|
8
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
9
|
+
|
10
|
+
attr_accessor :client
|
11
|
+
attr_reader :result
|
12
|
+
|
13
|
+
def initialize(client, input, options)
|
14
|
+
self.client = client
|
15
|
+
self.input = input
|
16
|
+
|
17
|
+
merged_options = OpenAmplify.options.merge(options)
|
18
|
+
OpenAmplify::Configuration::VALID_OPTIONS_KEYS.each do |key|
|
19
|
+
send("#{key}=", merged_options[key])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# The empty?, to_s, to_str are to make our object play
|
24
|
+
# with others that expect a string.
|
25
|
+
#
|
26
|
+
# Example, you can do this:
|
27
|
+
# puts result
|
28
|
+
#
|
29
|
+
# instead of:
|
30
|
+
# puts result.to_s
|
31
|
+
|
32
|
+
def empty?
|
33
|
+
result.empty?
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s
|
37
|
+
"#{result}"
|
38
|
+
end
|
39
|
+
alias_method :to_str, :to_s
|
40
|
+
|
41
|
+
# Support various conversion helpers
|
42
|
+
|
43
|
+
%w(xml json json_js rdf rdfa csv signals pretty dart oas).each do |format|
|
44
|
+
define_method("to_#{format}") do
|
45
|
+
@result = call(:output_format => format)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def result
|
52
|
+
@result ||= call
|
53
|
+
end
|
54
|
+
|
55
|
+
def call(options={})
|
56
|
+
@result = client.request_analysis(request_analysis_options.merge(options))
|
57
|
+
end
|
58
|
+
|
59
|
+
def request_analysis_options
|
60
|
+
Hash[* VALID_OPTIONS_KEYS.map { |key| [key, send(key) ]}.flatten ]
|
61
|
+
end
|
62
|
+
|
63
|
+
end # Context
|
64
|
+
|
65
|
+
end # Analysis
|
66
|
+
|
67
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'openamplify/analysis/context'
|
2
|
+
require 'openamplify/connection'
|
3
|
+
require 'openamplify/request'
|
4
|
+
|
5
|
+
module OpenAmplify
|
6
|
+
# Provides access to the OpenAmplify API http://portaltnx20.openamplify.com/AmplifyWeb_v20/
|
7
|
+
#
|
8
|
+
# Basic usage of the library is to call supported methods via the Client class.
|
9
|
+
#
|
10
|
+
# text = "After getting the MX1000 laser mouse and the Z-5500 speakers i fell in love with logitech"
|
11
|
+
# OpenAmplify::Client.new.amplify(text)
|
12
|
+
|
13
|
+
class Client
|
14
|
+
include OpenAmplify::Connection
|
15
|
+
include OpenAmplify::Request
|
16
|
+
|
17
|
+
attr_accessor *Configuration::VALID_CONFIG_KEYS
|
18
|
+
|
19
|
+
def initialize(options={})
|
20
|
+
merged_options = OpenAmplify.options.merge(options)
|
21
|
+
Configuration::VALID_CONFIG_KEYS.each do |key|
|
22
|
+
send("#{key}=", merged_options[key])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def amplify(input, options={})
|
27
|
+
options = analysis_options.merge(options)
|
28
|
+
Analysis::Context.new(self, input, options)
|
29
|
+
end
|
30
|
+
|
31
|
+
def analysis_options
|
32
|
+
Hash[ *Configuration::VALID_OPTIONS_KEYS.map { |key| [key, send(key)] }.flatten ]
|
33
|
+
end
|
34
|
+
|
35
|
+
def request_analysis(options)
|
36
|
+
params = prepare_request_params(options)
|
37
|
+
|
38
|
+
case self.method
|
39
|
+
when :post
|
40
|
+
post(self.endpoint, params)
|
41
|
+
else
|
42
|
+
get(self.endpoint, params)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# @deprecated Please use {#amplify} instead
|
47
|
+
def analyze_text(input)
|
48
|
+
warn "[DEPRECATION] `analyze_text` is deprecated. Please use `amplify` instead."
|
49
|
+
amplify input
|
50
|
+
end
|
51
|
+
|
52
|
+
# @deprecated Please use {#endpoint} instead
|
53
|
+
def base_url=(url)
|
54
|
+
warn "[DEPRECATION] `base_url` is deprecated. Please use `endpoint` instead."
|
55
|
+
self.endpoint = url
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# Formulate the parameters that is understood by
|
61
|
+
# the OpenAmplify webservice.
|
62
|
+
# ex: :api_key becomes 'apikey'
|
63
|
+
def prepare_request_params(options)
|
64
|
+
params = detect_request_input options.delete(:input)
|
65
|
+
|
66
|
+
options.inject(params) do |params, kv|
|
67
|
+
key, value = kv
|
68
|
+
params.merge!("#{key.to_s.downcase.gsub(/_+/, '')}" => value)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def detect_request_input(input)
|
73
|
+
if input =~ /^https?:\/\//
|
74
|
+
{ 'sourceurl' => input }
|
75
|
+
else
|
76
|
+
{ 'inputtext' => input }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end # Client
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'openamplify/version'
|
2
|
+
|
3
|
+
# TODO: output_format, analysis, scoring can be specied in the client and becomes the default unless overriden
|
4
|
+
|
5
|
+
module OpenAmplify
|
6
|
+
# Defines constants and methods for configuring a client
|
7
|
+
module Configuration
|
8
|
+
VALID_CONNECTION_KEYS = [:endpoint, :user_agent, :method, :adapter].freeze
|
9
|
+
VALID_OPTIONS_KEYS = [:api_key, :analysis, :output_format, :scoring].freeze
|
10
|
+
|
11
|
+
VALID_CONFIG_KEYS = VALID_CONNECTION_KEYS + VALID_OPTIONS_KEYS
|
12
|
+
|
13
|
+
DEFAULT_ENDPOINT = 'http://portaltnx20.openamplify.com/AmplifyWeb_v21/AmplifyThis'
|
14
|
+
DEFAULT_HTTP_METHOD = :get
|
15
|
+
DEFAULT_HTTP_ADAPTER = :net_http
|
16
|
+
DEFAULT_USER_AGENT = "OpenAmplify Ruby Gem #{OpenAmplify::VERSION}".freeze
|
17
|
+
|
18
|
+
DEFAULT_API_KEY = nil
|
19
|
+
DEFAULT_ANALYSIS = :all
|
20
|
+
DEFAULT_OUTPUT_FORMAT = :xml
|
21
|
+
DEFAULT_SCORING = :standard
|
22
|
+
DEFAULT_SOURCE_URL = nil
|
23
|
+
DEFAULT_INPUT_TEXT = nil
|
24
|
+
|
25
|
+
attr_accessor *VALID_CONFIG_KEYS
|
26
|
+
|
27
|
+
def self.extended(base)
|
28
|
+
base.reset
|
29
|
+
end
|
30
|
+
|
31
|
+
# Convenience method to allow configuration options to be set in a block
|
32
|
+
def configure
|
33
|
+
yield self
|
34
|
+
end
|
35
|
+
|
36
|
+
def options
|
37
|
+
Hash[ * VALID_CONFIG_KEYS.map { |key| [key, send(key)] }.flatten ]
|
38
|
+
end
|
39
|
+
|
40
|
+
def reset
|
41
|
+
self.endpoint = DEFAULT_ENDPOINT
|
42
|
+
self.method = DEFAULT_HTTP_METHOD
|
43
|
+
self.adapter = DEFAULT_HTTP_ADAPTER
|
44
|
+
self.user_agent = DEFAULT_USER_AGENT
|
45
|
+
|
46
|
+
self.api_key = DEFAULT_API_KEY
|
47
|
+
self.analysis = DEFAULT_ANALYSIS
|
48
|
+
self.output_format = DEFAULT_OUTPUT_FORMAT
|
49
|
+
self.scoring = DEFAULT_SCORING
|
50
|
+
end
|
51
|
+
|
52
|
+
end # Configuration
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'openamplify/response/raise_client_error'
|
3
|
+
require 'openamplify/response/raise_server_error'
|
4
|
+
|
5
|
+
module OpenAmplify
|
6
|
+
module Connection
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def connection(options)
|
11
|
+
default_options = {
|
12
|
+
:url => options.fetch(:endpoint, endpoint)
|
13
|
+
}
|
14
|
+
|
15
|
+
@connection ||= Faraday.new(default_options) do |builder|
|
16
|
+
builder.use OpenAmplify::Response::RaiseClientError
|
17
|
+
builder.use OpenAmplify::Response::RaiseServerError
|
18
|
+
|
19
|
+
# TODO: Make logging optional
|
20
|
+
# builder.response :logger
|
21
|
+
|
22
|
+
builder.adapter adapter
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end # Connection
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module OpenAmplify
|
2
|
+
|
3
|
+
class Error < StandardError
|
4
|
+
attr_reader :http_headers
|
5
|
+
|
6
|
+
def initialize(message, http_headers)
|
7
|
+
@http_headers = http_headers
|
8
|
+
super(message)
|
9
|
+
end
|
10
|
+
|
11
|
+
end # Error
|
12
|
+
|
13
|
+
class Error::ServerError < OpenAmplify::Error; end
|
14
|
+
class Error::ServiceUnavailable < Error::ServerError; end
|
15
|
+
|
16
|
+
class Error::ClientError < OpenAmplify::Error; end
|
17
|
+
class Error::Forbidden < Error::ClientError; end
|
18
|
+
class Error::BadRequest < Error::ClientError; end
|
19
|
+
class Error::RequestTooLarge < Error::ClientError; end
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module OpenAmplify
|
5
|
+
# Handless HTTP requests
|
6
|
+
module Request
|
7
|
+
def get(path, params={}, options={})
|
8
|
+
request(:get, path, params, options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def post(path, params={}, options={})
|
12
|
+
request(:post, path, params, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def request(method, path, params, options)
|
18
|
+
response = connection(options).run_request(method, nil, nil, nil) do |request|
|
19
|
+
request.options[:raw] = true if options[:raw]
|
20
|
+
|
21
|
+
case method.to_sym
|
22
|
+
when :get
|
23
|
+
request.url(path, params)
|
24
|
+
when :post
|
25
|
+
request.path = path
|
26
|
+
request.body = params unless params.empty?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
options[:raw] ? response : response.body
|
31
|
+
end
|
32
|
+
|
33
|
+
end # Request
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module OpenAmplify
|
4
|
+
|
5
|
+
module Response
|
6
|
+
|
7
|
+
class RaiseClientError < Faraday::Response::Middleware
|
8
|
+
|
9
|
+
def on_complete(env)
|
10
|
+
status = env[:status].to_i
|
11
|
+
body = env[:body]
|
12
|
+
headers = env[:response_headers]
|
13
|
+
|
14
|
+
case status
|
15
|
+
when 400
|
16
|
+
raise OpenAmplify::Error::BadRequest.new body, headers
|
17
|
+
when 403
|
18
|
+
raise OpenAmplify::Error::Forbidden.new body, headers
|
19
|
+
when 413
|
20
|
+
raise OpenAmplify::Error::RequestTooLarge.new body, headers
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end # RaiseClientError
|
25
|
+
|
26
|
+
end # Response
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'openamplify/error'
|
3
|
+
|
4
|
+
module OpenAmplify
|
5
|
+
module Response
|
6
|
+
class RaiseServerError < Faraday::Response::Middleware
|
7
|
+
|
8
|
+
def on_complete(env)
|
9
|
+
status = env[:status].to_i
|
10
|
+
body = env[:body]
|
11
|
+
headers = env[:response_headers]
|
12
|
+
|
13
|
+
case status
|
14
|
+
when 503
|
15
|
+
raise OpenAmplify::Error::ServiceUnavailable.new "503 No server is available to handle this request.", headers
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end # RaiseServerError
|
20
|
+
|
21
|
+
end # Response
|
22
|
+
|
23
|
+
end
|
data/openamplify.gemspec
CHANGED
@@ -1,56 +1,26 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "openamplify/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |s|
|
7
|
-
s.name
|
8
|
-
s.version
|
6
|
+
s.name = "openamplify"
|
7
|
+
s.version = OpenAmplify::VERSION
|
8
|
+
s.authors = ["Greg Moreno"]
|
9
|
+
s.email = ["greg.moreno@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = "Wrapper for the OpenAmplify API"
|
12
|
+
s.description = "The OpenAmplify API reads text you supply and returns linguistic data explaining and classifying the content."
|
9
13
|
|
10
|
-
s.
|
11
|
-
s.authors = ["Greg Moreno"]
|
12
|
-
s.date = %q{2010-06-22}
|
13
|
-
s.description = %q{The OpenAmplify API reads text you supply and returns linguistic data explaining and classifying the content.}
|
14
|
-
s.email = %q{rubyoncloud@gmail.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE",
|
17
|
-
"README.rdoc"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".gitignore",
|
21
|
-
"History",
|
22
|
-
"LICENSE",
|
23
|
-
"README.rdoc",
|
24
|
-
"Rakefile",
|
25
|
-
"VERSION.yml",
|
26
|
-
"lib/openamplify.rb",
|
27
|
-
"lib/openamplify/validations.rb",
|
28
|
-
"openamplify.gemspec",
|
29
|
-
"test/test_openamplify.rb"
|
30
|
-
]
|
31
|
-
s.homepage = %q{http://github.com/gregmoreno/openamplify}
|
32
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
-
s.require_paths = ["lib"]
|
34
|
-
s.rubygems_version = %q{1.3.6}
|
35
|
-
s.summary = %q{Wrapper for the OpenAmplify API}
|
36
|
-
s.test_files = [
|
37
|
-
"test/test_openamplify.rb"
|
38
|
-
]
|
14
|
+
s.rubyforge_project = "openamplify"
|
39
15
|
|
40
|
-
|
41
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
42
|
-
s.specification_version = 3
|
16
|
+
s.files = `git ls-files`.split("\n")
|
43
17
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
else
|
48
|
-
s.add_dependency(%q<json>, ["~> 1.2"])
|
49
|
-
s.add_dependency(%q<shoulda>, ["~> 2.11"])
|
50
|
-
end
|
51
|
-
else
|
52
|
-
s.add_dependency(%q<json>, ["~> 1.2"])
|
53
|
-
s.add_dependency(%q<shoulda>, ["~> 2.11"])
|
54
|
-
end
|
55
|
-
end
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
56
21
|
|
22
|
+
s.add_dependency 'faraday'
|
23
|
+
s.add_development_dependency 'minitest'
|
24
|
+
s.add_development_dependency 'nokogiri'
|
25
|
+
s.add_development_dependency 'json'
|
26
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'openamplify'
|
2
|
+
require 'minitest/spec'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
|
5
|
+
require 'awesome_print'
|
6
|
+
|
7
|
+
OpenAmplify.configure do |config|
|
8
|
+
config.api_key = ENV['OPEN_AMPLIFY_KEY']
|
9
|
+
end
|
10
|
+
|
11
|
+
def amplify_input
|
12
|
+
fpath = File.dirname(__FILE__) + '/fixtures/input.txt'
|
13
|
+
@amplify_params ||= File.read(fpath)
|
14
|
+
end
|