Class: DistributedPress

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/distributed_press.rb,
lib/distributed_press/v1.rb,
lib/distributed_press/version.rb,
lib/distributed_press/v1/token.rb,
lib/distributed_press/multipart.rb,
lib/distributed_press/v1/client.rb,
lib/distributed_press/v1/errors.rb,
lib/distributed_press/v1/social.rb,
lib/distributed_press/v1/schemas.rb,
lib/distributed_press/v1/client/auth.rb,
lib/distributed_press/v1/client/site.rb,
lib/distributed_press/v1/social/hook.rb,
lib/distributed_press/v1/client/admin.rb,
lib/distributed_press/v1/schemas/site.rb,
lib/distributed_press/v1/social/inbox.rb,
lib/distributed_press/v1/social/likes.rb,
lib/distributed_press/v1/schemas/admin.rb,
lib/distributed_press/v1/social/client.rb,
lib/distributed_press/v1/social/outbox.rb,
lib/distributed_press/v1/social/shares.rb,
lib/distributed_press/v1/social/replies.rb,
lib/distributed_press/v1/client/publisher.rb,
lib/distributed_press/v1/schemas/new_site.rb,
lib/distributed_press/v1/social/allowlist.rb,
lib/distributed_press/v1/social/blocklist.rb,
lib/distributed_press/v1/social/followers.rb,
lib/distributed_press/v1/social/reference.rb,
lib/distributed_press/v1/schemas/new_admin.rb,
lib/distributed_press/v1/schemas/publisher.rb,
lib/distributed_press/v1/social/collection.rb,
lib/distributed_press/v1/schemas/update_site.rb,
lib/distributed_press/v1/social/dereferencer.rb,
lib/distributed_press/v1/schemas/token_header.rb,
lib/distributed_press/v1/schemas/http_protocol.rb,
lib/distributed_press/v1/schemas/ipfs_protocol.rb,
lib/distributed_press/v1/schemas/new_publisher.rb,
lib/distributed_press/v1/schemas/token_payload.rb,
lib/distributed_press/v1/social/signed_headers.rb,
lib/distributed_press/v1/schemas/hyper_protocol.rb,
lib/distributed_press/v1/social/schemas/webhook.rb,
lib/distributed_press/v1/schemas/publishing_site.rb,
lib/distributed_press/v1/social/referenced_object.rb,
lib/distributed_press/v1/schemas/new_token_payload.rb,
lib/distributed_press/v1/schemas/bittorrent_protocol.rb

Overview

API client

Defined Under Namespace

Modules: V1 Classes: Multipart

Constant Summary collapse

DEFAULT_URL =

Default URL

'https://api.distributed.press'
VERSION =

Version

'0.5.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: DEFAULT_URL, api_key: nil, project_domain: nil) ⇒ DistributedPress

Returns a new instance of DistributedPress.

Parameters:

  • url (String) (defaults to: DEFAULT_URL)

    API URL

  • api_key (String) (defaults to: nil)

    API Key

  • project_domain (String) (defaults to: nil)

    Domain name

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
# File 'lib/distributed_press.rb', line 19

def initialize(url: DEFAULT_URL, api_key: nil, project_domain: nil)
  url ||= DEFAULT_URL
  @api_key = api_key || ENV['DISTRIBUTED_PRESS_API_KEY']
  @project_domain = project_domain || ENV['DISTRIBUTED_PRESS_PROJECT_DOMAIN']

  raise ArgumentError, 'API key is missing' unless @api_key
  raise ArgumentError, 'Project domain is missing' unless @project_domain

  self.class.default_options[:base_uri] = HTTParty.normalize_base_uri(url)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



14
15
16
# File 'lib/distributed_press.rb', line 14

def api_key
  @api_key
end

#configure_resultObject (readonly)

Returns the value of attribute configure_result.



14
15
16
# File 'lib/distributed_press.rb', line 14

def configure_result
  @configure_result
end

#project_domainObject (readonly)

Returns the value of attribute project_domain.



14
15
16
# File 'lib/distributed_press.rb', line 14

def project_domain
  @project_domain
end

#publish_resultObject (readonly)

Returns the value of attribute publish_result.



14
15
16
# File 'lib/distributed_press.rb', line 14

def publish_result
  @publish_result
end

Instance Method Details

#configureBoolean

Configure the domain

Returns:

  • (Boolean)

    Configuration was successful



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/distributed_press.rb', line 33

def configure
  stream, config = IO.pipe
  multipart = Multipart.new

  Thread.new do
    multipart.write_header config, 'config.json'
    IO.copy_stream StringIO.new({ 'domain' => project_domain }.to_json), config
    multipart.write_footer config

    config.close
  end

  @configure_result = upload_io('/v0/publication/configure', stream, multipart.boundary)

  configure_result['errorCode'].zero?
end

#publish(path) ⇒ Boolean

Publishes a directory by tarballing it on the fly.

Parameters:

  • path (String)

    Directory path

Returns:

  • (Boolean)

    Upload was successful

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/distributed_press.rb', line 54

def publish(path)
  raise ArgumentError, "#{path} is not a directory" unless File.directory? path

  filename = File.basename path

  Open3.popen2('tar', '--to-stdout', '--create', '--gzip', '--dereference', '--directory', path,
               '.') do |_stdin, stdout, thread|
    stream, body = IO.pipe

    multipart = Multipart.new

    # Generate a multipart body and copy contents to it.
    Thread.new do
      multipart.write_header body, "#{filename}.tar.gz"
      IO.copy_stream stdout, body
      multipart.write_footer body

      body.close
    end

    @publish_result = upload_io '/v0/publication/publish', stream, multipart.boundary

    # wait
    thread.value
    publish_result['errorCode'].zero?
  end
end