class Tilda::Page

Public Class Methods

new(id, only_body: nil) click to toggle source
# File lib/tilda/page.rb, line 3
def initialize(id, only_body: nil)
  raise 'Page id must be set when creating class' if id.nil?
  @page_id = id
  @only_body = only_body
  @current_page = nil
end

Public Instance Methods

fetch_page(only_body) click to toggle source
# File lib/tilda/page.rb, line 14
def fetch_page(only_body)
  if only_body.nil?
    page = Client.new.get_page_full_export(@page_id)
  else
    page = Client.new.get_page_export(@page_id)
  end
  raise "Page not found" if page[:status] != 'FOUND' #TODO: replace with Error class
  return page
end
get_css_files() click to toggle source
# File lib/tilda/page.rb, line 59
def get_css_files
  get_css_struct.map { |struct| struct[:to] }
end
get_css_struct() click to toggle source
# File lib/tilda/page.rb, line 43
def get_css_struct
  get_current_page.dig(:css)
end
get_current_page() click to toggle source
# File lib/tilda/page.rb, line 10
def get_current_page
  @current_page ||= fetch_page(@only_body).dig(:result)
end
get_html_code() click to toggle source
# File lib/tilda/page.rb, line 24
def get_html_code
  get_current_page.dig(:html)
end
get_html_filename() click to toggle source
# File lib/tilda/page.rb, line 28
def get_html_filename
  get_current_page.dig(:filename)
end
get_html_meta_tags(files_url_path = nil) click to toggle source
# File lib/tilda/page.rb, line 32
def get_html_meta_tags(files_url_path = nil)
  tags_list = ''
  get_css_files.each {|filename| tags_list << "<link rel=\"stylesheet\" href=\"#{files_url_path}#{filename}\" type=\"text/css\" media=\"all\"/>\n" }
  get_js_files.each {|filename| tags_list << "<script src=\"#{files_url_path}#{filename}\"></script>\n" }
  return tags_list
end
get_img_files() click to toggle source
# File lib/tilda/page.rb, line 55
def get_img_files
  get_img_struct.map { |struct| struct[:to] }
end
get_img_struct() click to toggle source
# File lib/tilda/page.rb, line 51
def get_img_struct
  get_current_page.dig(:images)
end
get_js_files() click to toggle source
# File lib/tilda/page.rb, line 63
def get_js_files
  get_js_struct.map { |struct| struct[:to] }
end
get_js_struct() click to toggle source
# File lib/tilda/page.rb, line 47
def get_js_struct
  get_current_page.dig(:js)
end
get_published_timestamp() click to toggle source
# File lib/tilda/page.rb, line 39
def get_published_timestamp
  get_current_page.dig(:published)
end
save(path) click to toggle source
# File lib/tilda/page.rb, line 84
def save(path)
  save_js(path)
  save_css(path)
  save_img(path)
  save_html(path)
end
save_css(path) click to toggle source
# File lib/tilda/page.rb, line 71
def save_css(path)
  download_files(get_css_struct, path) unless get_css_struct.nil?
end
save_html(path) click to toggle source
# File lib/tilda/page.rb, line 79
def save_html(path)
  full_name = "#{path}/#{get_html_filename}"
  File.open(full_name, "w") { |file| file.write(get_html_code) }
end
save_img(path) click to toggle source
# File lib/tilda/page.rb, line 75
def save_img(path)
  download_files(get_img_struct, path) unless get_img_struct.nil?
end
save_js(path) click to toggle source
# File lib/tilda/page.rb, line 67
def save_js(path)
  download_files(get_js_struct, path) unless get_js_struct.nil?
end

Private Instance Methods

download_file(url, full_name) click to toggle source
# File lib/tilda/page.rb, line 93
def download_file(url, full_name)
  File.open(full_name, "w") do |file|
    HTTParty.get(url, stream_body: true) { |fragment| file.write(fragment) }
  end
end
download_files(url_name_list, path) click to toggle source
# File lib/tilda/page.rb, line 99
def download_files(url_name_list, path)
  return if url_name_list.empty?
  url_name_list.each do |file_info|
    download_file(file_info[:from], "#{path}/#{file_info[:to]}")
  end
end