Class: DistributedPress::V1::Client::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/distributed_press/v1/client/site.rb

Overview

Sites can be created and updated to set distribution options, and a tarball with their contents can be sent to Distributed Press.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Site

Returns a new instance of Site.

Parameters:



22
23
24
# File 'lib/distributed_press/v1/client/site.rb', line 22

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientDistributedPress::V1::Client (readonly)

Client instance



19
20
21
# File 'lib/distributed_press/v1/client/site.rb', line 19

def client
  @client
end

Instance Method Details

#create(schema) ⇒ DistributedPress::V1::Schemas::Site

Creates a website



40
41
42
43
44
45
# File 'lib/distributed_press/v1/client/site.rb', line 40

def create(schema)
  validate_schema! schema
  validate_capabilities!

  Schemas::Site.new.call(**client.post(endpoint: '/v1/sites', schema: schema))
end

#delete(schema) ⇒ Boolean

Deletes a website

Parameters:

Returns:

  • (Boolean)


96
97
98
99
100
101
# File 'lib/distributed_press/v1/client/site.rb', line 96

def delete(schema)
  validate_schema! schema
  validate_capabilities!

  client.delete(endpoint: "/v1/sites/#{schema[:id]}")
end

#publish(schema, path) ⇒ Bool

Publish changes to a website

Parameters:

Returns:

  • (Bool)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/distributed_press/v1/client/site.rb', line 63

def publish(schema, path)
  validate_schema! schema
  validate_capabilities!
  responses = []

  Open3.popen2('tar', '--to-stdout', '--create', '--gzip',
               '--dereference', '--exclude="*.gz"',
               '--exclude="*.br"', '--directory', path,
               '.') do |_, stdout, thread|
    stream, body = IO.pipe
    multipart = Multipart.new

    Thread.new do
      multipart.write_header body, 'site.tar.gz'
      IO.copy_stream stdout, body
      multipart.write_footer body

      body.close
    end

    responses << client.put(endpoint: "/v1/sites/#{schema[:id]}", io: stream, boundary: multipart.boundary)

    # wait for tar to finish
    responses << thread.value
  end

  responses.all?(&:success?)
end

#show(schema) ⇒ Object

Retrieves information about a site



29
30
31
32
33
34
# File 'lib/distributed_press/v1/client/site.rb', line 29

def show(schema)
  validate_schema! schema
  validate_capabilities!

  Schemas::Site.new.call(**client.get(endpoint: "/v1/sites/#{schema[:id]}"))
end

#update(schema) ⇒ DistributedPress::V1::Schemas::Site

Updates a website configuration



51
52
53
54
55
56
# File 'lib/distributed_press/v1/client/site.rb', line 51

def update(schema)
  validate_schema! schema
  validate_capabilities!

  Schemas::Site.new.call(**client.post(endpoint: "/v1/sites/#{schema[:id]}", schema: schema))
end