krl

KRL and Yahoo Local Search


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/LocalSearchService/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().


Solution

  • 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