dandelionapi 0.0.3 → 0.0.5

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: e24257bbcc32e368db80064ce40f9ddd15c2c6a8
4
- data.tar.gz: bc758810bdfb49dbb0767b5f2018e13730ca234d
3
+ metadata.gz: a48b39d9bb3f7624ba350f7c0bd0edbeafeda0df
4
+ data.tar.gz: 7999d50dcae26f33fb6683e4ed71a781b38570ed
5
5
  SHA512:
6
- metadata.gz: a0408cdf917450acb12259956dd8852654cd154cd0f0fd5b7d482a7773c305985fd3300677a62461903d7d52eee1357215c5efd6372c641f10298dee607a74ea
7
- data.tar.gz: b9696acda5c66d2c2c5cd17817010b52d88adc09f3bb23340a157b70c5db8312e291c07096145a0ecb76e8b1adb1d66847ab8548db614928a85fc67352c1075d
6
+ metadata.gz: 371fa3174dd1a28f9ec212b5e8452ab5f832ad20537f6172905f029a63425ec75b186d8d9cf70d59c58bf2c508981f649a117db647006aad6b075119337f9005
7
+ data.tar.gz: 29b690656735cf22ba37b56aa2d54db8226fe4398bbd8a0da3d10c2f94ec35b829ae6d7c20de80211a19ef61e0fcf89492b0e6775b85ad21177b8ed4b36b3b9d
data/.gitignore CHANGED
@@ -8,4 +8,5 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.gem
11
- .DS_Store
11
+ .DS_Store
12
+ .idea/
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # [![Ruby Gem Icon](https://raw.githubusercontent.com/zenkay/dandelionapi-ruby/master/rubygem.png)](https://rubygems.org/gems/dandelionapi) Dandelion API Ruby Gem
2
2
 
