Sha256: 95dc4dcd7dedb968dd301938f18bc0c3240a086b301fce1a01a398bc2d421f9c

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

module Smooster
  module Deploy
    class SiteTemplate
      include ActiveModel::Model
      include ActiveModel::Serializers::JSON
      extend Smooster::Deploy::SiteTemplates

      attr_accessor  :title, :body, :path, :checksum, :smo_id

      def attributes
          {'title' => title, 'body' => body, 'path' => path, 'checksum' => checksum, 'smo_id' => smo_id}
      end

      def store
        Smooster::Application.instance.site_templates_store
      end

      def load_checksum
        store.transaction { store[self.short_path][:checksum] if store[self.short_path].present? }
      end

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

      def short_path
        self.path.gsub("#{Smooster::Application.instance.base_dir}/#{Smooster::Application.instance.html_folder()}","")
      end

      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
          response = RestClient.post "#{Smooster::Application.instance.api_url()}/#{Smooster::Application.instance.site_id()}/site_templates", {:body => self.body, :title => self.title}, {"Authorization" => 'Token token="'+ Smooster::Application.instance.api_key() +'"', "Accept" => 'application/vnd.smoosterid.v2'}
        end

        data = JSON.parse(response)
        self.smo_id = data['id']
        self.save
      end

      def save
        store.transaction do
          store[self.short_path] = {:checksum => self.checksum.to_s, :smo_id => self.smo_id}
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smooster-0.1.7 lib/smooster/deploy/site_template.rb