dstk 0.50.1
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/lib/dstk.rb +158 -0
- metadata +106 -0
data/lib/dstk.rb
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
# Client interface to the Data Science Toolkit API
|
2
|
+
# See http://www.datasciencetoolkit.org/developerdocs for details
|
3
|
+
# By Pete Warden, pete@petewarden.com
|
4
|
+
|
5
|
+
require 'rubygems' if RUBY_VERSION < '1.9'
|
6
|
+
|
7
|
+
require 'json'
|
8
|
+
require 'httparty'
|
9
|
+
require 'httmultiparty'
|
10
|
+
|
11
|
+
module DSTK
|
12
|
+
class DSTK
|
13
|
+
def initialize(options = {})
|
14
|
+
default_options = {
|
15
|
+
:api_base => 'http://www.datasciencetoolkit.org',
|
16
|
+
:check_version => true,
|
17
|
+
}
|
18
|
+
|
19
|
+
if ENV['DSTK_API_BASE']
|
20
|
+
default_options[:api_base] = ENV['DSTK_API_BASE']
|
21
|
+
end
|
22
|
+
|
23
|
+
default_options.each do |key, value|
|
24
|
+
if !options.has_key?(key)
|
25
|
+
options[key] = value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
@api_base = options[:api_base]
|
30
|
+
|
31
|
+
if options[:check_version]:
|
32
|
+
self.check_version()
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# A short-hand method to URL encode a string. See http://web.elctech.com/?p=58
|
37
|
+
def u(str)
|
38
|
+
str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
|
39
|
+
end
|
40
|
+
|
41
|
+
def json_api_call(endpoint, arguments = {}, data_payload = nil, data_payload_type = 'json')
|
42
|
+
|
43
|
+
api_url = @api_base + endpoint
|
44
|
+
arguments_list = arguments.map do |name, value|
|
45
|
+
name + '=' + u(value)
|
46
|
+
end
|
47
|
+
if arguments_list.length > 0
|
48
|
+
arguments_string = '?' + arguments_list.join('&')
|
49
|
+
api_url += arguments_string
|
50
|
+
end
|
51
|
+
response = nil
|
52
|
+
if !data_payload
|
53
|
+
response = HTTParty.get(api_url)
|
54
|
+
else
|
55
|
+
if data_payload_type == 'json'
|
56
|
+
data_payload_value = data_payload.to_json
|
57
|
+
else
|
58
|
+
data_payload_value = data_payload
|
59
|
+
end
|
60
|
+
response = HTTParty.post(api_url, { :body => data_payload_value })
|
61
|
+
end
|
62
|
+
|
63
|
+
if !response.body or response.code != 200
|
64
|
+
raise "DSTK::json_api_call('#{endpoint}', #{arguments.to_json}, '#{data_payload}', '#{data_payload_type}') call to '#{api_url}' failed with code #{response.code} : '#{response.message}'"
|
65
|
+
end
|
66
|
+
|
67
|
+
json_string = response.body
|
68
|
+
result = nil
|
69
|
+
begin
|
70
|
+
result = JSON.parse(json_string)
|
71
|
+
rescue JSON::ParseError => e
|
72
|
+
raise "DSTK::json_api_call('#{endpoint}', #{arguments.to_json}, '#{data_payload}', '#{data_payload_type}') call to '#{api_url}' failed to parse response '#{json_string}' as JSON - #{e.message}"
|
73
|
+
end
|
74
|
+
if !result.is_a?(Array) and result['error']
|
75
|
+
raise result['error']
|
76
|
+
end
|
77
|
+
result
|
78
|
+
end
|
79
|
+
|
80
|
+
def check_version
|
81
|
+
required_version = 50
|
82
|
+
response = json_api_call('/info')
|
83
|
+
actual_version = response['version']
|
84
|
+
if actual_version < required_version:
|
85
|
+
raise "DSTK: Version #{actual_version.to_s} found but #{required_version.to_s} is required"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def ip2coordinates(ips)
|
90
|
+
if !ips.is_a?(Array) then ips = [ips] end
|
91
|
+
response = json_api_call('/ip2coordinates', {}, ips)
|
92
|
+
response
|
93
|
+
end
|
94
|
+
|
95
|
+
def street2coordinates(addresses)
|
96
|
+
if !addresses.is_a?(Array) then addresses = [addresses] end
|
97
|
+
response = json_api_call('/street2coordinates', {}, addresses)
|
98
|
+
response
|
99
|
+
end
|
100
|
+
|
101
|
+
def coordinates2politics(coordinates)
|
102
|
+
response = json_api_call('/coordinates2politics', {}, coordinates)
|
103
|
+
response
|
104
|
+
end
|
105
|
+
|
106
|
+
def text2places(text)
|
107
|
+
response = json_api_call('/text2places', {}, text, 'string')
|
108
|
+
response
|
109
|
+
end
|
110
|
+
|
111
|
+
def file2text(inputfile)
|
112
|
+
response = json_api_call('/text2places', {}, {:inputfile => inputfile}, 'file')
|
113
|
+
response
|
114
|
+
end
|
115
|
+
|
116
|
+
def text2sentences(text)
|
117
|
+
response = json_api_call('/text2sentences', {}, text, 'string')
|
118
|
+
response
|
119
|
+
end
|
120
|
+
|
121
|
+
def html2text(html)
|
122
|
+
response = json_api_call('/html2text', {}, html, 'string')
|
123
|
+
response
|
124
|
+
end
|
125
|
+
|
126
|
+
def html2story(html)
|
127
|
+
response = json_api_call('/html2story', {}, html, 'string')
|
128
|
+
response
|
129
|
+
end
|
130
|
+
|
131
|
+
def text2people(text)
|
132
|
+
response = json_api_call('/text2people', {}, text, 'string')
|
133
|
+
response
|
134
|
+
end
|
135
|
+
|
136
|
+
def text2times(text)
|
137
|
+
response = json_api_call('/text2times', {}, text, 'string')
|
138
|
+
response
|
139
|
+
end
|
140
|
+
|
141
|
+
def text2sentiment(text)
|
142
|
+
response = json_api_call('/text2sentiment', {}, text, 'string')
|
143
|
+
response
|
144
|
+
end
|
145
|
+
|
146
|
+
def coordinates2statistics(coordinates, statistics = nil)
|
147
|
+
if statistics
|
148
|
+
if !statistics.is_a?(Array) then statistics = [statistics] end
|
149
|
+
arguments = { 'statistics' => statistics.join(',') }
|
150
|
+
else
|
151
|
+
arguments = {}
|
152
|
+
end
|
153
|
+
response = json_api_call('/coordinates2statistics', arguments, coordinates)
|
154
|
+
response
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dstk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 213
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 50
|
9
|
+
- 1
|
10
|
+
version: 0.50.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Pete Warden
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2013-05-14 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: json
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: httparty
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: httmultiparty
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
type: :runtime
|
61
|
+
version_requirements: *id003
|
62
|
+
description: An interface to the datasciencetoolkit.org open API for geocoding addresses, extracting entities and sentiment from unstructured text, and other common semantic and geo data tasks.
|
63
|
+
email: pete@petewarden.com
|
64
|
+
executables: []
|
65
|
+
|
66
|
+
extensions: []
|
67
|
+
|
68
|
+
extra_rdoc_files: []
|
69
|
+
|
70
|
+
files:
|
71
|
+
- lib/dstk.rb
|
72
|
+
homepage: http://rubygems.org/gems/dstk
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.24
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Data Science Toolkit client
|
105
|
+
test_files: []
|
106
|
+
|