class ReadOnce

Constants

BASE_URI
USER_AGENT_HEADER

Attributes

key[RW]

Public Class Methods

exit_if_gem_outdated!() click to toggle source
# File lib/readonce.rb, line 11
def self.exit_if_gem_outdated!
  response = get '/minimum-ruby-client-version', headers: USER_AGENT_HEADER
  major, minor, patch = response.body.split('.').map{|m| m.to_i}

  if Readonce::MAJOR_VERSION < major || Readonce::MINOR_VERSION < minor || Readonce::PATCH_VERSION < patch
    required_version = "#{major}.#{minor}.#{patch}"
    puts "ERROR: Gem version is #{Readonce::VERSION}, but #{required_version} or later is now required."
    puts 'Please run: gem install readonce'
    exit
  end
end
from_data(data) click to toggle source
# File lib/readonce.rb, line 29
def self.from_data(data)
  self.exit_if_gem_outdated!
  response = post '/create', body: data, headers: {'Content-type' => 'text/plain'}.merge(USER_AGENT_HEADER)
  r = ReadOnce.new
  r.instance_variable_set(:@key, response.body)
  r
end
from_key(key) click to toggle source
# File lib/readonce.rb, line 23
def self.from_key(key)
  r = ReadOnce.new
  r.instance_variable_set(:@key, key)
  r
end

Public Instance Methods

block_until_read() click to toggle source
# File lib/readonce.rb, line 59
def block_until_read
  until read? do
    sleep 1
  end
end
exists?() click to toggle source
# File lib/readonce.rb, line 47
def exists?
  self.class.exit_if_gem_outdated!
  response = self.class.get "/status/#{key}", headers: USER_AGENT_HEADER
  response.code == 200
end
read?() click to toggle source
# File lib/readonce.rb, line 41
def read?
  self.class.exit_if_gem_outdated!
  response = self.class.get "/status/#{key}", headers: USER_AGENT_HEADER
  !JSON.parse(response.body)['accessed_at'].nil?
end
read_url() click to toggle source
# File lib/readonce.rb, line 37
def read_url
  "#{BASE_URI}/#{key}"
end
status() click to toggle source
# File lib/readonce.rb, line 53
def status
  self.class.exit_if_gem_outdated!
  response = self.class.get "/status/#{key}", headers: USER_AGENT_HEADER
  JSON.parse(response.body)
end