Warum erhalte ich die Fehlermeldung „Syntaxfehler, unerwartete tCONSTANT, Schlüsselwort_ende wird erwartet“?

StackOverflow https://stackoverflow.com//questions/25005800

Frage

Ich möchte den Preis eines bestimmten Produkts in einem bestimmten Webshop überprüfen.

Ich verwende eine Konstante, um einen Hash von Webshop-Daten zu speichern, damit die Bearbeitung einfacher ist (weitere Shops werden hinzugefügt).

Hier ist der Code, den ich verwende:

require 'httparty'
require 'nokogiri'

class Prijscheckr

  STORES = {
    :zara => {
      'base_uri' => 'http://www.zara.com/nl/',
      'normal_price_css' => 'p.price > span',
      'css_normal_price_extract' => "[0].attr('data-price')",
      'normal_price_xpath' => '/p[3]/span',
      'xpath_normal_price_extract' => "[0].attr('data-price')"
    }
  }

  def begin(args = {})
    page = Nokogiri::HTML(HTTParty.get(args[:url]))

    price = page.css(STORES[:zara]['normal_price_css'])STORES[:zara]['css_normal_price_extract']
  end
end

Beim Tun

p = Prijscheckr.new

p.begin(url: 'http://www.zara.com/nl/nl/collectie-aw14/dames/jacks/leren-bikerjack-c269184p2137577.html')

Hier sind die Ergebnisse:

    # Works
    # price = page.css('p.price > span')[0].attr('data-price')

    # Works
    # price = page.css(STORES[:zara]['normal_price_css'])[0].attr('data-price')

    # Does not work
    # price = page.css(STORES[:zara]['normal_price_css'])STORES[:zara]['css_normal_price_extract']

Wie kann ich verketten price = page.css(STORES[:zara]['normal_price_css'])STORES[:zara]['css_normal_price_extract'] ohne es in der Methode hart zu codieren?

War es hilfreich?

Lösung

Ruby-Code kann nicht durch Verkettung von Zeichenfolgen erstellt werden.Vielleicht möchten Sie es erklären css_normal_price_extract als Lamba

'css_normal_price_extract' => ->(val) {val[0].attr('data-price')}

price = STORES[:zara]['css_normal_price_extract'].call(page.css(STORES[:zara]['normal_price_css']))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top