Skip to content

Commit

Permalink
Opening EPUB file to handle
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandoalmeida committed Dec 29, 2011
0 parents commit cf15393
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format nested
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "http://rubygems.org"

gemspec

group :development, :test do
gem "rspec"
end
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'bundler/gem_tasks'
22 changes: 22 additions & 0 deletions epub-reader.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "epub-reader/version"

Gem::Specification.new do |s|
s.name = "epub-reader"
s.version = Epub::Reader::VERSION
s.authors = ["Fernando Almeida"]
s.email = ["fernando@fernandoalmeida.net"]
s.homepage = "http://bitbucket.com/fernandoalmeida/epub-reader"
s.summary = "A library for accessing the content of EPUB files"
s.description = "The epub-reader library implements a EPUB parser conforming as much as possible to the EPUB 3 specification from IDPF"

s.rubyforge_project = "epub-reader"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency('rubyzip')
end
6 changes: 6 additions & 0 deletions lib/epub-reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "epub-reader/version"
require "epub-reader/reader"

module Epub
class FileNotFoundError < StandardError; end
end
24 changes: 24 additions & 0 deletions lib/epub-reader/reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'zip/zipfilesystem'

module Epub
class Reader

attr_reader :filename, :file

def initialize(file)
raise FileNotFoundError unless File.exists?(file)
@filename = file
@file = Zip::ZipFile.open(file)
end

def Reader.open(file)
reader = Reader.new(file)
if block_given?
yield reader
else
reader
end
end

end
end
5 changes: 5 additions & 0 deletions lib/epub-reader/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Epub
class Reader
VERSION = "0.0.1"
end
end
Binary file added spec/data/valid.epub
Binary file not shown.
21 changes: 21 additions & 0 deletions spec/reader_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

describe Epub::Reader do

before(:all) do
@epub = Epub::Reader.open('spec/data/valid.epub')
end

it 'should open a epub file' do
@epub.should_not be_nil
end

it 'should raise exception if file not found' do
lambda { Epub::Reader.open('not_found.epub') }.should raise_error
end

it 'should have the epub file to handle' do
@epub.file.should_not be_nil
end

end
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$: << File.join(File.dirname(__FILE__), "/../lib")

require "rspec"
require "epub-reader"

RSpec.configure do |c|
c.mock_with :rspec
end

0 comments on commit cf15393

Please sign in to comment.