Class: PufferPages::Backends::Origin

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/puffer_pages/backends/models/origin.rb

Direct Known Subclasses

Origin

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.export_jsonObject



38
39
40
41
42
43
# File 'lib/puffer_pages/backends/models/origin.rb', line 38

def self.export_json
  %w(layouts snippets pages).each_with_object({}) do |table, result|
    klass = "puffer_pages/#{table}".classify.constantize
    result[table] = klass.export_json
  end.as_json.to_json
end

Instance Method Details

#import!Object



18
19
20
# File 'lib/puffer_pages/backends/models/origin.rb', line 18

def import!
  import_json import_data
end

#import_dataObject



22
23
24
25
26
# File 'lib/puffer_pages/backends/models/origin.rb', line 22

def import_data
  Net::HTTP.get(uri)
rescue
  raise PufferPages::ImportFailed.new("Could not connect to `#{name}` (#{uri})")
end

#import_json(json) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/puffer_pages/backends/models/origin.rb', line 28

def import_json json
  data = json.is_a?(String) ? ActiveSupport::JSON.decode(json) : json
  ActiveRecord::Base.transaction do
    %w(layouts snippets pages).each do |table|
      klass = "puffer_pages/#{table}".classify.constantize
      klass.import_json data[table]
    end
  end
end

#pathObject



10
11
12
# File 'lib/puffer_pages/backends/models/origin.rb', line 10

def path
  read_attribute(:path).presence || PufferPages.export_path
end

#uriObject



14
15
16
# File 'lib/puffer_pages/backends/models/origin.rb', line 14

def uri
  parsed_host.merge URI::Generic.build(:path => path, :query => { :token => token }.to_query)
end