class Barcoder::Bucket

Attributes

import_hook[RW]
name[RW]
persisted[RW]
token[RW]

Public Class Methods

all(&block) click to toggle source

Returns an array of all Buckets belonging to the account identified by the account_token (see Barcoder::Config).

# File lib/barcoder/bucket.rb, line 9
def self.all(&block)
  response = Typhoeus::Request.new(buckets_uri).run

  buckets_data = response_to_json(response)

  buckets = buckets_data.map do |data|
    new(data.merge(persisted: true))
  end

  buckets
end
bucket_uri(bucket) click to toggle source
# File lib/barcoder/bucket.rb, line 25
def self.bucket_uri(bucket)
  "#{Barcoder::Config.host}/bucket"
end
buckets_uri() click to toggle source
# File lib/barcoder/bucket.rb, line 21
def self.buckets_uri
  "#{Barcoder::Config.host}/buckets?token=#{Barcoder::Config.account_token}"
end

Public Instance Methods

decode_from_file(filename) click to toggle source
# File lib/barcoder/bucket.rb, line 29
def decode_from_file(filename)
  return false unless token.present?
  Barcoder::PDF.decode_from_file(self, filename)
end
decode_from_string(string) click to toggle source
# File lib/barcoder/bucket.rb, line 34
def decode_from_string(string)
  return false unless token.present?
  Barcoder::PDF.decode_from_string(self, string)
end
method_missing(symbol, *args) click to toggle source
Calls superclass method
# File lib/barcoder/bucket.rb, line 85
def method_missing(symbol, *args)
  # e.g., if users calls queue_unmatched_pages, add unmatched_pages_request
  # to hydra queue
  if symbol.to_s =~ /queue_(.*)/
    request_method = "#{$1}_request"
    Barcoder::Config.hydra.queue send(request_method, *args)
  else
    super
  end
end
partial_imports(use_cache=true) click to toggle source

Fetches partial_imports.

This method is also queueable by calling queue_partial_imports.

# File lib/barcoder/bucket.rb, line 64
def partial_imports(use_cache=true)
  (use_cache && @partial_imports) || begin
    partial_imports_request.run
  end

  @partial_imports
end
recent_imports(since, use_cache=true) click to toggle source

Fetches recent_imports.

This method is also queueable by calling queue_recent_imports.

# File lib/barcoder/bucket.rb, line 53
def recent_imports(since, use_cache=true)
  (use_cache && @recent_imports) || begin
    recent_imports_request(since).run
  end

  @recent_imports
end
save() click to toggle source

Persists the Bucket.

# File lib/barcoder/bucket.rb, line 73
def save
  return false unless valid?

  if !persisted
    create_bucket!
  else
    update_bucket!
  end

  true
end
unmatched_pages(use_cache=true) click to toggle source

Fetches unmatched_pages.

This method is also queueable by calling queue_unmatched_pages.

# File lib/barcoder/bucket.rb, line 42
def unmatched_pages(use_cache=true)
  (use_cache && @unmatched_pages) || begin
    unmatched_pages_request.run
  end

  @unmatched_pages
end

Private Instance Methods

create_bucket!() click to toggle source
# File lib/barcoder/bucket.rb, line 139
def create_bucket!
  opt = {body: save_attr, method: :post}
  response = Typhoeus::Request.new(self.class.buckets_uri, opt).run
  process_save_response response
end
partial_imports_request() click to toggle source
# File lib/barcoder/bucket.rb, line 125
def partial_imports_request
  url = "#{Barcoder::Config.host}/partial_imports"
  params = {token: token}
  request = Typhoeus::Request.new(url, params: params)

  request.on_success do |response|
    json = response_to_json(response)
    @partial_imports = json.map { |j|
      Barcoder::PDF.new(j.with_indifferent_access) }
  end

  request
end
recent_imports_request(since) click to toggle source
# File lib/barcoder/bucket.rb, line 111
def recent_imports_request(since)
  url = "#{Barcoder::Config.host}/recent_imports"
  params = {token: token, since: since}
  request = Typhoeus::Request.new(url, params: params)

  request.on_success do |response|
    json = response_to_json(response)
    @recent_imports = json.map { |j|
      Barcoder::PDF.new(j.with_indifferent_access) }
  end

  request
end
save_attr() click to toggle source
# File lib/barcoder/bucket.rb, line 151
def save_attr
  {name: name, import_hook: import_hook}
end
unmatched_pages_request() click to toggle source
# File lib/barcoder/bucket.rb, line 98
def unmatched_pages_request
  url = "#{Barcoder::Config.host}/unmatched_pages"
  request = Typhoeus::Request.new(url, params: {token: token})

  request.on_success do |response|
    json = response_to_json(response)
    @unmatched_pages = json.map { |j|
      Barcoder::Page.new(j.with_indifferent_access) }
  end

  request
end
update_bucket!() click to toggle source
# File lib/barcoder/bucket.rb, line 145
def update_bucket!
  opt = {params: save_attr.merge(token: token), method: :put}
  response = Typhoeus::Request.new(self.class.bucket_uri(self), opt).run
  process_save_response response
end