Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Audience Range to follow DTD #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source :gemcutter
source "https://rubygems.org"

gemspec
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "bundler"
Bundler.setup

require 'rake'
require 'rake/rdoctask'
require 'rdoc/task'
require 'rspec/core/rake_task'


Expand All @@ -16,7 +16,7 @@ RSpec::Core::RakeTask.new("spec") do |t|
t.rspec_opts = ["--color", "--format progress"]
end
desc 'Generate documentation'
Rake::RDocTask.new(:rdoc) do |rdoc|
RDoc::Task.new do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ONIX'
rdoc.options << '--line-numbers' << '--inline-source'
Expand Down
82 changes: 82 additions & 0 deletions data/product_google_sample_onix_2.1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!-- Sample product composite, puts a book a book on sale on Google Play -->
<Product>
<RecordReference>myid.9789999999991</RecordReference> <!-- Unique idenfier per product record, Google suggests using ISBN13 -->
<NotificationType>03</NotificationType> <!-- 01 = early notification, 02 = advance notification, 03 = notification confirmed on publication, 05 = delete -->
<ProductIdentifier> <!-- The record must have at least one valid ISBN, when possible, type = 15 recommended -->
<ProductIDType>15</ProductIDType>
<IDValue>9789999999991</IDValue>
</ProductIdentifier>
<ProductForm>DG</ProductForm> <!-- DG = electronic book text -->
<EpubType>029</EpubType> <!-- 000 = Epublication content package, 002 = PDF, 023 = Open Ebook, 029 = EPUB, 032 = Google Edition -->
<Title>
<TitleType>01</TitleType>
<TitleText>This is my distinctive title.</TitleText>
<Subtitle>This is my subtitle.</Subtitle> <!-- optional -->
</Title>
<Contributor>
<ContributorRole>A01</ContributorRole>
<PersonName>Jane Smith</PersonName>
<BiographicalNote>I am a bibliographical note tied to a specific author.</BiographicalNote>
</Contributor>
<Language>
<LanguageRole>01</LanguageRole>
<LanguageCode>eng</LanguageCode>
</Language>
<NumberOfPages>1024</NumberOfPages>
<Subject>
<!-- BISAC classification -->
<SubjectSchemeIdentifier>10</SubjectSchemeIdentifier>
<SubjectCode>LIT014000</SubjectCode>
</Subject>
<OtherText>
<TextTypeCode>01</TextTypeCode>
<Text>I am the main description of the book.</Text>
</OtherText>
<OtherText>
<TextTypeCode>13</TextTypeCode>
<Text>I am a bibiliographical note of a book not a specific author.</Text>
</OtherText>
<Imprint>
<NameCodeType>02</NameCodeType>
<NameCodeValue>My Imprint Name</NameCodeValue>
</Imprint>
<Publisher>
<NameCodeType>02</NameCodeType>
<NameCodeValue>My Publishing Company</NameCodeValue>
</Publisher>
<PublishingStatus>04</PublishingStatus>
<PublicationDate>20120927</PublicationDate> <!-- YYYY and YYYYMM also valid -->
<SalesRights>
<SalesRightsType>01</SalesRightsType>
<RightsTerritory>WORLD</RightsTerritory>
<!-- Note, if you supply RightsCountry instead, do not also supply RightsTerritory -->
</SalesRights>
<RelatedProduct>
<RelationCode>13</RelationCode> <!-- 13 = Epublication based on print book, enables the digital book to appear in Play serach results on a print ISBN -->
<ProductIdentifier>
<ProductIDType>15</ProductIDType>
<IDValue>9789999999984</IDValue> <!-- ISBN13 of print book -->
</ProductIdentifier>
</RelatedProduct>
<SupplyDetail>
<SupplierName>My Supplier Name</SupplierName>
<ProductAvailability>20</ProductAvailability>
<Price>
<PriceTypeCode>02</PriceTypeCode> <!-- This should match the price type that you are configured to sell in the Partner Center -->
<PriceAmount>9.99</PriceAmount>
<CurrencyCode>USD</CurrencyCode>
<CountryCode>US</CountryCode>
<!-- Note, if you supply CountryCode, do not also supply Territory -->
</Price>
</SupplyDetail>
</Product>

<!-- Sample delete composite, deletes book from sale on Play and removes from preview on Google Books -->
<Product>
<RecordReference>myid.9780000000009</RecordReference> <!-- The record reference of the product to delete, associated with the ISBN provided in ProductIdentifier -->
<NotificationType>05</NotificationType> <!-- 05 = delete record -->
<ProductIdentifier> <!-- The record must have at least one valid ISBN -->
<ProductIDType>15</ProductIDType>
<IDValue>9780000000009</IDValue>
</ProductIdentifier>
</Product>
10 changes: 8 additions & 2 deletions lib/onix/apa_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ def pack_quantity=(val)
def rrp_exc_sales_tax
price_get(1).andand.price_amount
end

# set the rrp excluding any sales tax
def rrp_exc_sales_tax_with_currency(num, currency_code)
price_set(1, num, currency_code)
end

# set the rrp excluding any sales tax
def rrp_exc_sales_tax=(num)
Expand All @@ -432,7 +437,7 @@ def rrp_inc_sales_tax

# set the rrp including any sales tax
def rrp_inc_sales_tax=(num)
price_set(2, num)
price_set(2, num, currency_code)
end

# retrieve the nett price including any sales tax
Expand Down Expand Up @@ -709,14 +714,15 @@ def price_get(type)
end

# set the value of a particular price
def price_set(type, num)
def price_set(type, num, currency_code=nil)
p = price_get(type)