3
- [![Code Climate](https://codeclimate.com/github/zenkay/dandelionapi-ruby/badges/gpa.svg)](https://codeclimate.com/github/zenkay/dandelionapi-ruby) [![Travis CI](https://travis-ci.org/zenkay/dandelionapi-ruby.svg?branch=master)](https://travis-ci.org/zenkay/dandelionapi-ruby) [![Gem Version](https://badge.fury.io/rb/dandelionapi.svg)](http://badge.fury.io/rb/dandelionapi) [![Coverage Status](https://coveralls.io/repos/zenkay/dandelionapi-ruby/badge.svg)](https://coveralls.io/r/zenkay/dandelionapi-ruby)
3
+ [![Code Climate](https://codeclimate.com/github/zenkay/dandelionapi-ruby/badges/gpa.svg)](https://codeclimate.com/github/zenkay/dandelionapi-ruby) [![Travis CI](https://travis-ci.org/zenkay/dandelionapi-ruby.svg?branch=master)](https://travis-ci.org/zenkay/dandelionapi-ruby) [![Gem Version](https://badge.fury.io/rb/dandelionapi.svg)](http://badge.fury.io/rb/dandelionapi) [![Coverage Status](https://coveralls.io/repos/github/zenkay/dandelionapi-ruby/badge.svg?branch=master)](https://coveralls.io/github/zenkay/dandelionapi-ruby?branch=master)
4
4
 
5
5
  ## Installation
6
6
 
@@ -15,14 +15,14 @@ And then execute:
15
15
  ```
16
16
  $ bundle install
17
17
  ```
18
+
18
19
  ## Setup
19
20
 
20
21
  Setup configuration parameters
21
22
 
22
23
  ```
23
24
  Dandelionapi.configure do |c|
24
- c.app_id = "your-app-id-for-dandelionapi-account"
25
- c.app_key = "your-app-key-for-dandelionapi-account"
25
+ c.token = "your-token-for-dandelionapi-account"
26
26
  c.endpoint = "https://api.dandelion.eu/"
27
27
  end
28
28
  ```
@@ -62,13 +62,14 @@ response = element.analyze(text: "I'm really disappointed")
62
62
 
63
63
  ## About the DandelionAPI project
64
64
 
65
- This library is part of a larger group of libraries used to explore best strategies to build packages among multiple languages. Each library is written using conventions about coding style, filesystem structure, testing, documentation and distribution typical of the language enviroment but preserve the library main concepts. This version use the following conventions:
65
+ This library is part of a larger group of libraries used to explore best strategies to build packages among multiple languages. Each library is written using conventions about coding style, filesystem structure, testing, documentation and distribution typical of the language enviroment but preserve the library main concepts. This version use the following conventions for the **Ruby** environment:
66
66
 
67
67
  - Tested using [RSpec](http://rspec.info/)
68
68
  - Documented using [YARD](http://yardoc.org/)
69
+ - Quality monitored using [Travis CI](https://travis-ci.org/), [Code Climate](https://codeclimate.com/) and [Coveralls](https://coveralls.io/)
69
70
  - Packed using [Bundler](http://bundler.io/) and [Gem](https://rubygems.org/)
70
71
  - Distributed over [RubyGems](https://rubygems.org/) as [dandelionapi](https://rubygems.org/gems/dandelionapi)
71
72
 
72
73
  ----
73
74
 
74
- Here is the complete list of libraries available: [Ruby](https://github.com/zenkay/dandelionapi-ruby), [PHP](https://github.com/zenkay/dandelionapi-php), [Node](https://github.com/zenkay/dandelionapi-node)
75
+ Here is the complete list of available libraries: [Ruby](https://github.com/zenkay/dandelionapi-ruby), [PHP](https://github.com/zenkay/dandelionapi-php), [Node](https://github.com/zenkay/dandelionapi-node), [Python](https://github.com/zenkay/dandelionapi-python)
@@ -3,6 +3,8 @@
3
3
  require "dandelionapi/version"
4
4
 
5
5
  require "dandelionapi/base"
6
+ require "dandelionapi/analysis"
7
+ require "dandelionapi/comparison"
6
8
  require "dandelionapi/entity_extraction"
7
9
  require "dandelionapi/text_similarity"
8
10
  require "dandelionapi/language_detection"
@@ -14,7 +16,7 @@ module Dandelionapi
14
16
  #
15
17
  # @example
16
18
  # Dandelionapi.configure do |c|
17
- # c.app_id = "test"
19
+ # c.token = "test"
18
20
  # end
19
21
  #
20
22
  def self.configure(&block)
@@ -24,7 +26,7 @@ module Dandelionapi
24
26
  # Return configuration parameters
25
27
  #
26
28
  # @example
27
- # Dandelionapi.config.app_id
29
+ # Dandelionapi.config.token
28
30
  #
29
31
  def self.config
30
32
  @config
@@ -33,7 +35,7 @@ module Dandelionapi
33
35
  # Container for configuration parameters
34
36
  #
35
37
  class Configuration
36
- attr_accessor :app_id, :app_key, :endpoint
38
+ attr_accessor :token, :endpoint, :path
37
39
  end
38
40
 
39
41
  # Exception raised for connection error
@@ -42,5 +44,14 @@ module Dandelionapi
42
44
 
43
45
  # Exception raised when a mandatory parameter is missing
44
46
  #
45
- class MissingParameters < Exception; end
47
+ class MissingParameter < Exception; end
48
+
49
+ # Exception raised when more than one require parameter is given
50
+ #
51
+ class TooManyParameters < Exception; end
52
+
53
+ # Exception raised when a parameter is in the wrong format
54
+ #
55
+ class WrongParameterFormat < Exception; end
56
+
46
57
  end
@@ -0,0 +1,60 @@
1
+ # encoding: UTF-8
2
+
3
+ require "faraday"
4
+ require "faraday_middleware"
5
+ require "json"
6
+
7
+ module Dandelionapi
8
+ module Analysis
9
+
10
+ class Request < Base::Request
11
+
12
+ def analyze(options)
13
+ params = filter_permitted_params options
14
+
15
+ if required_params_missing? params
16
+ raise MissingParameter.new "Please provide one of the following parameters: #{self.class::MANDATORY_FIELDS.join(", ")} as input"
17
+ end
18
+
19
+ if too_many_mandatory_parameters? params, 1
20
+ raise TooManyParameters.new "Please provide only one of the following parameters: #{self.class::MANDATORY_FIELDS.join(", ")} as input"
21
+ end
22
+
23
+ verify_format params do |wrong_param|
24
+ raise WrongParameterFormat.new "Wrong format: #{self.class::FIELDS_FORMAT[wrong_param][:error_message]}"
25
+ end
26
+
27
+ call(self.class::ENDPOINT, params)
28
+ end
29
+
30
+ protected
31
+
32
+ def filter_permitted_params options
33
+ params = {}
34
+ options.each { |key, value| params[key.to_s] = value }
35
+ params.select { |key,value| (self.class::MANDATORY_FIELDS + self.class::OPTIONAL_FIELDS).include? key }
36
+ end
37
+
38
+ def required_params_missing? params
39
+ mandatory_fields_count(params) == 0
40
+ end
41
+
42
+ def too_many_mandatory_parameters? params, max
43
+ mandatory_fields_count(params) > max
44
+ end
45
+
46
+ def mandatory_fields_count params
47
+ (self.class::MANDATORY_FIELDS & params.keys).size
48
+ end
49
+
50
+ def verify_format params, &block
51
+ params.each do |key, value|
52
+ if self.class::FIELDS_FORMAT[key] and self.class::FIELDS_FORMAT[key][:valid]
53
+ yield key unless self.class::FIELDS_FORMAT[key][:valid].call value
54
+ end
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -1,32 +1,29 @@
1
1
  # encoding: UTF-8
2
2
 
3
- require "faraday"
4
- require "faraday_middleware"
5
- require "json"
6
-
7
3
  module Dandelionapi
4
+ module Base
8
5
 
9
- class Base
6
+ class Request
10
7
 
11
- protected
8
+ protected
12
9
 
13
- def call(endpoint, params)
14
- begin
15
- params = params.merge(
16
- :$app_id => Dandelionapi.config.app_id,
17
- :$app_key => Dandelionapi.config.app_key
18
- )
19
- conn = Faraday.new(url: Dandelionapi.config.endpoint) do |faraday|
20
- faraday.request :url_encoded
21
- faraday.adapter Faraday.default_adapter
10
+ def call(endpoint, params)
11
+ begin
12
+ params = params.merge(
13
+ :token => Dandelionapi.config.token
14
+ )
15
+ conn = Faraday.new(url: Dandelionapi.config.endpoint) do |faraday|
16
+ faraday.request :url_encoded
17
+ faraday.adapter Faraday.default_adapter
18
+ end
19
+ response = conn.post "#{Dandelionapi.config.path}#{endpoint}", params
20
+ JSON.parse response.body
21
+ rescue Exception => e
22
+ raise Dandelionapi::BadResponse
22
23
  end
23
- response = conn.post endpoint, params
24
- JSON.parse response.body
25
- rescue Exception => e
26
- raise Dandelionapi::BadResponse
27
24
  end
25
+
28
26
  end
29
27
 
30
28
  end
31
-
32
29
  end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+
3
+ require "faraday"
4
+ require "faraday_middleware"
5
+ require "json"
6
+
7
+ module Dandelionapi
8
+ module Comparison
9
+
10
+ class Request < Base::Request
11
+
12
+ def compare(options)
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -5,24 +5,86 @@ require "faraday_middleware"
5
5
  require "json"
6
6
 
7
7
  module Dandelionapi
8
+ module EntityExtraction
9
+
10
+ class Request < Analysis::Request
8
11
 
9
- class EntityExtraction < Base
12
+ ENDPOINT = "/nex/v1"
10
13
 
11
- ENDPOINT = "/datatxt/nex/v1"
14
+ MANDATORY_FIELDS = [
15
+ "text",
16
+ "url",
17
+ "html",
18
+ "html_fragment"
19
+ ]
12
20
 
13
- attr_accessor :text, :url, :html, :html_fragment,
14
- :lang, :min_confidence, :min_length, :social_hashtag,
15
- :social_mention, :include, :extra_types, :country,
16
- :custom_spots
21
+ OPTIONAL_FIELDS = [
22
+ "lang",
23
+ "min_confidence",
24
+ "min_length",
25
+ "social.hashtag",
26
+ "social.mention",
27
+ "include",
28
+ "extra_types",
29
+ "country",
30
+ "custom_spots"
31
+ ]
17
32
 
18
- def analyze(options)
33
+ FIELDS_FORMAT = {
34
+ "text" => {
35
+ valid: lambda {|value| value.is_a? String},
36
+ error_message: 'text needs to be a String'
37
+ },
38
+ "url" => {
39
+ valid: lambda {|value| value.is_a? String},
40
+ error_message: 'url needs to be a String'
41
+ },
42
+ "html" => {
43
+ valid: lambda {|value| value.is_a? String},
44
+ error_message: 'html needs to be a String'
45
+ },
46
+ "html_fragment" => {
47
+ valid: lambda {|value| value.is_a? String},
48
+ error_message: 'html_fragment needs to be a String'
49
+ },
50
+ "lang" => {
51
+ valid: lambda {|value| (value.is_a? String and ["de", "en", "fr", "it", "pt", "auto"].include? value)},
52
+ error_message: 'lang needs to be one of the following values: "de", "en", "fr", "it", "pt", "auto"'
53
+ },
54
+ "min_confidence" => {
55
+ valid: lambda {|value| (value.is_a? Float and [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0].include? value) },
56
+ error_message: 'wrong format'
57
+ },
58
+ "min_length" => {
59
+ valid: lambda {|value| (value.is_a? Integer and value >= 2) },
60
+ error_message: 'wrong format'
61
+ },
62
+ "social.hashtag" => {
63
+ valid: lambda {|value| (value.is_a? TrueClass or value.is_a? FalseClass)},
64
+ error_message: 'wrong format'
65
+ },
66
+ "social.mention" => {
67
+ valid: lambda {|value| (value.is_a? TrueClass or value.is_a? FalseClass)},
68
+ error_message: 'wrong format'
69
+ },
70
+ "include" => {
71
+ valid: lambda {|values| (values.is_a? Array and values.all?{|value|["types", "categories", "abstract", "image", "lod", "alternate_labels"].include? value})},
72
+ error_message: 'wrong format'
73
+ },
74
+ "extra_types" => {
75
+ valid: lambda {|values| (values.is_a? Array and values.all?{|value|["phone", "vat"].include? value})},
76
+ error_message: 'wrong format'
77
+ },
78
+ "country" => {
79
+ valid: lambda {|value| ["AD", "AE", "AM", "AO", "AQ", "AR", "AU", "BB", "BR", "BS", "BY", "CA", "CH", "CL", "CN", "CX", "DE", "FR", "GB", "HU", "IT", "JP", "KR", "MX", "NZ", "PG", "PL", "RE", "SE", "SG", "US", "YT", "ZW"].include? value},
80
+ error_message: 'wrong format'
81
+ },
82
+ "custom_spots" => {
83
+ valid: lambda {|value| value.is_a? String},
84
+ error_message: 'custom_spots needs to be a String'
85
+ }
86
+ }
19
87
 
20
- raise MissingParameters.new("Please provide one of the following parameters: text, url, html or html_fragment") if ([:text, :url, :html, :html_fragment] & options.keys).empty?
21
-
22
- params = options
23
- call(ENDPOINT, params)
24
88
  end
25
-
26
89
  end
27
-
28
90
  end
@@ -5,21 +5,47 @@ require "faraday_middleware"
5
5
  require "json"
6
6
 
7
7
  module Dandelionapi
8
+ module LanguageDetection
9
+
10
+ class Request < Analysis::Request
11
+
12
+ ENDPOINT = "/li/v1"
13
+
14
+ MANDATORY_FIELDS = [
15
+ "text",
16
+ "url",
17
+ "html",
18
+ "html_fragment"
19
+ ]
20
+
21
+ OPTIONAL_FIELDS = [
22
+ "clean"
23
+ ]
24
+
25
+ FIELDS_FORMAT = {
26
+ "text" => {
27
+ valid: lambda {|value| value.is_a? String},
28
+ error_message: 'text needs to be String'
29
+ },
30
+ "url" => {
31
+ valid: lambda {|value| value.is_a? String},
32
+ error_message: 'url needs to be String'
33
+ },
34
+ "html" => {
35
+ valid: lambda {|value| value.is_a? String},
36
+ error_message: 'html needs to be String'
37
+ },
38
+ "html_fragment" => {
39
+ valid: lambda {|value| value.is_a? String},
40
+ error_message: 'html_fragment needs to be String'
41
+ },
42
+ "clean" => {
43
+ valid: lambda {|value| (value.is_a? TrueClass or value.is_a? FalseClass)},
44
+ error_message: 'clean needs to be Boolean'
45
+ },
46
+ }
8
47
 
9
- class LanguageDetection < Base
10
-
11
- ENDPOINT = "/datatxt/li/v1"
12
-
13
- attr_accessor :text, :url, :html, :html_fragment, :clean
14
-
15
- def analyze(options)
16
-
17
- raise MissingParameters.new("Please provide one of the following parameters: text, url, html or html_fragment") if ([:text, :url, :html, :html_fragment] & options.keys).empty?
18
-
19
- params = options
20
- call(ENDPOINT, params)
21
48
  end
22
49
 
23
50
  end
24
-
25
51
  end
@@ -6,18 +6,46 @@ require "json"
6
6
 
7
7
  module Dandelionapi
8
8
 
9
- class SentimentAnalysis < Base
9
+ module SentimentAnalysis
10
+
11
+ class Request < Analysis::Request
12
+
13
+ ENDPOINT = "/sent/v1"
14
+
15
+ MANDATORY_FIELDS = [
16
+ "text",
17
+ "url",
18
+ "html",
19
+ "html_fragment"
20
+ ]
21
+
22
+ OPTIONAL_FIELDS = [
23
+ "lang"
24
+ ]
25
+
26
+ FIELDS_FORMAT = {
27
+ "text" => {
28
+ valid: lambda {|value| value.is_a? String},
29
+ error_message: 'text needs to be String'
30
+ },
31
+ "url" => {
32
+ valid: lambda {|value| value.is_a? String},
33
+ error_message: 'url needs to be String'
34
+ },
35
+ "html" => {
36
+ valid: lambda {|value| value.is_a? String},
37
+ error_message: 'html needs to be String'
38
+ },
39
+ "html_fragment" => {
40
+ valid: lambda {|value| value.is_a? String},
41
+ error_message: 'html_fragment needs to be String'
42
+ },
43
+ "lang" => {
44
+ valid: lambda {|value| (value.is_a? String and ["de", "en", "fr", "it", "pt", "auto"].include? value)},
45
+ error_message: 'lang needs to be one of the following values: "de", "en", "fr", "it", "pt", "auto"'
46
+ }
47
+ }
10
48
 
11
- ENDPOINT = "/datatxt/sent/v1"
12
-
13
- attr_accessor :text, :url, :html, :html_fragment, :lang
14
-
15
- def analyze(options)
16
-
17
- raise MissingParameters.new("Please provide one of the following parameters: text, url, html or html_fragment") if ([:text, :url, :html, :html_fragment] & options.keys).empty?
18
-
19
- params = options
20
- call(ENDPOINT, params)
21
49
  end
22
50
 
23
51
  end
@@ -6,17 +6,37 @@ require "json"
6
6
 
7
7
  module Dandelionapi
8
8
 
9
- class TextSimilarity < Base
9
+ class TextSimilarity
10
10
 
11
- ENDPOINT = "/datatxt/sim/v1"
11
+ class Request < Comparison::Request
12
12
 
13
- attr_accessor :text1, :url1, :html1, :html_fragment1, :text2, :url2, :html2, :html_fragment2, :lang, :bow
13
+ ENDPOINT = "/sim/v1"
14
14
 
15
- def analyze(options)
16
- params = options
17
- call(ENDPOINT, params)
18
- end
15
+ MANDATORY_FIELDS_1 = [
16
+ "text1",
17
+ "url1",
18
+ "html1",
19
+ "html_fragment1"
20
+ ]
21
+
22
+ MANDATORY_FIELDS_2 = [
23
+ "text2",
24
+ "url2",
25
+ "html2",
26
+ "html_fragment2"
27
+ ]
28
+
29
+ OPTIONAL_FIELDS = [
30
+ "lang",
31
+ "bow"
32
+ ]
19
33
 
34
+ def compare(options)
35
+ params = options
36
+ call(ENDPOINT, params)
37
+ end
38
+
39
+ end
20
40
  end
21
41
 
22
42
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Dandelionapi
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.5"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dandelionapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrea Mostosi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-06 00:00:00.000000000 Z
11
+ date: 2017-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -182,7 +182,9 @@ files:
182
182
  - bin/setup
183
183
  - dandelionapi.gemspec
184
184
  - lib/dandelionapi.rb
185
+ - lib/dandelionapi/analysis.rb
185
186
  - lib/dandelionapi/base.rb
187
+ - lib/dandelionapi/comparison.rb
186
188
  - lib/dandelionapi/entity_extraction.rb
187
189
  - lib/dandelionapi/language_detection.rb
188
190
  - lib/dandelionapi/sentiment_analysis.rb
@@ -209,9 +211,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
209
211
  version: '0'
210
212
  requirements: []
211
213
  rubyforge_project:
212
- rubygems_version: 2.4.6
214
+ rubygems_version: 2.5.1
213
215
  signing_key:
214
216
  specification_version: 4
215
217
  summary: Ruby Gem for Dandelion API service
216
218
  test_files: []
217
- has_rdoc: