Class: DSTK::DSTK
- Inherits:
-
Object
- Object
- DSTK::DSTK
- Defined in:
- lib/dstk.rb
Instance Method Summary collapse
- #check_version ⇒ Object
- #coordinates2politics(coordinates) ⇒ Object
- #coordinates2statistics(coordinates, statistics = nil) ⇒ Object
- #file2text(inputfile) ⇒ Object
- #geocode(address) ⇒ Object
- #html2story(html) ⇒ Object
- #html2text(html) ⇒ Object
-
#initialize(options = {}) ⇒ DSTK
constructor
A new instance of DSTK.
- #ip2coordinates(ips) ⇒ Object
- #json_api_call(endpoint, arguments = {}, data_payload = nil, data_payload_type = 'json') ⇒ Object
- #street2coordinates(addresses) ⇒ Object
- #text2people(text) ⇒ Object
- #text2places(text) ⇒ Object
- #text2sentences(text) ⇒ Object
- #text2sentiment(text) ⇒ Object
- #text2times(text) ⇒ Object
-
#u(str) ⇒ Object
A short-hand method to URL encode a string.
Constructor Details
#initialize(options = {}) ⇒ DSTK
Returns a new instance of DSTK.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dstk.rb', line 13 def initialize( = {}) = { :api_base => 'http://www.datasciencetoolkit.org', :check_version => true, } if ENV['DSTK_API_BASE'] [:api_base] = ENV['DSTK_API_BASE'] end .each do |key, value| if !.has_key?(key) [key] = value end end @api_base = [:api_base] if [:check_version] self.check_version() end end |
Instance Method Details
#check_version ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/dstk.rb', line 80 def check_version required_version = 50 response = json_api_call('/info') actual_version = response['version'] if actual_version < required_version raise "DSTK: Version #{actual_version.to_s} found but #{required_version.to_s} is required" end end |
#coordinates2politics(coordinates) ⇒ Object
107 108 109 110 |
# File 'lib/dstk.rb', line 107 def coordinates2politics(coordinates) response = json_api_call('/coordinates2politics', {}, coordinates) response end |
#coordinates2statistics(coordinates, statistics = nil) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/dstk.rb', line 152 def coordinates2statistics(coordinates, statistics = nil) if statistics if !statistics.is_a?(Array) then statistics = [statistics] end arguments = { 'statistics' => statistics.join(',') } else arguments = {} end response = json_api_call('/coordinates2statistics', arguments, coordinates) response end |
#file2text(inputfile) ⇒ Object
117 118 119 120 |
# File 'lib/dstk.rb', line 117 def file2text(inputfile) response = json_api_call('/text2places', {}, {:inputfile => inputfile}, 'file') response end |
#geocode(address) ⇒ Object
101 102 103 104 105 |
# File 'lib/dstk.rb', line 101 def geocode(address) arguments = { 'address' => address } response = json_api_call('/maps/api/geocode/json', arguments) response end |
#html2story(html) ⇒ Object
132 133 134 135 |
# File 'lib/dstk.rb', line 132 def html2story(html) response = json_api_call('/html2story', {}, html, 'string') response end |
#html2text(html) ⇒ Object
127 128 129 130 |
# File 'lib/dstk.rb', line 127 def html2text(html) response = json_api_call('/html2text', {}, html, 'string') response end |
#ip2coordinates(ips) ⇒ Object
89 90 91 92 93 |
# File 'lib/dstk.rb', line 89 def ip2coordinates(ips) if !ips.is_a?(Array) then ips = [ips] end response = json_api_call('/ip2coordinates', {}, ips) response end |
#json_api_call(endpoint, arguments = {}, data_payload = nil, data_payload_type = 'json') ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/dstk.rb', line 41 def json_api_call(endpoint, arguments = {}, data_payload = nil, data_payload_type = 'json') api_url = @api_base + endpoint arguments_list = arguments.map do |name, value| name + '=' + URI.encode(value) end if arguments_list.length > 0 arguments_string = '?' + arguments_list.join('&') api_url += arguments_string end response = nil if !data_payload response = HTTParty.get(api_url) else if data_payload_type == 'json' data_payload_value = data_payload.to_json else data_payload_value = data_payload end response = HTTParty.post(api_url, { :body => data_payload_value }) end if !response.body or response.code != 200 raise "DSTK::json_api_call('#{endpoint}', #{arguments.to_json}, '#{data_payload}', '#{data_payload_type}') call to '#{api_url}' failed with code #{response.code} : '#{response.}'" end json_string = response.body result = nil begin result = JSON.parse(json_string) rescue JSON::ParseError => e 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.}" end if !result.is_a?(Array) and result['error'] raise result['error'] end result end |
#street2coordinates(addresses) ⇒ Object
95 96 97 98 99 |
# File 'lib/dstk.rb', line 95 def street2coordinates(addresses) if !addresses.is_a?(Array) then addresses = [addresses] end response = json_api_call('/street2coordinates', {}, addresses) response end |
#text2people(text) ⇒ Object
137 138 139 140 |
# File 'lib/dstk.rb', line 137 def text2people(text) response = json_api_call('/text2people', {}, text, 'string') response end |
#text2places(text) ⇒ Object
112 113 114 115 |
# File 'lib/dstk.rb', line 112 def text2places(text) response = json_api_call('/text2places', {}, text, 'string') response end |
#text2sentences(text) ⇒ Object
122 123 124 125 |
# File 'lib/dstk.rb', line 122 def text2sentences(text) response = json_api_call('/text2sentences', {}, text, 'string') response end |
#text2sentiment(text) ⇒ Object
147 148 149 150 |
# File 'lib/dstk.rb', line 147 def text2sentiment(text) response = json_api_call('/text2sentiment', {}, text, 'string') response end |
#text2times(text) ⇒ Object
142 143 144 145 |
# File 'lib/dstk.rb', line 142 def text2times(text) response = json_api_call('/text2times', {}, text, 'string') response end |
#u(str) ⇒ Object
A short-hand method to URL encode a string. See web.elctech.com/?p=58
37 38 39 |
# File 'lib/dstk.rb', line 37 def u(str) str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) } end |