Class: RBook::Bisac::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/rbook/bisac/message.rb

Overview

Represents a single Bisac File. See RBook::Bisac for a basic usage example.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(company, san, batch, code) ⇒ Message

creates a new bisac file in memory params:

  • company = company name (10 chars)

  • san = company SAN number (12 chars)

  • batch = batch name for this file (6 chars)

  • code = 1 for new titles, 2 for updated titles (1 char)



18
19
20
21
22
23
24
# File 'lib/rbook/bisac/message.rb', line 18

def initialize(company, san, batch, code)
  @company = company
  @san = san
  @batch = batch
  @code = code
  @products = []
end

Instance Attribute Details

#batchObject

Returns the value of attribute batch.



10
11
12
# File 'lib/rbook/bisac/message.rb', line 10

def batch
  @batch
end

#codeObject

Returns the value of attribute code.



10
11
12
# File 'lib/rbook/bisac/message.rb', line 10

def code
  @code
end

#companyObject

Returns the value of attribute company.



10
11
12
# File 'lib/rbook/bisac/message.rb', line 10

def company
  @company
end

#productsObject

Returns the value of attribute products.



10
11
12
# File 'lib/rbook/bisac/message.rb', line 10

def products
  @products
end

#sanObject

Returns the value of attribute san.



10
11
12
# File 'lib/rbook/bisac/message.rb', line 10

def san
  @san
end

Class Method Details

.load(filename) ⇒ Object

loads the requested BISAC file into memory. returns a Message object or nil if the file is not a BISAC file



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rbook/bisac/message.rb', line 29

def self.load(filename)
  msg = self.new(nil,nil,nil,nil)
  
  File.open(filename, "r") do |f|
    f.each_line do |l|
      if l[0,10].eql?("**HEADER**") 
        msg.company = l[16,10].strip
        msg.san = l[26,12].strip
        msg.batch = l[38,6].strip
        msg.code = l[44,1].strip
      elsif !l[0,11].eql?("**TRAILER**") 
        product = RBook::Bisac::Product.from_string(l)
        msg << product unless product.nil?
      end
    end
  end
  
  if msg.company.nil? || msg.san.nil? || msg.batch.nil? || msg.code.nil?
    return nil
  else
    return msg
  end
end

Instance Method Details

#<<(item) ⇒ Object

adds a new title to the bisic file. Expects a Rbook::Bisac::Product object.



54
55
56
57
58
59
# File 'lib/rbook/bisac/message.rb', line 54

def << (item)
  unless item.class == RBook::Bisac::Product
    raise ArgumentError, 'item must be a RBook::Bisac::Product object'
  end
  @products << item
end

#to_sObject

converts this bisac file into a string



62
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
91
# File 'lib/rbook/bisac/message.rb', line 62

def to_s
  # File Header
  content = "**HEADER**"
  content << Time.now.strftime("%y%m%d")
  content << @company[0,10].ljust(10)
  content << @san[0,12].ljust(12)
  content << @batch[0,6].ljust(6)
  content << @code[0,1].ljust(1)
  content << "**PUBSTAT*"
  content << "040"
  content << "".ljust(201)
  content << "\n"

  # File Content
  counter = 0
  @products.each do |item|
    content << item.to_s + "\n"
    counter = counter + 1
  end

  # File Footer
  content << "**TRAILER*"
  content << Time.now.strftime("%y%m%d")
  content << @batch[0,6].ljust(6)
  content << counter.to_s[0,6].ljust(6)
  content << "**PUBSTAT*"
  content << "".ljust(221)

  return content
end

#write(filename) ⇒ Object

writes the content of this bisac file out to the specified file



94
95
96
# File 'lib/rbook/bisac/message.rb', line 94

def write(filename)
  File.open(filename, "w") { |f| f.puts to_s }
end