wolfram-alpha 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/library/wolfram-alpha.rb +3 -3
- data/library/wolfram-alpha/client.rb +16 -18
- data/library/wolfram-alpha/pod.rb +47 -7
- data/library/wolfram-alpha/response.rb +13 -30
- data/library/wolfram-alpha/subpod.rb +28 -25
- metadata +19 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 00652f21bcb2490794e8336b9358369c1229251d
|
4
|
+
data.tar.gz: fe54230b01c9511646c62a9daea873de63d13dde
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3fdf8b6086f5ed6b3057425aaa6bc94ac9b0edc13fd21a31c75b55df983db14299b605763d1aac331481c15d7e059013f002bab290b1a3be137d5b5afed21371
|
7
|
+
data.tar.gz: ea2c7ec6cc2de5926d27d97863fb37aa29660bbbca258d33391f96e4a8fa7e5f7f37890f057ffc28a501a360528c020bf5713fc992829203cf97f07d9664d7af
|
data/library/wolfram-alpha.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'cgi'
|
4
|
-
require '
|
4
|
+
require 'net/http'
|
5
5
|
require 'nokogiri'
|
6
6
|
|
7
7
|
require 'wolfram-alpha/pod'
|
@@ -16,8 +16,8 @@ require 'wolfram-alpha/response'
|
|
16
16
|
# @author Mikkel Kroman
|
17
17
|
module WolframAlpha
|
18
18
|
# The current version of the WolframAlpha library.
|
19
|
-
Version = "0.
|
19
|
+
Version = "0.3"
|
20
20
|
|
21
21
|
# The API request-uri.
|
22
|
-
RequestURI = "http://api.wolframalpha.com/v2/query
|
22
|
+
RequestURI = URI "http://api.wolframalpha.com/v2/query"
|
23
23
|
end
|
@@ -1,10 +1,9 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module WolframAlpha
|
4
|
-
# Front object for communicating with the Wolfram|Alpha API.
|
5
4
|
class Client
|
6
|
-
# The Wolfram|Alpha API
|
7
|
-
attr_accessor :
|
5
|
+
# The Wolfram|Alpha API application id, which is set upon initialization.
|
6
|
+
attr_accessor :token
|
8
7
|
|
9
8
|
# The default options for a new client, it is merged with the options
|
10
9
|
# set upon initialization.
|
@@ -14,23 +13,25 @@ module WolframAlpha
|
|
14
13
|
|
15
14
|
# Initialize a new client to communicate with the Wolfram|Alpha API.
|
16
15
|
#
|
17
|
-
# @param [String]
|
16
|
+
# @param [String] token The developers API-id that can be freely
|
18
17
|
# obtained from http://products.wolframalpha.com/api/
|
19
18
|
# @param [Hash] options A set of options that may be put in the request.
|
20
19
|
#
|
21
20
|
# @see DefaultOptions
|
22
|
-
def initialize
|
21
|
+
def initialize token, options = {}
|
22
|
+
@http = Net::HTTP.new RequestURI.host, RequestURI.port
|
23
|
+
@token = token or raise "Invalid token"
|
23
24
|
@options = DefaultOptions.merge options
|
24
|
-
@application_key = application_key
|
25
25
|
end
|
26
26
|
|
27
|
-
# Compute the
|
27
|
+
# Compute the result of +input+, and return a new response.
|
28
28
|
#
|
29
|
-
# @param [String]
|
29
|
+
# @param [String] input The input query.
|
30
30
|
#
|
31
31
|
# @return [Response] The parsed response object.
|
32
|
-
def
|
33
|
-
|
32
|
+
def query input
|
33
|
+
response = @http.get request_url(input)
|
34
|
+
document = Nokogiri::XML response.body
|
34
35
|
|
35
36
|
if document.root.name == "queryresult"
|
36
37
|
Response.new document
|
@@ -44,18 +45,15 @@ module WolframAlpha
|
|
44
45
|
# Mash together the request url, this is where the request-uri is modified
|
45
46
|
# according to the client options.
|
46
47
|
#
|
47
|
-
# @param [String]
|
48
|
+
# @param [String] input The input query.
|
48
49
|
#
|
49
50
|
# @return [String] The complete, formatted uri.
|
50
|
-
def request_url
|
51
|
-
|
52
|
-
formatted_url = WolframAlpha::RequestURI % [escaped_query, @application_key]
|
51
|
+
def request_url input
|
52
|
+
query = { "appid" => @token, "input" => "#{input}" }.merge @options
|
53
53
|
|
54
|
-
|
55
|
-
|
54
|
+
RequestURI.dup.tap do |this|
|
55
|
+
this.query = URI.encode_www_form query
|
56
56
|
end
|
57
|
-
|
58
|
-
return formatted_url
|
59
57
|
end
|
60
58
|
end
|
61
59
|
end
|
@@ -3,19 +3,59 @@
|
|
3
3
|
module WolframAlpha
|
4
4
|
# A wrapper for the pod element.
|
5
5
|
class Pod
|
6
|
-
# @return [Array] an
|
6
|
+
# @return [Array<Subpod>] an list of sub-pods in this pod.
|
7
7
|
attr_reader :subpods
|
8
8
|
|
9
|
-
# Access an attribute on the pod element.
|
10
|
-
def [] attribute
|
11
|
-
@element[attribute]
|
12
|
-
end
|
13
|
-
|
14
9
|
# Construct a new pod with an assigned element, then extract all subpods from it.
|
15
10
|
def initialize element
|
16
11
|
@element = element
|
17
12
|
|
18
|
-
|
13
|
+
extract_subpods
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns whether the pod has any subpods.
|
17
|
+
def subpods?
|
18
|
+
@subpods and @subpods.any?
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns the pod id.
|
22
|
+
#
|
23
|
+
# @return [String] the pod id.
|
24
|
+
def id
|
25
|
+
@element["id"]
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns the pod title.
|
29
|
+
#
|
30
|
+
# @return [String] the pod title.
|
31
|
+
def title
|
32
|
+
@element["title"]
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns the pod scanner.
|
36
|
+
#
|
37
|
+
# @return [String] the pod scanner.
|
38
|
+
def scanner
|
39
|
+
@element["scanner"]
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the pod position.
|
43
|
+
#
|
44
|
+
# @return [Fixnum] the pod position.
|
45
|
+
def position
|
46
|
+
@element["position"].to_i
|
47
|
+
end
|
48
|
+
|
49
|
+
# Inspect the pod.
|
50
|
+
def inspect
|
51
|
+
%{#<#{self.class.name} id: #{id.inspect} title: #{title.inspect} scanner: #{scanner.inspect} position: #{position.inspect} @subpods=#{@subpods.inspect}>}
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
# Extract the subpods.
|
57
|
+
def extract_subpods
|
58
|
+
@subpods = @element.css("subpod").map { |element| Subpod.new self, element }
|
19
59
|
end
|
20
60
|
end
|
21
61
|
end
|
@@ -3,45 +3,28 @@
|
|
3
3
|
module WolframAlpha
|
4
4
|
# This is a response object that wraps the response data in a
|
5
5
|
# easily managable object.
|
6
|
-
#
|
7
|
-
# @see #input_as_text
|
8
|
-
# @see #result_as_text
|
9
6
|
class Response
|
7
|
+
include Enumerable
|
10
8
|
attr_reader :pods
|
11
9
|
|
12
10
|
# Construct a new response for a +document+.
|
13
|
-
def initialize document
|
14
|
-
@
|
15
|
-
|
16
|
-
@pods = @document.xpath("//pod").map {|element| Pod.new element }
|
17
|
-
end
|
18
|
-
|
19
|
-
# Return the pod containing the subpods which is of type +input+.
|
20
|
-
def input
|
21
|
-
find_pod_with_id "Input"
|
22
|
-
end
|
23
|
-
|
24
|
-
# Map and join the input interpreted pod's subpods as a printable string.
|
25
|
-
def input_as_text
|
26
|
-
input.subpods.map(&:text).join ', '
|
27
|
-
end
|
28
|
-
|
29
|
-
# Return the pod containing the subpods which is of type +result+.
|
30
|
-
def result
|
31
|
-
find_pod_with_id "Result"
|
11
|
+
def initialize document
|
12
|
+
@pods = document.css("pod").map { |element| Pod.new element }
|
32
13
|
end
|
33
14
|
|
34
|
-
#
|
35
|
-
def
|
36
|
-
|
15
|
+
# Return the first occurence of a pod with the id of +id+.
|
16
|
+
def [] id
|
17
|
+
find { |pod| pod.id == id }
|
37
18
|
end
|
38
19
|
|
39
|
-
|
20
|
+
# Calls the given block once for each element in self, passing that element as a parameter.
|
21
|
+
#
|
22
|
+
# An Enumerator is returned if no block is given.
|
23
|
+
def each
|
24
|
+
return @pods.to_enum unless block_given?
|
40
25
|
|
41
|
-
|
42
|
-
|
43
|
-
@pods.find do |pod|
|
44
|
-
pod["id"] == id
|
26
|
+
@pods.each do |pod|
|
27
|
+
yield pod
|
45
28
|
end
|
46
29
|
end
|
47
30
|
end
|
@@ -1,25 +1,28 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module WolframAlpha
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module WolframAlpha
|
4
|
+
class Subpod
|
5
|
+
# Construct a new pod with an assigned element.
|
6
|
+
def initialize parent, element
|
7
|
+
@parent = parent
|
8
|
+
@element = element
|
9
|
+
end
|
10
|
+
|
11
|
+
# Returns whether the subpod contains a plaintext element.
|
12
|
+
def plaintext?
|
13
|
+
not plaintext.nil?
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the plaintext element as text.
|
17
|
+
#
|
18
|
+
# @return [String] the plain text.
|
19
|
+
def plaintext
|
20
|
+
@plaintext ||= @element.at_css("plaintext").text
|
21
|
+
end
|
22
|
+
|
23
|
+
# Inspect the subpod.
|
24
|
+
def inspect
|
25
|
+
%{#<#{self.class.name} plaintext: #{plaintext.inspect}>}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,61 +1,63 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wolfram-alpha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
5
|
-
prerelease:
|
4
|
+
version: '0.3'
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Mikkel Kroman
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-06-02 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: nokogiri
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
25
27
|
description:
|
26
|
-
email: mk@
|
28
|
+
email: mk@uplink.io
|
27
29
|
executables: []
|
28
30
|
extensions: []
|
29
31
|
extra_rdoc_files: []
|
30
32
|
files:
|
31
|
-
- library/wolfram-alpha.rb
|
32
33
|
- library/wolfram-alpha/client.rb
|
33
34
|
- library/wolfram-alpha/pod.rb
|
34
35
|
- library/wolfram-alpha/response.rb
|
35
36
|
- library/wolfram-alpha/subpod.rb
|
36
|
-
|
37
|
-
|
37
|
+
- library/wolfram-alpha.rb
|
38
|
+
homepage: https://github.com/mkroman/wolfram-alpha
|
39
|
+
licenses:
|
40
|
+
- Internet Systems Consortium (ISC)
|
41
|
+
metadata: {}
|
38
42
|
post_install_message:
|
39
43
|
rdoc_options: []
|
40
44
|
require_paths:
|
41
45
|
- library
|
42
46
|
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
-
none: false
|
44
47
|
requirements:
|
45
|
-
- -
|
48
|
+
- - '>='
|
46
49
|
- !ruby/object:Gem::Version
|
47
50
|
version: 1.9.1
|
48
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
52
|
requirements:
|
51
|
-
- -
|
53
|
+
- - '>='
|
52
54
|
- !ruby/object:Gem::Version
|
53
55
|
version: '0'
|
54
56
|
requirements: []
|
55
57
|
rubyforge_project:
|
56
|
-
rubygems_version:
|
58
|
+
rubygems_version: 2.0.0
|
57
59
|
signing_key:
|
58
|
-
specification_version:
|
60
|
+
specification_version: 4
|
59
61
|
summary: Wolfram|Alpha introduces a fundamentally new way to get knowledge and answers
|
60
62
|
— not by searching the web, but by doing dynamic computations based on a vast collection
|
61
63
|
of built-in data, algorithms, and methods.
|