Class: Smooster::Deploy::Page

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Serializers::JSON
Defined in:
lib/smooster/deploy/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject

extend Smooster::Deploy::Pages



8
9
10
# File 'lib/smooster/deploy/page.rb', line 8

def filename
  @filename
end

#site_template_idObject

extend Smooster::Deploy::Pages



8
9
10
# File 'lib/smooster/deploy/page.rb', line 8

def site_template_id
  @site_template_id
end

#smo_idObject

extend Smooster::Deploy::Pages



8
9
10
# File 'lib/smooster/deploy/page.rb', line 8

def smo_id
  @smo_id
end

#titleObject

extend Smooster::Deploy::Pages



8
9
10
# File 'lib/smooster/deploy/page.rb', line 8

def title
  @title
end

#urlObject

extend Smooster::Deploy::Pages



8
9
10
# File 'lib/smooster/deploy/page.rb', line 8

def url
  @url
end

Instance Method Details

#attributesObject



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

def attributes
    {'title' => title, 'filename' => filename, 'site_template_id' => site_template_id, 'smo_id' => smo_id, 'url' => url}
end

#load_smo_idObject



18
19
20
# File 'lib/smooster/deploy/page.rb', line 18

def load_smo_id
  store.transaction { store[self.site_template_id][:smo_id] if store[self.site_template_id].present? }
end

#sanitize_filename(filename) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/smooster/deploy/page.rb', line 52

def sanitize_filename(filename)
  # Split the name when finding a period which is preceded by some
  # character, and is followed by some character other than a period,
  # if there is no following period that is followed by something
  # other than a period (yeah, confusing, I know)
  fn = filename.split /(?<=.)\.(?=[^.])(?!.*\.[^.])/m

  # We now have one or two parts (depending on whether we could find
  # a suitable period). For each of these parts, replace any unwanted
  # sequence of characters with an underscore
  fn.map! { |s| s.gsub /[^a-z0-9\-]+/i, '-' }

  # Finally, join the parts with a period and return the result
  return fn.join '.'
end

#saveObject



46
47
48
49
50
# File 'lib/smooster/deploy/page.rb', line 46

def save
  store.transaction do
    store[self.site_template_id] = {:smo_id => self.smo_id}
  end
end

#storeObject



14
15
16
# File 'lib/smooster/deploy/page.rb', line 14

def store
  Smooster::Application.instance.pages_store
end

#uploadObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/smooster/deploy/page.rb', line 26

def upload
  #return false if self.checksum.to_s == self.load_checksum.to_s

  if self.smo_id.present?
    #response = RestClient.put "#{Smooster::Application.instance.api_url()}/#{Smooster::Application.instance.site_id()}/site_templates/#{self.smo_id}", {:site_template => {:body => self.body, :title => self.title}}, {"Authorization" => 'Token token="'+ Smooster::Application.instance.api_key() +'"', "Accept" => 'application/vnd.smoosterid.v2'}
  else
    begin
      response = RestClient.post "#{Smooster::Application.instance.api_url()}/#{Smooster::Application.instance.site_id()}/folders/root/pages", {:page => {:title => self.title, :filename => self.filename, :site_template_id => self.site_template_id, :folder_id => "root"}}, {"Authorization" => 'Token token="'+ Smooster::Application.instance.api_key() +'"', "Accept" => 'application/vnd.smoosterid.v2'}
      data = JSON.parse(response)
      self.url = data['url']
      self.smo_id = data['smo_id']
      self.save
    rescue => e
      Smooster::Application.instance.logger.error "Error in page create for #{self.title} / #{self.filename}!: #{e} / #{e.backtrace.inspect}"
      puts "This page failed to create: #{self.title} / #{self.filename}".colorize(:red)
    end
  end

end