Class: Tilda::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/tilda/page.rb

Instance Method Summary collapse

Constructor Details

#initialize(id, only_body: nil) ⇒ Page

Returns a new instance of Page.



3
4
5
6
7
8
# 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

Instance Method Details

#fetch_page(only_body) ⇒ Object



14
15
16
17
18
19
20
21
22
# 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_filesObject



59
60
61
# File 'lib/tilda/page.rb', line 59

def get_css_files
  get_css_struct.map { |struct| struct[:to] }
end

#get_css_structObject



43
44
45
# File 'lib/tilda/page.rb', line 43

def get_css_struct
  get_current_page.dig(:css)
end

#get_current_pageObject



10
11
12
# File 'lib/tilda/page.rb', line 10

def get_current_page
  @current_page ||= fetch_page(@only_body).dig(:result)
end

#get_html_codeObject



24
25
26
# File 'lib/tilda/page.rb', line 24

def get_html_code
  get_current_page.dig(:html)
end

#get_html_filenameObject



28
29
30
# 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) ⇒ Object



32
33
34
35
36
37
# 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_filesObject



55
56
57
# File 'lib/tilda/page.rb', line 55

def get_img_files
  get_img_struct.map { |struct| struct[:to] }
end

#get_img_structObject



51
52
53
# File 'lib/tilda/page.rb', line 51

def get_img_struct
  get_current_page.dig(:images)
end

#get_js_filesObject



63
64
65
# File 'lib/tilda/page.rb', line 63

def get_js_files
  get_js_struct.map { |struct| struct[:to] }
end

#get_js_structObject



47
48
49
# File 'lib/tilda/page.rb', line 47

def get_js_struct
  get_current_page.dig(:js)
end

#get_published_timestampObject



39
40
41
# File 'lib/tilda/page.rb', line 39

def get_published_timestamp
  get_current_page.dig(:published)
end

#save(path) ⇒ Object



84
85
86
87
88
89
# 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) ⇒ Object



71
72
73
# 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) ⇒ Object



79
80
81
82
# 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) ⇒ Object



75
76
77
# 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) ⇒ Object



67
68
69
# File 'lib/tilda/page.rb', line 67

def save_js(path)
  download_files(get_js_struct, path) unless get_js_struct.nil?
end