wolfram-alpha 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'cgi'
4
- require 'open-uri'
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.2"
19
+ Version = "0.3"
20
20
 
21
21
  # The API request-uri.
22
- RequestURI = "http://api.wolframalpha.com/v2/query?input=%s&appid=%s"
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 developer key, which is set upon initialization.
7
- attr_accessor :application_key
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] application_key The developers API-key that can be easily
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 application_key = nil, options = {}
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 value of user input, and return a new response.
27
+ # Compute the result of +input+, and return a new response.
28
28
  #
29
- # @param [String] query The users input query.
29
+ # @param [String] input The input query.
30
30
  #
31
31
  # @return [Response] The parsed response object.
32
- def compute query
33
- document = Nokogiri::XML open request_url(query)
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] query The users input query.
48
+ # @param [String] input The input query.
48
49
  #
49
50
  # @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]
51
+ def request_url input
52
+ query = { "appid" => @token, "input" => "#{input}" }.merge @options
53
53
 
54
- if @options[:timeout]
55
- formatted_url << "&timeout=#{@options[:timeout]}"
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 array of sub-pods in this pod.
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
- @subpods = element.xpath("subpod").map { |element| Subpod.new self, element }
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 = nil
14
- @document = document
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
- # Map and join the result pod's subpods as a printable string.
35
- def result_as_text
36
- result.subpods.map(&:text).join ', '
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
- private
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
- # Find a pod of which id is +id+.
42
- def find_pod_with_id id
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
- # A subpod is a container for a type of output, such as plain text,
5
- # numbers, images, etc.
6
- class Subpod
7
- # This is the pod that this subpod belongs to.
8
- attr_accessor :pod
9
-
10
- # Construct a new subpod.
11
- def initialize pod, element
12
- @pod, @element = pod, element
13
- end
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.
21
- def text
22
- @element.xpath("plaintext").text
23
- end
24
- end
25
- end
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.2'
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: 2012-03-22 00:00:00.000000000 Z
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: &22778340 !ruby/object:Gem::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: *22778340
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  description:
26
- email: mk@maero.dk
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
- homepage:
37
- licenses: []
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: 1.8.11
58
+ rubygems_version: 2.0.0
57
59
  signing_key:
58
- specification_version: 3
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.