wajs
— Wolfram|Alpha via JS
wajs provides JavaScript bindings for the Wolfram|Alpha web-service API. wajs exposes the Wolfram|Alpha web-service API via a small JavaScript library (Node.js environment). The wajs library also offers an interface for querying Wolfram|Alpha, and manipulating query results programmatically without needing to wrangle raw XML.
Created by Clark Feusier
All other language bindings for Wolfram|Alpha can be found here.
Overview
Here is a list of the core features of wajs:
- Full Wolfram|Alpha web-service API coverage
- Simple to use 'out of the box', yet easily configurable
- Predictable promise interface for query
- Powerful utility API on query result
- Access to raw XML at each level of query result
- Access to JSON representation at each level of query result
If you have directly consumed the Wolfram|Alpha web-service API, you might have appreciation for the features listed above. If you haven't yet interacted with the Wolfram|Alpha API, save yourself some pain and use wajs. Installation instructions, example usage, and API reference can be found below.
Installation
wajs is available as an npm package.
* Note: Wolfram|Alpha does not support CORS for serving browser requests. wajs is only available in the Node environment until client-side requests are supported by the Wolfram|Alpha API.
Install module from command-line
npm install wajs
Require or import module for use in desired file
// commonjs requirevar wajs =
// es2015 import
Documentation
Example Usage
// require wajs module -- assuming commonjs/node environmentvar wajs = // the Wolfram|Alpha App Id -- generated in Wolfram|Alpha developer portal// NOTE: copy your app id and set your WA_APP_ID environment variable// from the command-line, as follows:// `export WA_APP_ID='<YOUR APP ID GOES HERE>'`var waAppId = processenvWA_APP_ID // create a client to query the Wolfram|Alpha web-service// NOTE: you can create as many clients as you want, though you only need onevar waClient = waAppId // the only required argument to `<wajs-client>.query` is a query stringvar queryString = 'weather' // maybe this comes from user input // if you want to customize the query further, provide query options// for documentation on query options, please see// the API Reference for `<wajs-client>.query`// NOTE: this is an optional argumentvar queryOptions = format: 'image,plaintext,sound,wav' units: 'metric' // use the client to send a querywaClient
API Reference
wajs
new wajs(appId: string): wajs-client<T>
This is the constructor function for generating a <wajs-client>
.
appId
:- required
- type: string
- Obtain an appId from Wolfram|Alpha
<wajs-client>.query
<wajs-client>.query(input: string, options: object): Promise<T>
This method will query the Wolfram|Alpha web-service API with the provided query.
input
:- required
- type: string
options
:- optional
- type: object
- API Reference for options
// query with string and default optionswaClient
// query with string and custom optionswaClient
options
var queryOptions = // podTitle // repeatable, globable, comma-separated // default: all pod titles podTitle: '' // podIndex // repeatable, globable, comma-separated // default: all pod indices podIndex: '' // format // repeatable, comma-separated // default: 'image,plaintext' // possible options: // image // plaintext // minput // moutput // cell // mathml // imagemap // sound // wav format: '' // assumption // single string -- must match an assumption string from Wolfram|Alpha assumptions assumption: '' // includePodId // list of strings or single string, globable // default: all pod ids includePodId: // '', // excludePodId // list of strings or single string, globable // default: none are excluded excludePodId: // '', // scanner // list of strings or single string, globable // default: all scanners scanner: // '' // async // boolean -- if set to true, results will contain 'async' flags for the // properties that require another request to get content. If you want all the query // results in a single response, do not use the async flag // default: false async: true // ip // string -- should represent the ip address to be used for restricting the query // default: the requesting ip address ip: '' // location // string -- should represent the location to be used for // restricting the query, e.g., 'CA' // default: none location: '' // coordinates // string -- should represent the latitude and longitude pair // to be used for restricting the query, e.g., '40.42,-3.71' // default: none coordinates: '' // podState // string -- should match a pod state string from a Wolfram|Alpha pod // default: none podState: '' // units // string // default: based on requesting location // possible options: // 'metric' // 'nonmetric' units: '' // width // string // default: '500' width: '' // maxWidth // string // default: '500' maxWidth: '' // plotWidth // string // default: '200' plotWidth: '' // magnitude // string // default: '1.0' magnitude: '' // scanTimeout // string // default: '3.0' scanTimeout: '' // podTimeout // string // default: '4.0' podTimeout: '' // parseTimeout // string // default: '5.0' parseTimeout: '' // formatTimeout // string // default: '8.0' formatTimeout: '' // reinterpret // boolean -- if true, and there are no results for a query, Wolfram|Alpha // can switch to a related query for which some results are available // default: false reinterpret: '' // translation // boolean -- if false, auto-translation of simple queries is turned off // default: true translation: '' // ignoreCase // boolean -- if true, case will not be significant for the given query // default: false ignoreCase: '' // sig // string -- used to sign and validate identity of request/response // default: none sig: ''
<query-result>
When the promise, returned by <wajs-client>.query
, resolves, the resolution handler function is provided a <query-result>
instance, with the following API.
<query-result>.succeeded
<query-result>.succeeded(): boolean
This method will return a boolean indicating the success of the query operation (not the success of the overall HTTP request, which is indicated by the query promise resolution).
// e.g.,waClient // outputtrue
<query-result>.failed
<query-result>.failed(): boolean
This method will return a boolean indicating the failure of the query operation (not the failure of the overall HTTP request, which is indicated by the query promise rejection).
// e.g.,waClient // outputfalse
<query-result>.error
<query-result>.error(): { Error, null }
This method will return an Error
object if the query operation failed, and null
otherwise.
// e.g.,waClient // outputnull
<query-result>.numPods
<query-result>.numPods(): number
This method will return the number of pods in the given query result.
// e.g.,waClient // output8
<query-result>.dataTypes
<query-result>.dataTypes(): array<DataType>
This method will return a list of DataType
strings representing the categories and types of data represented in the query result.
// e.g.,waClient // output'MathematicalFunctionIdentity'
<query-result>.toJson
<query-result>.toJson(): json-string
This method will convert the entire query result to JSON.
// e.g.,waClient // output (truncated) "pod":... "assumptions":... "success":"true" "error":"false" "numpods":"8" "datatypes":"MathematicalFunctionIdentity" "timedout":"Numeric" "timedoutpods":"" "timing":"3.4170000000000003" "parsetiming":"0.14" "parsetimedout":"false" "recalculate":"http://www4b.wolframalpha.com/api/v2/recalc.jsp?id=..." "id":"..." "host":"http://www4b.wolframalpha.com" "server":"30" "related":"http://www4b.wolframalpha.com/api/v2/relatedQueries.jsp?id=..." "version":"2.6"
<query-result>.rawXml
<query-result>.rawXml(): xml-string
This method will return the entire original xml query result.
// e.g.,waClient
<!-- output (truncated) --> ... ...
<query-result>.pods
<query-result>.pods(xmlFormat: boolean): array<pod>
This method return a list of pod
objects from the query result.
xmlFormat
:- type: boolean
- optional
- default:
false
- if
true
, the list of pods will be a list of xml string representations of the pods
// e.g., defaultwaClient // output (truncated) subpod: subpod1... title: 'Input' scanner: 'Identity' id: 'Input' position: '100' error: 'false' numsubpods: '1' __parent: ... ... // e.g., xmlFormat is truewaClient // output (truncated) '<pod title="Input" scanner="Identity" id="Input" position="100" error="false" numsubpods="1">...</pod>' ...
<query-result>.assumptions
<query-result>.assumptions(xmlFormat: boolean): array<assumption>
This method return a list of assumption
objects from the query result.
xmlFormat
:- type: boolean
- optional
- default:
false
- if
true
, the list of assumptions will be a list of xml string representations of the assumptions
// e.g., defaultwaClient // output (truncated) value: ... ... ... ... ... ... type: 'Clash' word: 'pi' template: 'Assuming "${word}" is ${desc1}. Use as ${desc2} instead' count: '6' __parent: ... // e.g., xmlFormat is truewaClient // output (truncated) '<assumption type="Clash" word="pi" template="Assuming "${word}" is ${desc1}. Use as ${desc2} instead" count="6">...</assumption>' ...
<query-result>.sources
<query-result>.sources(xmlFormat: boolean): array<source>
This method return a list of source
objects from the query result.
xmlFormat
:- type: boolean
- optional
- default:
false
- if
true
, the list of sources will be a list of xml string representations of each source object
// e.g., defaultwaClient // output (truncated) source: ... ... ... ... count: '4' ... // e.g., xmlFormat is truewaClient // output (truncated) '<sources count="4">...</sources>' ...
<query-result>.warnings
<query-result>.warnings(xmlFormat: boolean): array<warning>
This method return a list of warning
objects from the query result.
xmlFormat
:- type: boolean
- optional
- default:
false
- if
true
, the list of warnings will be a list of xml string representations of each warning object
// e.g., defaultwaClient // output (truncated)undefined // (there are no warnings for this query result) // e.g., xmlFormat is truewaClient // output (truncated)undefined // (there are no warnings for this query result)
<pod>
Each <query-result>
provides a .pods()
method to access the <query-result>
pods, if any. Each <pod>
provides the following API.
<pod>.succeeded
<pod>.succeeded(): boolean
This method returns true if the pod was loaded successfully from Wolfram|Alpha.
// e.g.,waClient // output (truncated)true // this pod was fetched successfully
<pod>.failed
<pod>.failed(): boolean
This method returns true if the pod was not successfully fetched from Wolfram|Alpha.
// e.g.,waClient // output (truncated)false // this pod was fetched successfully
<pod>.getTitle
<pod>.getTitle(): string
This method returns the title of the pod, represented as a string.
// e.g.,waClient // output (truncated)'Example Pod Title'
<pod>.numSubPods
<pod>.numSubPods(): number = 0
This method returns the number of <subPods>
contained by the <pod>
.
// e.g.,waClient // output (truncated)2
<pod>.rawXml
<pod>.rawXml(): xml-string
This method returns the raw XML string representation of the <pod>
.
// e.g.,waClient
<!-- output (truncated) -->...
<pod>.getScanner
<pod>.getScanner(): string
This method returns a string representation of the <pod>'s
scanner.
// e.g.,waClient // output (truncated)'Numeric'
<pod>.getPosition
<pod>.getPosition(): number = 0
This method returns a number representation of the <pod>'s
position in the <query-result>
.
// e.g.,waClient // output (truncated)1
<pod>.asyncEndpoint
<pod>.asyncEndpoint(): string
This method returns a string representation of the <pod>'s
asynchronous url -- the endpoint for asynchronously fetching the <pod>'s
data, assuming the query used async=true
.
// e.g.,waClient // output (truncated)'http://www1.wolframalpha.com/api/v2/asyncPod.jsp?id=MSPa1071c679e3ea1d0h94h00002h3ggc950ehaibf9&s=13'
<pod>.subPods
<pod>.subPods(xmlFormat: boolean): array<subpod>
This method returns a list of <subpod>s
from the pod.
xmlFormat
:- type: boolean
- optional
- default:
false
- if
true
, the list of subpods will be a list of xml string representations of each subpod object
// e.g.,waClient // output (truncated) "title" : "" "img" : "src" : "http:\/\/www4c.wolframalpha.com\/Calculate\/MSP\/MSP4541i6ff0cbeghe4a6a0000248ahgicidag8e12?MSPStoreType=image\/gif&s=61" "alt" : "" "title" : "" "width" : 300 "height" : 56 "plaintext" : "" // e.g., xmlFormat is truewaClient // output (truncated) '<subpod title=''...>...</subpod>'
<pod>.getStates
<pod>.getStates(xmlFormat: boolean): array<state>
This method returns a list of <state>s
from the pod.
xmlFormat
:- type: boolean
- optional
- default:
false
- if
true
, the list of state objects will be a list of xml string representations of each state object
// e.g.,waClient // output (truncated) "name" : "Fraction form" "input" : "ContinuedFraction__Fraction form" // e.g., xmlFormat is truewaClient
// output (truncated)
<pod>.getInfos
<pod>.getInfos(xmlFormat: boolean): array<info>
This method returns a list of <info>s
from the pod.
xmlFormat
:- type: boolean
- optional
- default:
false
- if
true
, the list of info objects will be a list of xml string representations of each info object
// e.g.,waClient // output (truncated) "text" : "(n\nm) is the binomial coefficient" "img" : "src" : "http:\/\/www4b.wolframalpha.com\/Calculate\/MSP\/MSP22591ca2276efe0853cd000045fid320b029578h?MSPStoreType=image\/gif&s=61" "alt" : "(n\nm) is the binomial coefficient" "title" : "(n\nm) is the binomial coefficient" "width" : "204" "height" : "36" "links" : "url" : "http:\/\/reference.wolfram.com\/language\/ref\/Binomial.html" "text" : "Documentation" "title" : "Mathematica" "url" : "http:\/\/functions.wolfram.com\/GammaBetaErf\/Binomial" "text" : "Properties" "title" : "Wolfram Functions Site" "url" : "http:\/\/mathworld.wolfram.com\/BinomialCoefficient.html" "text" : "Definition" "title" : "MathWorld" // e.g., xmlFormat is truewaClient
// output (truncated)
Roadmap
The future of wajs is managed through this repository's Issues — view the roadmap here.
Contributing to wajs
I welcome contributions, but please submit an issue before beginning so that I don't duplicate your work. The development requirements and instructions are below.
Development Requirements
JavaScript Engine:
- node > 0.10.x
Package Manager:
- npm > 2.x.x
Global Packages:
- webpack
Project Package Dependencies:
- bluebird
- cheerio
- popsicle
- traverse
- xml2js
Development Package Dependencies:
- babel-core
- babel-loader
- babel-plugin-transform-runtime
- babel-polyfill
- babel-preset-es2015
- babel-preset-stage-0
- babel-runtime
- watch
- webpack-node-externals
Installing Dependencies
Install Node (bundled with npm) using Homebrew:
brew install node
Install global project dependencies using npm:
npm install -g webpack
Install all local project dependencies using npm:
npm install
Running Tests
After installing the above dependencies, tests can be run using the following command:
npm run test
Building
For a one-off build, run the following command:
npm run build
For a continuous build on changes during development, run the following command:
npm run build:watch
License
MIT LICENSE
wajs -- JavaScript bindings for the Wolfram|Alpha web-service API
Copyright (C) 2016 Clark Feusier cfeusier@gmail.com - All Rights Reserved
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.