2016-03-14 86 views
0

我正在编程Phoenix书,我的Wolfram服务没有按预期工作。编程Phoenix Wolfram Alpha服务返回`[]`而不是结果元组

当我运行在iex -S mix以下命令:

Rumbl.InfoSys.compute("what is the meaning of life?") 

它返回:

[] 

的预期值是这样的:

[%Rumbl.InfoSys.Result{​backend:​ %Rumbl.User{...}, ​score:​ 95, 
​text:​ ​"​​42\n(according to the book The Hitchhiker"​, ​url:​ nil}] 

这里是lib/rumbl/info_sys/wolfram.ex内容

defmodule Rumbl.InfoSys.Wolfram do 
    import SweetXml 
    alias Rumbl.InfoSys.Result 

    def start_link(query, query_ref, owner, limit) do 
    Task.start_link(__MODULE__, :fetch, [query, query_ref, owner, limit]) 
    end 

    def fetch(query_str, query_ref, owner, _limit) do 
    query_str 
    |> fetch_xml() 
    |> xpath(~x"/queryresult/pod[contains(@title, 'Result') or 
           contains(@title, 'Definitions')] 
          /subpod/plaintext/text()") 
    |> send_results(query_ref, owner) 
    end 

    defp send_results(nil, query_ref, owner) do 
    send(owner, {:results, query_ref, []}) 
    end 

    defp send_results(answer, query_ref, owner) do 
    results = [%Result{backend: "wolfram", score: 95, text: to_string(answer)}] 
    send(owner, {:results, query_ref, results}) 
    end 

    defp fetch_xml(query_str) do 
    {:ok, {_, _, body}} = :httpc.request(
     String.to_char_list("http://api.wolframalpha.com/v2/query" <> 
     "?appid=#{app_id()}" <> 
     "&input={URI.encode(query_str)}&format=plaintext")) 
    body 
    end 

    defp app_id, do: Application.get_env(:rumbl, :wolfram)[:app_id] 
end 

...和lib/rumbl/info_sys/supervisor.ex

defmodule Rumbl.InfoSys.Supervisor do 
    use Supervisor 

    def start_link() do 
    Supervisor.start_link(__MODULE__, [], name: __MODULE__) 
    end 

    def init(_opts) do 
    children = [ 
     worker(Rumbl.InfoSys, [], restart: :temporary) 
    ] 

    supervise children, strategy: :simple_one_for_one 
    end 
end 

为什么我没有得到在iex正确的返回值?任何帮助表示赞赏 - 请让我知道,如果你想看到任何其他文件。

+1

是否当你卷曲或Wget的发送相同的查询工作? – Amiramix

+0

如何使用curl或wget发出请求? –

回答

0

{URI.encode(query_str)}应该是#{URI.encode(query_str)}