wolfram-alpha 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'cgi'
3
4
  require 'open-uri'
4
5
  require 'nokogiri'
5
6
 
@@ -8,10 +9,15 @@ require 'wolfram-alpha/client'
8
9
  require 'wolfram-alpha/subpod'
9
10
  require 'wolfram-alpha/response'
10
11
 
12
+ # Wolfram|Alpha introduces a fundamentally new way to get knowledge and answers—
13
+ # not by searching the web, but by doing dynamic computations based on a vast collection of
14
+ # built-in data, algorithms, and methods.
15
+ #
16
+ # @author Mikkel Kroman
11
17
  module WolframAlpha
12
- RequestURI = "http://api.wolframalpha.com/v2/query?input=%s&appid=%s"
18
+ # The current version of the WolframAlpha library.
19
+ Version = "0.2"
13
20
 
14
- class << Version = [1,0]
15
- def to_s; join '.' end
16
- end
21
+ # The API request-uri.
22
+ RequestURI = "http://api.wolframalpha.com/v2/query?input=%s&appid=%s"
17
23
  end
@@ -1,15 +1,36 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module WolframAlpha
4
+ # Front object for communicating with the Wolfram|Alpha API.
4
5
  class Client
5
- attr_accessor :app_id
6
+ # The Wolfram|Alpha API developer key, which is set upon initialization.
7
+ attr_accessor :application_key
6
8
 
7
- def initialize app_id = nil
8
- @app_id = app_id
9
+ # The default options for a new client, it is merged with the options
10
+ # set upon initialization.
11
+ DefaultOptions = {
12
+ timeout: 1
13
+ }
14
+
15
+ # Initialize a new client to communicate with the Wolfram|Alpha API.
16
+ #
17
+ # @param [String] application_key The developers API-key that can be easily
18
+ # obtained from http://products.wolframalpha.com/api/
19
+ # @param [Hash] options A set of options that may be put in the request.
20
+ #
21
+ # @see DefaultOptions
22
+ def initialize application_key = nil, options = {}
23
+ @options = DefaultOptions.merge options
24
+ @application_key = application_key
9
25
  end
10
26
 
27
+ # Compute the value of user input, and return a new response.
28
+ #
29
+ # @param [String] query The users input query.
30
+ #
31
+ # @return [Response] The parsed response object.
11
32
  def compute query
12
- document = Nokogiri::XML open WolframAlpha::RequestURI % [URI.escape(query), @app_id]#File.open("/home/mk/Documents/response.xml")
33
+ document = Nokogiri::XML open request_url(query)
13
34
 
14
35
  if document.root.name == "queryresult"
15
36
  Response.new document
@@ -17,5 +38,24 @@ module WolframAlpha
17
38
  raise "Invalid response"
18
39
  end
19
40
  end
41
+
42
+ private
43
+
44
+ # Mash together the request url, this is where the request-uri is modified
45
+ # according to the client options.
46
+ #
47
+ # @param [String] query The users input query.
48
+ #
49
+ # @return [String] The complete, formatted uri.
50
+ def request_url query = nil
51
+ escaped_query = CGI.escape query
52
+ formatted_url = WolframAlpha::RequestURI % [escaped_query, @application_key]
53
+
54
+ if @options[:timeout]
55
+ formatted_url << "&timeout=#{@options[:timeout]}"
56
+ end
57
+
58
+ return formatted_url
59
+ end
20
60
  end
21
61
  end
@@ -1,9 +1,17 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module WolframAlpha
4
+ # A wrapper for the pod element.
4
5
  class Pod
6
+ # @return [Array] an array of sub-pods in this pod.
5
7
  attr_reader :subpods
6
8
 
9
+ # Access an attribute on the pod element.
10
+ def [] attribute
11
+ @element[attribute]
12
+ end
13
+
14
+ # Construct a new pod with an assigned element, then extract all subpods from it.
7
15
  def initialize element
8
16
  @element = element
9
17
 
