require 'net/http' module FlyAdmin class PageCache attr_accessor :rails_cache EXPIRATION_TIMEOUT = 10.minutes def initialize @rails_cache = ActiveSupport::Cache::MemoryStore.new end # Get footers like this # footer_wap_ru # Get pages (unsubscribe etc) like this # page_mt_en def read(elem) text = @rails_cache.read(elem) expired_in = @rails_cache.read("#{elem}_expired_an") unless text text = check_footer(elem) # Write to Rails cache like # elem = content_wap_ru write_with_date(elem, text) else if Time.now > expired_in new_text = check_footer(elem) unless new_text.empty? @rails_cache.clear write_with_date(elem, new_text) return new_text end end end text end # After saving footer, clear cache and check if from imbs source(now - from db) def clear_element(elem) @rails_cache.delete_matched(elem) new_text = check_footer(elem) write_with_date(elem, new_text) end def write_with_date(elem, text) write(elem, text) write("#{elem}_expired_an", Time.now + EXPIRATION_TIMEOUT) end def write(name, text) @rails_cache.write(name, text) end def clear @rails_cache.clear end def check_footer(elem) name, paysite, country = elem.split '_' footer = FlyAdmin::Footer .joins(:country, :paysite) .where('fly_admin_countries.name = ? and fly_admin_paysites.name = ?', country, paysite).first # get footer from db , else get it content from url like this www.bornpay.com/footer/hsSDAasd.wap.ru.html FlyAdmin.api_type == 'bornpay' ? get_footer_from_bornpay(footer, name, paysite, country) : get_footer_from_imbs(footer, name, paysite, country) end def get_footer_from_bornpay(footer, name, paysite, country) if footer && footer.local? # Get from db footer.send(name) else # Load from bornpay footer_from_request = http_load(name, paysite, country) # Save bornpay footer to db save_footer_to_db(footer_from_request, name, paysite, country) return footer_from_request end end def get_footer_from_imbs(footer, name, paysite, country) if footer # Get from db footer.send(name) else # Empty footer "" end end def http_load(name, paysite, country) name = (name == 'content') ? 'footer' : name service = SiteConfig['service_code'] link = "/#{name}/#{service}.#{paysite}.#{country}.html" uri = URI("http://www.bornpay.com#{link}") res = Net::HTTP.get_response(uri) message = "#{res.code} , message: #{res.message}, url: http://bornpay.com#{link}" API_LOG.info "GET FlyAdmin::Footer : #{message} " if res.code == '200' && res.message == 'OK' res.body.force_encoding("UTF-8") else "" end end def save_footer_to_db(footer_text, name, paysite, country) if footer_text.present? API_LOG.info "footer :#{footer_text}, name: #{name}, paysite: #{paysite}, country: #{country}" country = Country.find_by_name(country) paysite = Paysite.find_by_name(paysite) case name when 'content' attributes = { 'content' => footer_text} when 'unsubscription' attributes = { 'unsubscription' => footer_text } when 'offer' attributes = { 'offer' => footer_text } when 'terms' attributes = { 'terms' => footer_text } end attributes.merge!('source_type' => FlyAdmin.api_type) footer = FlyAdmin::Footer.find_or_create_by(country: country, paysite: paysite) footer.assign_attributes(attributes) footer.source_type = FlyAdmin.api_type footer.save! end end end end