Class: Boxcab::PageService

Inherits:
Object
  • Object
show all
Defined in:
lib/boxcab/page_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(api, production = true) ⇒ PageService

Returns a new instance of PageService.



4
5
6
# File 'lib/boxcab/page_service.rb', line 4

def initialize(api, production = true)
  @api, @production = api, production
end

Instance Method Details

#find_or_build(url, content = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/boxcab/page_service.rb', line 8

def find_or_build(url, content = nil)
  path = extract_path(url)
  page_id = Digest::MD5.hexdigest(path)

  page = (if content.blank? && attributes = @api.find(page_id)
    Boxcab::Page.new(attributes)
  else
    Boxcab::Page.build_default(url, path)
  end)

  page.content = content unless content.blank?

  page
end

#production?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/boxcab/page_service.rb', line 34

def production?
  !!@production
end

#save(page) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/boxcab/page_service.rb', line 23

def save(page)
  if !production? || page.read_only?
    :skip
  elsif page.persisted?
    return :unchanged unless page.changed?
    @api.update(page.id, page.to_api) ? :success : :fail
  else
    @api.create(page.to_api) ? :success : :fail
  end
end