开发者

KRL and Yahoo Local Search

开发者 https://www.devze.com 2023-02-01 00:14 出处:网络
I\'m trying to use Yahoo Local Search in a Kynetx Application. ruleset avogadro { meta { name \"yahoo-local-ruleset\"

I'm trying to use Yahoo Local Search in a Kynetx Application.

ruleset avogadro {
  meta {
    name "yahoo-local-ruleset"
    description "use results from Yahoo local search"
    author "randall bohn"
    key yahoo_local "get-your-own-key"
  }
  dispatch { domain "example.com"}
  global {
    datasource local:XML <- "http://local.yahooapis.com/LocalSea开发者_如何学PythonrchService/V3/localsearch";
  }

  rule add_list {
    select when pageview ".*" setting ()
    pre {
      ds = datasource:local("?appid=#{keys:yahoo_local()}&query=pizza&zip=#{zip}&results=5");
      rs = ds.pick("$..Result");
    }
    append("body","<ul id='my_list'></ul>");
    always {
      set ent:pizza rs;
    }
  }

  rule add_results {
    select when pageview ".*" setting ()
    foreach ent:pizza setting pizza
    pre {
      title = pizza.pick("$..Title");
    }
    append("#my_list", "<li>#{title}</li>");
  }
}

The list I wind up with is

. [object Object]

and 'title' has

{'$t' => 'Pizza Shop 1'}

I can't figure out how to get just the title. It looks like the 'text content' from the original XML file turns into {'$t' => 'text content'} and the '$t' give problems to pick().


When XML datasources and datasets get converted into JSON, the text value within an XML node gets assigned to $t. You can pick the text of the title by changing your pick statement in the pre block to

title = pizza.pick("$..Title.$t");

Try that and see if that solves your problem.


Side notes on things not related to your question to consider:

1) Thank you for sharing the entire ruleset, what problem you were seeing and what you expected. Made answering your question much easier.

2) The ruleset identifier should not be changed from what AppBuilder or the command-line gem generate for you. Your identifier that is currently

ruleset avogadro {

should look something more like

ruleset a60x304 {

3) You don't need the

setting ()

in the select statement unless you have a capture group in your regular expression


Turns out that pick("$..Title.$t") does work. It looks funny but it works. Less funny than a clown hat I guess.

name = pizza.pick("$..Title.$t");
city = pizza.pick("$..City.$t");
phone = pizza.pick("$..Phone.$t");
list_item = "<li>#{name}/#{city} #{phone}</li>"

Wish I had some pizza right now!

0

精彩评论

暂无评论...
验证码 换一张
取 消