Class: WolframAlpha::Client

Inherits:
Object
  • Object
show all
Defined in:
library/wolfram-alpha/client.rb

Constant Summary collapse

Options =

The default options for a new client, it is merged with the options set upon initialization.

{
  timeout: 15
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, options = {}) ⇒ Client

Initialize a new client to communicate with the Wolfram|Alpha API.

Parameters:

  • token (String)

    The developers API-id that can be freely obtained from products.wolframalpha.com/api/

  • options (Hash) (defaults to: {})

    A set of options that may be put in the request.

See Also:

  • DefaultOptions


21
22
23
24
25
# File 'library/wolfram-alpha/client.rb', line 21

def initialize token, options = {}
  @http = Net::HTTP.new RequestURI.host, RequestURI.port
  @token = token or raise "Invalid token"
  @options = Options.merge options
end

Instance Attribute Details

#tokenObject

The Wolfram|Alpha API application id, which is set upon initialization.



6
7
8
# File 'library/wolfram-alpha/client.rb', line 6

def token
  @token
end

Instance Method Details

#query(input) ⇒ Response

Compute the result of input, and return a new response.

Parameters:

  • input (String)

    The input query.

Returns:

  • (Response)

    The parsed response object.



32
33
34
35
36
37
38
39
40
41
# File 'library/wolfram-alpha/client.rb', line 32

def query input
  response = @http.get request_url(input).request_uri
  document = Nokogiri::XML response.body

  if document.root.name == "queryresult"
    Response.new document
  else
    raise "Invalid response"
  end
end