elibri_onix_mocks 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,160 @@
1
+ # encoding: UTF-8
2
+
3
+ module OnixHelpers
4
+ extend ActiveSupport::Concern
5
+
6
+ module InstanceMethods
7
+
8
+ #zwróć różne poziomy tytułu, w formacie potrzebnym do exportu w ONIX-ie
9
+ def title_parts
10
+ [].tap do |res|
11
+ product_code = Elibri::ONIX::Dict::Release_3_0::TitleElementLevel::PRODUCT #01
12
+ collection_code = Elibri::ONIX::Dict::Release_3_0::TitleElementLevel::COLLECTION #02
13
+ res << OpenStruct.new(:level => collection_code, :title => collection.name, :part => self.collection_part) if collection
14
+ res << OpenStruct.new(:level => product_code, :title => self.title, :subtitle => self.subtitle, :part => self.title_part) if self.title.present?
15
+ end
16
+ end
17
+
18
+
19
+ # Zwróć datę publikacji w formacie ONIX
20
+ def publication_date(format = :onix, splitter = nil)
21
+ publication_year_str = self.publication_year ? "%04d"% self.publication_year : nil
22
+ publication_month_str = self.publication_month ? "%02d"% self.publication_month : nil
23
+ publication_day_str = self.publication_day ? "%02d"% self.publication_day : nil
24
+
25
+ case format
26
+ when :onix
27
+ splitter ||= ''
28
+ date_string = [publication_year_str, publication_month_str, publication_day_str].compact.join(splitter)
29
+ when :polish
30
+ splitter ||= '.'
31
+ date_string = [publication_day_str, publication_month_str, publication_year_str].compact.join(splitter)
32
+ end
33
+ date_string.blank? ? nil : date_string
34
+ end
35
+
36
+
37
+ def publication_date_with_onix_format_code
38
+ #lista 55
39
+ if self.publication_year.present? && self.publication_month.present? && self.publication_day.present?
40
+ [publication_date, Elibri::ONIX::Dict::Release_3_0::DateFormat::YYYYMMDD]
41
+ elsif self.publication_year.present? && self.publication_month.present?
42
+ [self.publication_year.to_s + "%02d" % self.publication_month, Elibri::ONIX::Dict::Release_3_0::DateFormat::YYYYMM]
43
+ elsif self.publication_year.present?
44
+ [self.publication_year.to_s, Elibri::ONIX::Dict::Release_3_0::DateFormat::YYYY]
45
+ else
46
+ [nil, nil]
47
+ end
48
+ end
49
+
50
+
51
+ # Parsuj datę publikacji z ONIX`a (YYYYMMDD) i uzupełnij odpowiednie pola
52
+ def publication_date=(date_from_xml)
53
+ if match = date_from_xml.to_s.match(/ (\d{4}) [\s-]? (\d{2})? [\s-]? (\d{2})? /x)
54
+ self.publication_year = $1
55
+ self.publication_month = $2
56
+ self.publication_day = $3
57
+ else
58
+ self.publication_year = self.publication_month = self.publication_day = nil if date_from_xml.blank?
59
+ end
60
+ end
61
+
62
+
63
+ # TODO
64
+ def notification_type
65
+ if current_state == :private
66
+ raise "Cannot handle private state"
67
+ elsif current_state == :announced
68
+ Elibri::ONIX::Dict::Release_3_0::NotificationType.find_by_onix_code(Elibri::ONIX::Dict::Release_3_0::NotificationType::EARLY_NOTIFICATION)
69
+ elsif current_state == :preorder
70
+ Elibri::ONIX::Dict::Release_3_0::NotificationType.find_by_onix_code(Elibri::ONIX::Dict::Release_3_0::NotificationType::ADVANCED_NOTIFICATION)
71
+ else
72
+ Elibri::ONIX::Dict::Release_3_0::NotificationType.find_by_onix_code(Elibri::ONIX::Dict::Release_3_0::NotificationType::CONFIRMED_ON_PUBLICATION)
73
+ end
74
+ end
75
+
76
+ #hack wynikający z nieobecności maszyny stanów
77
+ def current_state
78
+ state
79
+ end
80
+
81
+
82
+ def publishing_status_onix_code
83
+ if current_state.to_sym == :private #domyślny stan
84
+ raise "Cannot handle private state"
85
+ elsif current_state.to_sym == :announced #zapowiedź
86
+ Elibri::ONIX::Dict::Release_3_0::PublishingStatusCode::FORTHCOMING
87
+ elsif current_state.to_sym == :preorder #przedsprzedaż
88
+ Elibri::ONIX::Dict::Release_3_0::PublishingStatusCode::FORTHCOMING
89
+ elsif current_state.to_sym == :published #dostępna na rynku
90
+ Elibri::ONIX::Dict::Release_3_0::PublishingStatusCode::ACTIVE
91
+ elsif current_state.to_sym == :out_of_print #nakład wyczerpany
92
+ Elibri::ONIX::Dict::Release_3_0::PublishingStatusCode::OUT_OF_PRINT
93
+ elsif current_state.to_sym == :deleted #błędnie stworzony rekord
94
+ Elibri::ONIX::Dict::Release_3_0::PublishingStatusCode::UNSPECIFIED
95
+ else
96
+ raise "Don't know how to handle state = #{current_state}"
97
+ end
98
+ end
99
+
100
+ def publishing_status_onix_code=(code)
101
+ if code.present?
102
+ if code == Elibri::ONIX::Dict::Release_3_0::PublishingStatusCode::ACTIVE
103
+ self.state = "published"
104
+ elsif code == Elibri::ONIX::Dict::Release_3_0::PublishingStatusCode::OUT_OF_PRINT
105
+ self.state = "out_of_print"
106
+ elsif code == Elibri::ONIX::Dict::Release_3_0::PublishingStatusCode::FORTHCOMING
107
+ self.state = "preorder"
108
+ else
109
+ raise "Don't know how to handle publishing_stats_onix_code = #{code}"
110
+ end
111
+ end
112
+ end
113
+
114
+ def form
115
+ Elibri::ONIX::Dict::Release_3_0::ProductFormCode.find_by_onix_code(self.product_form_onix_code)
116
+ end
117
+
118
+ def publishing_status
119
+ Elibri::ONIX::Dict::Release_3_0::PublishingStatusCode.find_by_onix_code(self.publishing_status_onix_code)
120
+ end
121
+
122
+ def epub_technical_protection
123
+ Elibri::ONIX::Dict::Release_3_0::EpubTechnicalProtection.find_by_onix_code(self.epub_technical_protection_onix_code)
124
+ end
125
+
126
+ def form_detail
127
+ Elibri::ONIX::Dict::Release_3_0::ProductFormDetail.find_by_onix_code(self.product_form_detail_onix_code)
128
+ end
129
+
130
+ def audience_range_present?
131
+ self.audience_age_from.present? or self.audience_age_to.present?
132
+ end
133
+
134
+
135
+ def authorship_kind
136
+ value = self.method_missing(:authorship_kind) || ActiveSupport::StringInquirer.new(:user_given.to_s)
137
+ end
138
+
139
+
140
+ def series_membership_kind
141
+ value = self.method_missing(:series_membership_kind) || ActiveSupport::StringInquirer.new(:user_given.to_s)
142
+ end
143
+
144
+
145
+ def series_names
146
+ self.series_memberships.map {|series_membership| series_membership.publisher_series.try(:name)}.compact
147
+ end
148
+
149
+ def stock_quantity_for_merlin
150
+ if stock_quantity.nil?
151
+ 0
152
+ elsif stock_quantity > 50
153
+ 50
154
+ else
155
+ stock_quantity
156
+ end
157
+ end
158
+
159
+ end
160
+ end
@@ -0,0 +1,3 @@
1
+ module ElibriOnixMocks
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,122 @@
1
+ # encoding: UTF-8
2
+ require 'set'
3
+
4
+ module Elibri
5
+
6
+ # Klasa implementująca logikę wariantów XML produktów. Gdy klient zapłaci za pełne informacje
7
+ # o produkcie, to dostaje bogatszy XML. Poniższa klasa jest tylko helperem, który upraszcza
8
+ # i redukuje kod w innych miejscach aplikacji.
9
+ class XmlVariant
10
+ attr_reader :features
11
+
12
+ # Wyjątek zgłaszany przy nieprawidłowej kombinacji wariantu:
13
+ class InvalidFeature < Exception; end
14
+
15
+ FEATURES_SEPARATOR = ':'
16
+
17
+ # Dostępne elementy XML produktu. Gdy nic nie będzie wybrane, XML będzie zawierał
18
+ # tylko RecordReference.
19
+ FEATURES = ActiveSupport::OrderedHash.new
20
+ FEATURES[:basic_meta] = 'podstawowe meta-dane produktów'
21
+ FEATURES[:media_files] = 'linki do załączników (okładki, fragmenty treści)'
22
+ FEATURES[:other_texts] = 'opisy szczegółowe (spis treści, recenzje)'
23
+ FEATURES[:stocks] = 'stany magazynowe dostawców'
24
+
25
+ #jeśli ostatni argument to true, to nie waliduj, czy kobinacja jest prawidłowa,
26
+ #a więc jest jedną z ALL_FEATURES_COMBINATIONS
27
+ def initialize(*features)
28
+ @features = Array(features).flatten
29
+ if @features[-1] === true
30
+ ignore_unknow_combinations = true
31
+ @features.pop
32
+ else
33
+ ignore_unknow_combinations = false
34
+ end
35
+ @features = @features.reject(&:blank?).map(&:to_sym).to_set
36
+
37
+ unless (@features - FEATURES.keys).empty?
38
+ raise InvalidFeature.new("Unknown xml features #{@features.inspect}")
39
+ end
40
+ unless ignore_unknow_combinations
41
+ if @features.present? and !ALL_FEATURES_COMBINATIONS.map(&:features).include?(@features)
42
+ raise InvalidFeature.new("Unknow combination of xml features #{@features.inspect}")
43
+ end
44
+ end
45
+ end
46
+
47
+ # Wszystkie możliwe kombinacje wariantów XML - zbiór uzupełniany na końcu klasy.
48
+ # Upraszcza kod odświeżacza XML. W celu odświeżenia wszystkich możliwych wariantów XML
49
+ # dla produktu, po prostu iteruję po tej kolekcji.
50
+ ALL_FEATURES_COMBINATIONS = Set.new
51
+ ALL_FEATURES_COMBINATIONS << XmlVariant.new([:basic_meta, :media_files, :other_texts], true)
52
+ ALL_FEATURES_COMBINATIONS << XmlVariant.new([:basic_meta, :media_files, :other_texts, :stocks], true)
53
+ ALL_FEATURES_COMBINATIONS << XmlVariant.new([:stocks], true)
54
+
55
+
56
+ # Fabryka budująca instancę XmlVariant ze stringu 'basic_meta:media_files:other_texts:stocks'
57
+ def self.deserialize(features_str, ignore_unknow_combinations = false)
58
+ if ignore_unknow_combinations
59
+ XmlVariant.new(features_str.to_s.split(FEATURES_SEPARATOR), true)
60
+ else
61
+ XmlVariant.new(features_str.to_s.split(FEATURES_SEPARATOR))
62
+ end
63
+ end
64
+
65
+ def except(*features)
66
+ excluded_features = Array(features).flatten.map(&:to_sym)
67
+ XmlVariant.new( (self.features - excluded_features).to_a )
68
+ end
69
+
70
+
71
+ def ==(other_xml_variant)
72
+ self.features == other_xml_variant.features
73
+ end
74
+
75
+
76
+ def eql?(other_xml_variant)
77
+ self == other_xml_variant
78
+ end
79
+
80
+
81
+ def equal?(other_xml_variant)
82
+ self == other_xml_variant
83
+ end
84
+
85
+
86
+ # Żeby działało porównywanie w Set`ach
87
+ def hash
88
+ self.features.hash
89
+ end
90
+
91
+
92
+ def feature_included?(feature)
93
+ self.features.include?(feature.to_sym)
94
+ end
95
+
96
+
97
+ FEATURES.keys.each do |feature_sym|
98
+ self.class_eval %Q{
99
+ def includes_#{feature_sym}? # def includes_other_texts?
100
+ feature_included?(:#{feature_sym}) # feature_included?(:other_texts)
101
+ end # end
102
+ }, __FILE__, __LINE__
103
+ end
104
+
105
+
106
+ # Wykorzystywane do utworzenia nazwy pliku XML oraz serializacji wariantu w MySQL.
107
+ # Zwraca np. 'basic_meta:media_files:other_texts:stocks'
108
+ def serialize
109
+ # features muszą być posortowane, aby za każdym razem generować tę samą nazwę - Set nie gwarantuje kolejności.
110
+ self.features.map(&:to_s).sort.join(FEATURES_SEPARATOR)
111
+ end
112
+
113
+
114
+ def to_s
115
+ "#<Elibri::XmlVariant: features=#{self.features.inspect}>"
116
+ end
117
+
118
+ # Wariant zawierający wszystkie możliwe dane:
119
+ FULL_VARIANT = self.new(FEATURES.keys)
120
+ end
121
+
122
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ $VERBOSE = nil #temp: supress id warnings
6
+
7
+ describe Elibri::XmlMocks::Examples do
8
+
9
+ [
10
+ :basic_product, :book_example, :onix_record_identifiers_example, :onix_product_form_example,
11
+ :onix_epub_details_example, :onix_categories_example, :onix_languages_example,
12
+ :onix_measurement_example, :onix_sale_restrictions_example, :onix_audience_range_example,
13
+ :onix_publisher_info_example, :onix_subjects_example, :onix_edition_example, :onix_ebook_extent_example,
14
+ :onix_audiobook_extent_example, :onix_no_contributors_example, :onix_collective_work_example,
15
+ :onix_contributors_example, :onix_announced_product_example, :onix_preorder_product_example,
16
+ :onix_published_product_example, :onix_out_of_print_product_example, :onix_titles_example,
17
+ :onix_title_with_collection_example, :onix_texts_example, :onix_related_products_example,
18
+ :onix_supply_details_example, :onix_series_memberships_example, :onix_supporting_resources_example,
19
+ :onix_elibri_extensions_example, :contributor_mock, :review_mock, :supply_detail_mock, :imprint_mock,
20
+ :description_mock
21
+ ].each do |symbol|
22
+
23
+ it "should create #{symbol} xml and parse it properly" do
24
+ Elibri::XmlMocks::Examples.send(symbol, {}).should_not be_nil
25
+ Elibri::ONIX::XMLGenerator.new(Elibri::XmlMocks::Examples.send(symbol, {})).should_not be_nil
26
+ Elibri::ONIX::Release_3_0::ONIXMessage.from_xml(Elibri::ONIX::XMLGenerator.new(Elibri::XmlMocks::Examples.send(symbol, {})).to_s)
27
+ end
28
+
29
+ end
30
+
31
+ #more tests to add
32
+
33
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+
5
+ require 'elibri_onix_mocks'
6
+ require 'rspec'
7
+ require 'ostruct'
8
+
9
+
10
+ RSpec.configure do |config|
11
+ # some (optional) config here
12
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elibri_onix_mocks
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Piotr Szmielew
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-04-05 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rake
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: elibri_onix_dict
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: elibri_api_client
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :runtime
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: mocha
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ type: :runtime
89
+ version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ name: builder
92
+ prerelease: false
93
+ requirement: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ type: :runtime
103
+ version_requirements: *id006
104
+ - !ruby/object:Gem::Dependency
105
+ name: activesupport
106
+ prerelease: false
107
+ requirement: &id007 !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ type: :runtime
117
+ version_requirements: *id007
118
+ description: "Usage: Elibri::XmlGenerator.basic_product etc"
119
+ email:
120
+ - p.szmielew@ava.waw.pl
121
+ executables: []
122
+
123
+ extensions: []
124
+
125
+ extra_rdoc_files: []
126
+
127
+ files:
128
+ - .gitignore
129
+ - .travis.yml
130
+ - Gemfile
131
+ - README.md
132
+ - Rakefile
133
+ - elibri_onix_mocks.gemspec
134
+ - lib/elibri_onix_mocks.rb
135
+ - lib/elibri_onix_mocks/generators/xml_generator.rb
136
+ - lib/elibri_onix_mocks/generators/xml_tags.yml
137
+ - lib/elibri_onix_mocks/mocks/mock_method_missing.rb
138
+ - lib/elibri_onix_mocks/mocks/xml_mocks.rb
139
+ - lib/elibri_onix_mocks/onix_helpers.rb
140
+ - lib/elibri_onix_mocks/version.rb
141
+ - lib/elibri_onix_mocks/xml_variant.rb
142
+ - spec/elibri_onix_mocks_spec.rb
143
+ - spec/spec_helper.rb
144
+ homepage: ""
145
+ licenses: []
146
+
147
+ post_install_message:
148
+ rdoc_options: []
149
+
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ hash: 3
158
+ segments:
159
+ - 0
160
+ version: "0"
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ hash: 3
167
+ segments:
168
+ - 0
169
+ version: "0"
170
+ requirements: []
171
+
172
+ rubyforge_project: elibri_onix_mocks
173
+ rubygems_version: 1.8.17
174
+ signing_key:
175
+ specification_version: 3
176
+ summary: Gem that allows you to mock eLibri style xmls
177
+ test_files:
178
+ - spec/elibri_onix_mocks_spec.rb
179
+ - spec/spec_helper.rb