@@ -1,13 +1,48 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module WolframAlpha
4
+ # This is a response object that wraps the response data in a
5
+ # easily managable object.
6
+ #
7
+ # @see #input_as_text
8
+ # @see #result_as_text
4
9
  class Response
5
10
  attr_reader :pods
6
11
 
12
+ # Construct a new response for a +document+.
7
13
  def initialize document = nil
8
14
  @document = document
9
15
 
10
- @pods = @document.xpath("//pod").map { |element| Pod.new element }
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"
32
+ end
33
+
34
+ # Map and join the result pod's subpods as a printable string.
35
+ def result_as_text
36
+ result.subpods.map(&:text).join ', '
37
+ end
38
+
39
+ private
40
+
41
+ # Find a pod of which id is +id+.
42
+ def find_pod_with_id id
43
+ @pods.find do |pod|
44
+ pod["id"] == id
45
+ end
11
46
  end
12
47
  end
13
48
  end
@@ -1,11 +1,23 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module WolframAlpha
4
+ # A subpod is a container for a type of output, such as plain text,
5
+ # numbers, images, etc.
4
6
  class Subpod
7
+ # This is the pod that this subpod belongs to.
8
+ attr_accessor :pod
9
+
10
+ # Construct a new subpod.
5
11
  def initialize pod, element
6
12
  @pod, @element = pod, element
7
13
  end
8
14
 
15
+ # Access an attribute on the subpod element.
16
+ def [] key
17
+ return @element[key]
18
+ end
19
+
20
+ # Get the contents of plaintext as plain text.
9
21
  def text
10
22
  @element.xpath("plaintext").text
11
23
  end
metadata CHANGED
@@ -1,69 +1,63 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: wolfram-alpha
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- version: "0.1"
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ prerelease:
9
6
  platform: ruby
10
- authors:
7
+ authors:
11
8
  - Mikkel Kroman
12
9
  autorequire:
13
- bindir: executables
10
+ bindir: bin
14
11
  cert_chain: []
15
-
16
- date: 2011-01-27 00:00:00 +01:00
17
- default_executable:
18
- dependencies: []
19
-
12
+ date: 2012-03-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &22778340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *22778340
20
25
  description:
21
26
  email: mk@maero.dk
22
27
  executables: []
23
-
24
28
  extensions: []
25
-
26
29
  extra_rdoc_files: []
27
-
28
- files:
30
+ files:
31
+ - library/wolfram-alpha.rb
29
32
  - library/wolfram-alpha/client.rb
30
- - library/wolfram-alpha/response.rb
31
33
  - library/wolfram-alpha/pod.rb
34
+ - library/wolfram-alpha/response.rb
32
35
  - library/wolfram-alpha/subpod.rb
33
- - library/wolfram-alpha.rb
34
- has_rdoc: true
35
36
  homepage:
36
37
  licenses: []
37
-
38
38
  post_install_message:
39
39
  rdoc_options: []
40
-
41
- require_paths:
40
+ require_paths:
42
41
  - library
43
- required_ruby_version: !ruby/object:Gem::Requirement
42
+ required_ruby_version: !ruby/object:Gem::Requirement
44
43
  none: false
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- segments:
49
- - 1
50
- - 9
51
- - 1
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
52
47
  version: 1.9.1
53
- required_rubygems_version: !ruby/object:Gem::Requirement
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
49
  none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- segments:
59
- - 0
60
- version: "0"
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
61
54
  requirements: []
62
-
63
55
  rubyforge_project:
64
- rubygems_version: 1.3.7
56
+ rubygems_version: 1.8.11
65
57
  signing_key:
66
58
  specification_version: 3
67
- summary: Wolfram|Alpha API implementation.
59
+ summary: Wolfram|Alpha introduces a fundamentally new way to get knowledge and answers
60
+ — not by searching the web, but by doing dynamic computations based on a vast collection
61
+ of built-in data, algorithms, and methods.
68
62
  test_files: []
69
-
63
+ has_rdoc: