require 'trip_advisor/translation' require 'rest-client' require 'nokogiri' require "active_support/core_ext" module TripAdvisor class TranslationTool module Scraper class << self def translations_from_doc(doc) Array.new.tap do |translations| doc.each { |translation| translations << Translation.new(id: translation["id"], key: translation["key"], note: translation["note"]) } end end def localizations_from_doc(doc) Array.new.tap do |localizations| doc.each { |localization| locale_string = localization["lang"]["locale"] localized_string = localization["displayedValue"] status = localization["isTranslatedValue"] language_name = localization["lang"]["localizedDisplayName"] locale_identifier = localization["lang"]["locale"] localizations << Translation::Localization.new(locale_identifier: locale_identifier, language_name: language_name, string: localized_string, status: status, ) } end end end end class ResultsPaginator attr_reader :base_url, :offset, :total, :per_page, :params, :translations def initialize(base_url, attributes = {}) @base_url = base_url @total = 0 @per_page = 50 @offset = attributes[:offset] || 0 @params = attributes[:params] end def load(options = {}) @offset = options[:offset] if options[:offset] @offset = ((options[:page] - 1) * per_page) if options[:page] raise ArgumentError, "Cannot load a negative offset" if @offset < 0 raise ArgumentError, "Cannot load an offset greater than the total number of objects" if total && @offset > total response = RestClient.post(base_url, params.to_json, :content_type => :json, :accept => :json) doc = JSON.parse(response) @total = doc.count @per_page = doc.count @translations = Scraper.translations_from_doc(doc) end def page_count (total / per_page.to_f).ceil end def loaded? @translations end def paginate load unless loaded? 1.upto(page_count).collect do |page_number| load(page: page_number).tap do |translations| yield page_number, translations if block_given? end end.flatten end alias_method :all, :paginate def current_page_index offset / per_page end def next_page load(page: current_page_index + 1) end def previous_page load(page: current_page_index - 1) end end attr_accessor :username, :password, :locale def initialize(attributes = {}) attributes.each { |k, v| self.send("#{k}=", v) } @locale ||= 'en' end def search(query, &block) ResultsPaginator.new("https://#{username}:#{password}@localization.tripadvisor.com/translations/search/keys", params: { lang: locale, keyName: query }).tap do |paginator| if block_given? paginator.paginate(&block) else paginator.load end end end def get_translation(translation) translation = Translation.new(id: translation) if translation.is_a?(Integer) response = RestClient.get("https://#{username}:#{password}@localization.tripadvisor.com/translations/keys/id/#{translation.id}/translations") translation.localizations = Scraper.localizations_from_doc(JSON.parse(response)) translation end end end