class WinewooCore::Services::ElasticSearch::Parsers::NewsParser

Public Class Methods

parse(raw_data, parse_error) click to toggle source
# File lib/winewoo_core/services/elastic_search/parsers/news_parser.rb, line 7
def parse(raw_data, parse_error)
  if parse_error
    render_error_message
  else
    render_data(raw_data)
  end
end

Private Class Methods

render_data(raw_data) click to toggle source
# File lib/winewoo_core/services/elastic_search/parsers/news_parser.rb, line 19
def render_data(raw_data)
  raw_data.map do |raw_entry|
    tweet = raw_entry["_source"]
    WinewooCore::Models::NewsEntry.new.tap do |entry|
      entry.type = tweet["type"] || :tweet
      entry.text = tweet["text"]
      entry.wine_id = tweet["wine_id"]
      entry.vintage_id = tweet["vintage_id"]
      entry.created_at = Time.parse(tweet["created_at"]).to_i
      entry.user = serialize_tweet_author(tweet["user"])
      entry.media = tweet["media"]
    end
  end
end
render_error_message() click to toggle source
# File lib/winewoo_core/services/elastic_search/parsers/news_parser.rb, line 35
def render_error_message
  [
    WinewooCore::Models::NewsEntry.new.tap do |entry|
      entry.type = :tweet
      entry.text = "Suite à un problème serveur, nous sommes dans l'incapacité de délivrer nos services. Nous mettons tout en oeuvre pour y répondre dans les plus bref délais. Merci de votre compréhension."
      entry.created_at = Time.now.to_i
      entry.user = winewoo_author
    end
  ]
end
serialize_tweet_author(author) click to toggle source
# File lib/winewoo_core/services/elastic_search/parsers/news_parser.rb, line 53
def serialize_tweet_author(author)
  if author
    {
      twitter_id: author["id"],
      name: author["name"],
      profile_img: author["profile_image_url"],
      big_profile_img: author["profile_image_url"].gsub('normal', 'bigger')
    }
  else
    winewoo_author
  end
end
serialize_tweet_media(medias) click to toggle source
# File lib/winewoo_core/services/elastic_search/parsers/news_parser.rb, line 47
def serialize_tweet_media(medias)
  media = medias && medias.first
  media && { url: media["display_url"] }
end
winewoo_author() click to toggle source
# File lib/winewoo_core/services/elastic_search/parsers/news_parser.rb, line 67
def winewoo_author
  {
    name: "winewoo",
    profile_img: "http://api.winewoo.com/images/default.png",
    big_profile_img: "http://api.winewoo.com/images/default.png"
  }
end