Class: FlyAdmin::PageCache

Inherits:
Object
  • Object
show all
Defined in:
lib/fly_admin/page_cache.rb

Constant Summary collapse

EXPIRATION_TIMEOUT =
10.minutes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePageCache

Returns a new instance of PageCache.



8
9
10
# File 'lib/fly_admin/page_cache.rb', line 8

def initialize
  @rails_cache = ActiveSupport::Cache::MemoryStore.new
end

Instance Attribute Details

#rails_cacheObject

Returns the value of attribute rails_cache.



4
5
6
# File 'lib/fly_admin/page_cache.rb', line 4

def rails_cache
  @rails_cache
end

Instance Method Details



57
58
59
60
61
62
63
64
65
# File 'lib/fly_admin/page_cache.rb', line 57

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

#clearObject



53
54
55
# File 'lib/fly_admin/page_cache.rb', line 53

def clear
  @rails_cache.clear
end

#clear_element(elem) ⇒ Object

After saving footer, clear cache and check if from imbs source(now - from db)



38
39
40
41
42
# File 'lib/fly_admin/page_cache.rb', line 38

def clear_element(elem)
  @rails_cache.delete_matched(elem)
  new_text = check_footer(elem)
  write_with_date(elem, new_text)
end


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fly_admin/page_cache.rb', line 66

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


79
80
81
82
83
84
85
86
87
# File 'lib/fly_admin/page_cache.rb', line 79

def get_footer_from_imbs(footer, name, paysite, country)
  if footer
    # Get from db
    footer.send(name)
  else
    # Empty footer 
    ""
  end
end

#http_load(name, paysite, country) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fly_admin/page_cache.rb', line 89

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

#read(elem) ⇒ Object

Get footers like this footer_wap_ru Get pages (unsubscribe etc) like this page_mt_en



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fly_admin/page_cache.rb', line 15

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


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fly_admin/page_cache.rb', line 104

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

#write(name, text) ⇒ Object



49
50
51
# File 'lib/fly_admin/page_cache.rb', line 49

def write(name, text)
  @rails_cache.write(name, text)
end

#write_with_date(elem, text) ⇒ Object



44
45
46
47
# File 'lib/fly_admin/page_cache.rb', line 44

def write_with_date(elem, text)
  write(elem, text)
  write("#{elem}_expired_an", Time.now + EXPIRATION_TIMEOUT)
end