Class: Bisac::Message

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

Overview

Represents a single BISAC product metadata file. Note that the BISAC metadata file format is now quite dated, and should be avoided where possible, as it doesn’t support the ISBN13 standard.

Generating

msg = Bisac::Message.new("Company Name", "1111111", "080906", 1)
msg << Bisac:Product.new("0385519869")
msg << Bisac:Product.new("014044565X")
msg.write("filename.bsc")

Reading

msg = Bisac::Message.load("filename.bsc")
puts msg.company
puts msg.san
puts msg.products.size

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)



30
31
32
33
34
35
36
# File 'lib/bisac/message.rb', line 30

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.



22
23
24
# File 'lib/bisac/message.rb', line 22

def batch
  @batch
end

#codeObject

Returns the value of attribute code.



22
23
24
# File 'lib/bisac/message.rb', line 22

def code
  @code
end

#companyObject

Returns the value of attribute company.



22
23
24
# File 'lib/bisac/message.rb', line 22

def company
  @company
end

#productsObject

Returns the value of attribute products.



22
23
24
# File 'lib/bisac/message.rb', line 22

def products
  @products
end

#sanObject

Returns the value of attribute san.



22
23
24
# File 'lib/bisac/message.rb', line 22

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bisac/message.rb', line 41

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 = 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 Bisac::Product object.



66
67
68
69
70
71
# File 'lib/bisac/message.rb', line 66

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

#to_sObject

converts this bisac file into a string



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bisac/message.rb', line 74

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



106
107
108
# File 'lib/bisac/message.rb', line 106

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