class DistributedPress
Distributed Press (distributed.press) API client
Constants
- BOUNDARY
- NEWLINE
Attributes
api_key[R]
project_domain[R]
Public Class Methods
new(url: 'https://api.distributed.press', api_key: nil, project_domain: nil)
click to toggle source
@param options [Hash] @option options [String] :url API URL @option options [String] :api_key API Key @option options [String] :project_domain Domain name
# File lib/distributed_press.rb 19 def initialize(url: 'https://api.distributed.press', api_key: nil, project_domain: nil) 20 @api_key = api_key || ENV['DISTRIBUTED_PRESS_API_KEY'] 21 @project_domain = project_domain || ENV['DISTRIBUTED_PRESS_PROJECT_DOMAIN'] 22 23 raise ArgumentError, "API key is missing" unless @api_key 24 raise ArgumentError, "Project domain is missing" unless @project_domain 25 26 self.class.default_options[:base_uri] = HTTParty.normalize_base_uri(url) 27 end
Public Instance Methods
configure()
click to toggle source
Configure the domain
@return [Boolean] Configuration was successful
# File lib/distributed_press.rb 32 def configure 33 stream, config = IO.pipe 34 35 Thread.new do 36 write_multipart_header config, 'config.json' 37 IO.copy_stream StringIO.new({ 'domain' => project_domain }.to_json), config 38 write_multipart_footer config 39 40 config.close 41 end 42 43 result = upload_io('/v0/publication/configure', stream) 44 45 result['errorCode'].zero? 46 end
publish(path)
click to toggle source
Publishes a directory by tarballing it on the fly.
@param [String] Directory path @return [Boolean] Upload was successful
# File lib/distributed_press.rb 52 def publish(path) 53 raise ArgumentError, "#{path} is not a directory" unless File.directory? path 54 55 filename = File.basename path 56 57 Open3.popen2('tar', '--to-stdout', '--create', '--gzip', '--dereference', '--directory', path, '.') do |stdin, stdout, thread| 58 stream, body = IO.pipe 59 60 # Generate a multipart body and copy contents to it. 61 Thread.new do 62 write_multipart_header body, filename 63 IO.copy_stream stdout, body 64 write_multipart_footer body 65 66 body.close 67 end 68 69 result = upload_io '/v0/publication/publish', stream 70 71 # wait 72 thread.value 73 result['errorCode'].zero? 74 end 75 end
Private Instance Methods
boundary()
click to toggle source
Generate a MultiPart boundary and reuse it
@return [String]
# File lib/distributed_press.rb 119 def boundary 120 @boundary ||= HTTParty::Request::MultipartBoundary.generate 121 end
headers(extra = {})
click to toggle source
Default request headers
@param [Hash] Extra headers @return [Hash]
# File lib/distributed_press.rb 127 def headers(extra = {}) 128 extra.merge('Accept' => 'application/json', 'Authorization' => 'Bearer ' + api_key) 129 end
upload_io(endpoint, io)
click to toggle source
Upload an IO object
@param [String] @param [IO]
# File lib/distributed_press.rb 108 def upload_io(endpoint, io) 109 self.class.post(endpoint, body_stream: io, headers: headers( 110 'Accept' => 'application/json', 111 'Content-Type' => "multipart/form-data; boundary=#{boundary}", 112 'Transfer-Encoding' => 'chunked' 113 )) 114 end
write_multipart_header(io, filename)
click to toggle source
Write the multipart header
# File lib/distributed_press.rb 80 def write_multipart_header(io, filename) 81 io.write BOUNDARY 82 io.write boundary 83 io.write NEWLINE 84 io.write "Content-Disposition: form-data; name=\"file\"; filename=\"#{filename}.tar.gz\"" 85 io.write NEWLINE 86 io.write 'Content-Type: application/octet-stream' 87 io.write NEWLINE 88 io.write NEWLINE 89 90 nil 91 end