# create a new isbn record if we need to
if p.nil?
supply = find_or_create_supply_detail
p = ONIX::Price.new
p.price_type_code = type
p.currency_code = currency_code if currency_code
supply.prices << p
end

Expand Down
35 changes: 25 additions & 10 deletions lib/onix/audience_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,34 @@ class AudienceRange
xml_name "AudienceRange"

xml_accessor :audience_range_qualifier, :from => "AudienceRangeQualifier", :as => Fixnum, :to_xml => ONIX::Formatters.two_digit
xml_accessor :audience_range_precisions, :from => "AudienceRangePrecision", :as => [Fixnum], :to_xml => [ONIX::Formatters.two_digit] # TODO: two_digit isn't working on the array items
xml_accessor :audience_range_values, :from => "AudienceRangeValue"

# TODO: element AudienceRange: validity error :
# Element AudienceRange content does not follow the DTD, expecting
# (AudienceRangeQualifier , AudienceRangePrecision , AudienceRangeValue ,
# (AudienceRangePrecision , AudienceRangeValue)?),
# got
# (AudienceRangeQualifier AudienceRangePrecision AudienceRangePrecision
# AudienceRangeValue AudienceRangeValue )
xml_accessor :audience_range_precisions, :from => "AudienceRangePrecision", :as => [Fixnum] , :to_xml => [ONIX::Formatters.two_digit]
xml_accessor :audience_range_values, :from => "AudienceRangeValue", :as => [Fixnum] , :to_xml => [ONIX::Formatters.two_digit]

def initialize
self.audience_range_precisions = []
self.audience_range_values = []
end

# Returns an XML object representing this object
def to_xml(params = {})
params.reverse_merge!(:name => self.class.tag_name, :namespace => self.class.roxml_namespace)
params[:namespace] = nil if ['*', 'xmlns'].include?(params[:namespace])
XML.new_node([params[:namespace], params[:name]].compact.join(':')).tap do |root|
refs = (self.roxml_references.present? \
? self.roxml_references \
: self.class.roxml_attrs.map {|attr| attr.to_ref(self) })


refs[0].update_xml(root, refs[0].to_xml(self)) #:audience_range_qualifier

if refs[1] #:audience_range_precisions
refs[1].to_xml(self).each_with_index do |r, i|
refs[1].update_xml(root, [refs[1].to_xml(self)[i]] )
refs[2].update_xml(root, [refs[2].to_xml(self)[i]] )
end
end
end
end
end

end
1 change: 1 addition & 0 deletions lib/onix/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Product
xml_accessor :notification_type, :from => "NotificationType", :as => Fixnum, :to_xml => ONIX::Formatters.two_digit
xml_accessor :product_identifiers, :from => "ProductIdentifier", :as => [ONIX::ProductIdentifier]
xml_accessor :product_form, :from => "ProductForm"
xml_accessor :epub_type, :from => "EpubType"
xml_accessor :product_form_detail, :from => "ProductFormDetail"
xml_accessor :series, :from => "Series", :as => [ONIX::Series]
xml_accessor :titles, :from => "Title", :as => [ONIX::Title]
Expand Down
17 changes: 16 additions & 1 deletion spec/apa_product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,24 @@
it "should return the first price in the file of type 1" do
@product = ONIX::Product.from_xml(@product_node.to_s)
@apa = ONIX::APAProduct.new(@product)

@apa.rrp_exc_sales_tax.should eql(BigDecimal.new("99.95"))
end

it "should return the first price in the file of type 1" do
@apa = ONIX::APAProduct.new
@apa.rrp_exc_sales_tax_with_currency(2, "USD")
@apa.to_xml.to_s.should eql ("<Product>
<SupplyDetail>
<Price>
<PriceTypeCode>01</PriceTypeCode>
<PriceAmount>2</PriceAmount>
<CurrencyCode>USD</CurrencyCode>
</Price>
</SupplyDetail>
</Product>")
end

end

describe ONIX::APAProduct, "proprietry_discount_code_for_rrp method" do
Expand Down
9 changes: 8 additions & 1 deletion spec/audience_range_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

it "should provide read access to first level attributes" do
aud = ONIX::AudienceRange.from_xml(@root.to_s)

aud.audience_range_qualifier.should eql(11)
aud.audience_range_precisions.size.should eql(2)
aud.audience_range_precisions[0].should eql(3)
Expand All @@ -41,4 +40,12 @@
aud.to_xml.to_s.include?("<AudienceRangeValue>999</AudienceRangeValue>").should be_true
end

it "should provide write access to first level attributes and handle arrays" do
range = ONIX::AudienceRange.new
range.audience_range_qualifier = 18
range.audience_range_precisions = [3,4]
range.audience_range_values =[3,6]
range.to_xml.to_s.should eql("<AudienceRange>\n <AudienceRangeQualifier>18</AudienceRangeQualifier>\n <AudienceRangePrecision>3</AudienceRangePrecision>\n <AudienceRangeValue>3</AudienceRangeValue>\n <AudienceRangePrecision>4</AudienceRangePrecision>\n <AudienceRangeValue>6</AudienceRangeValue>\n</AudienceRange>")
end

end
19 changes: 19 additions & 0 deletions spec/product_gsample_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# coding: utf-8

require File.dirname(__FILE__) + '/spec_helper.rb'

describe ONIX::Product do

before(:each) do
@data_path = File.join(File.dirname(__FILE__),"..","data")
file1 = File.join(@data_path, "product_google_sample_onix_2.1.xml")
@doc = Nokogiri::XML::Document.parse(File.read(file1))
@product_node = @doc.root
end

it "should provide read access to product IDs" do
product = ONIX::Product.from_xml(@product_node.to_s)
product.epub_type.should eql( "029")
end

end