brandmaker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ module Brandmaker
2
+ class JobConfig
3
+ attr_accessor :technical_name
4
+ attr_accessor :variables
5
+
6
+ def initialize technical_name, variables = []
7
+ @technical_name = technical_name
8
+ @variables = variables
9
+ end
10
+
11
+ def add_variables *vars
12
+ vars.each_with_index do |var, index|
13
+ hash = var.is_a?(Hash) ? var : { :name => var }
14
+ variables << VariableConfig.new(hash)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module Brandmaker
2
+ class MediaVariable < Variable
3
+ def value
4
+ super.split(",").join("\n")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Brandmaker
4
+ class Variable
5
+ attr_accessor :config
6
+
7
+ def initialize data, config = VariableConfig.new
8
+ @data = data
9
+ @config = config
10
+ end
11
+
12
+ def data
13
+ @data
14
+ end
15
+
16
+ def technical_name
17
+ @data[:technical_name]
18
+ end
19
+
20
+ def value
21
+ @data[:value]
22
+ end
23
+
24
+ def purpose
25
+ # purpose is actually not provided by the api
26
+ @data[:purpose] || config.purpose
27
+ end
28
+
29
+ def label
30
+ config.label || technical_name.humanize
31
+ end
32
+
33
+ def type_id
34
+ @data[:variable_type_id]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,26 @@
1
+ module Brandmaker
2
+ class VariableCollection < Array
3
+
4
+ def initialize variables, create = true
5
+ if create
6
+ super variables.map { |var| Brandmaker::Variable.new(var) }
7
+ else
8
+ super variables
9
+ end
10
+ end
11
+
12
+ def find_by_technical_name(technical_name)
13
+ find { |variable| variable.technical_name == technical_name }
14
+ end
15
+
16
+ def find_by_purpose(purpose)
17
+ find do |variable|
18
+ variable.purpose == purpose
19
+ end || raise("#{purpose} is not configured for this job")
20
+ end
21
+
22
+ def unpurposed
23
+ VariableCollection.new self.select { |var| var.purpose.blank? }, false
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,39 @@
1
+ require 'brandmaker/variable'
2
+ require 'brandmaker/media_variable'
3
+ require 'brandmaker/grid_variable'
4
+
5
+ module Brandmaker
6
+ class VariableConfig
7
+ attr_accessor :name
8
+
9
+ attr_accessor :label
10
+ attr_accessor :content_type
11
+ attr_accessor :purpose
12
+
13
+ VARIABLE_TYPES = {
14
+ :"" => Variable,
15
+ :media => MediaVariable,
16
+ :external_media => ExternalMediaVariable,
17
+ :grid => GridVariable
18
+ }
19
+
20
+ def initialize(*h)
21
+ if h.length == 1 && h.first.kind_of?(Hash)
22
+ h.first.each { |k,v| send("#{k}=",v) }
23
+ end
24
+ end
25
+
26
+ def to_typed_instance(data)
27
+ type = content_type
28
+ klass = VARIABLE_TYPES[content_type.to_s.downcase.to_sym]
29
+ unless klass.nil?
30
+ instance = klass.new(data)
31
+ instance.config = self
32
+ instance
33
+ else
34
+ raise "Unknown variable type #{type}"
35
+ end
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,7 @@
1
+ module Brandmaker
2
+ class VariablePurpose
3
+ EMAIL_RECIPIENT = 'email_recipient'
4
+ EMAIL_SUBJECT = 'email_subject'
5
+ EMAIL_MESSAGE = 'email_message'
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Brandmaker
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ module Brandmaker
4
+ describe Configuration do
5
+
6
+ describe 'default configuration' do
7
+ it 'initializes a default configuration' do
8
+ Brandmaker.configuration.should be_a(Configuration)
9
+ end
10
+
11
+ it 'initializes an empty job_configs hash' do
12
+ Brandmaker.configuration.job_configs.should == {}
13
+ end
14
+
15
+ it 'initializes a default dse_client' do
16
+ Brandmaker.configuration.dse_client.should be_a(Savon::Client)
17
+ end
18
+
19
+ it 'initializes a default media_pool_client' do
20
+ Brandmaker.configuration.media_pool_client.should be_a(Savon::Client)
21
+ end
22
+ end
23
+
24
+ describe '#configure' do
25
+ context 'when calling configure' do
26
+ before :each do
27
+ Brandmaker.configure do |c|
28
+ c.user = 'user'
29
+ c.password = 'user'
30
+ c.dse_service = 'dse_service_url'
31
+ c.media_pool_service = 'media_pool_url'
32
+ end
33
+ end
34
+
35
+ it 'creates a dse_client with the given configuration' do
36
+ Brandmaker.configuration.dse_client.wsdl.document.should == 'dse_service_url'
37
+ Brandmaker.configuration.dse_client.wsdl.endpoint.should == 'dse_service_url'
38
+ end
39
+
40
+ it 'creates a media_pool_client with the given configuration' do
41
+ Brandmaker.configuration.media_pool_client.wsdl.document.should == 'media_pool_url'
42
+ Brandmaker.configuration.media_pool_client.wsdl.endpoint.should == 'media_pool_url'
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe Brandmaker::CustomStructure do
4
+
5
+ describe 'class methods' do
6
+ before do
7
+ Brandmaker::CustomStructure.stub!(:find_all_custom_structures)
8
+ .and_return(mock(Object, :body => custom_structure_all_data))
9
+ end
10
+
11
+ describe '.all' do
12
+ it 'returns an array of Brandmaker::CustomStructure instances' do
13
+ Brandmaker::CustomStructure.all.should be_all { |cs| cs.should be_a(Brandmaker::CustomStructure) }
14
+ end
15
+ end
16
+
17
+ describe '.find_by_technical_name' do
18
+ context 'when the technical name successfully matches a mapping' do
19
+ it 'returns an instance of Brandmaker::CustomStructure' do
20
+ Brandmaker::CustomStructure.find_by_technical_name('anbieter_hinzufgen').should be_a(Brandmaker::CustomStructure)
21
+ end
22
+
23
+ it 'accesses the custom structure collection' do
24
+ Brandmaker::CustomStructure.should_receive(:all).and_return []
25
+ Brandmaker::CustomStructure.find_by_technical_name('anbieter_hinzufgen')
26
+ end
27
+ end
28
+
29
+ context 'when the technical name does not match a mapping' do
30
+ it 'returns nil' do
31
+ Brandmaker::CustomStructure.find_by_technical_name('anything').should be_nil
32
+ end
33
+
34
+ it 'does not access the collection' do
35
+ Brandmaker::CustomStructure.should_not_receive(:all)
36
+ Brandmaker::CustomStructure.find_by_technical_name('anything')
37
+ end
38
+ end
39
+ end
40
+
41
+ describe '.custom_structure_name_for_mapped_technical_name' do
42
+ it 'maps a technical name of variable to a custom structure name' do
43
+ mapped_value = 'PM_ExterneSuppliers_Druckereien'
44
+ Brandmaker::CustomStructure.custom_structure_name_for_mapped_technical_name('anbieter_hinzufgen').should == mapped_value
45
+ end
46
+ end
47
+ end
48
+
49
+ describe 'instance methods' do
50
+ let :custom_structure do
51
+ Brandmaker::CustomStructure.new custom_structure_external_suppliers_data
52
+ end
53
+
54
+ describe '#name' do
55
+ it 'returns the name' do
56
+ custom_structure.name.should == 'PM_ExterneSuppliers_Druckereien'
57
+ end
58
+ end
59
+
60
+ describe '#label' do
61
+ it 'returns the label' do
62
+ custom_structure.label.should == 'PM_ExterneSuppliers_Druckereien'
63
+ end
64
+ end
65
+
66
+ describe '#objects' do
67
+ it 'returns an array of hashes' do
68
+ custom_structure.objects.should be_all { |h| h.should be_a(Hash) }
69
+ end
70
+ end
71
+ end
72
+ end
73
+
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+
4
+ module Brandmaker
5
+ describe ExternalMediaVariable do
6
+
7
+ describe '#value' do
8
+ context 'when no value is available' do
9
+ it 'returns nil' do
10
+ var = ExternalMediaVariable.new({ :value => nil })
11
+ var.value.should be_nil
12
+ end
13
+ end
14
+
15
+ context 'when a single value is available' do
16
+ it 'returns that value' do
17
+ var = ExternalMediaVariable.new({ :value => "one" })
18
+ var.value.should == 'one'
19
+ end
20
+ end
21
+
22
+ context 'when multiple values are available' do
23
+ it 'returns the last value' do
24
+ var = ExternalMediaVariable.new({ :value => "one,two" })
25
+ var.value.should == 'two'
26
+ end
27
+ end
28
+ end
29
+
30
+ describe '#reload' do
31
+ before :each do
32
+ Brandmaker.configuration.external_media_service = 'URL'
33
+ Brandmaker.configuration.external_media_service_secret = 'SECRET'
34
+ end
35
+
36
+ let :var do
37
+ ExternalMediaVariable.new({ :value => "130" })
38
+ end
39
+
40
+ context 'with valid mediaID' do
41
+ before :each do
42
+ RestClient.should_receive(:get).with(
43
+ 'URL', {:params => {:mediaID => "130", :secret => 'SECRET'}}
44
+ ).and_return({
45
+ :downloadUrl => "download url",
46
+ :fileOriginalName => "original name"
47
+ }.to_json)
48
+ end
49
+
50
+ it 'fetches media variable attributes from the API' do
51
+ var.reload
52
+ var.fileOriginalName.should == 'original name'
53
+ var.downloadUrl.should == 'download url'
54
+ end
55
+
56
+ it 'returns self' do
57
+ var.reload.should be(var)
58
+ end
59
+ end
60
+
61
+ context 'with invalid mediaID' do
62
+ it 'fetches media variable attributes from the API' do
63
+ RestClient.should_receive(:get).and_return({
64
+ :ERROR => "Requested media not found!"
65
+ }.to_json)
66
+ expect { var.reload }.to raise_error(/Requested media not found/)
67
+ end
68
+ end
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,153 @@
1
+ require 'spec_helper'
2
+
3
+ module Brandmaker
4
+ describe GridVariable do
5
+ describe '#value' do
6
+
7
+ context 'without a matching custom structure' do
8
+ context 'when the purpose is not present' do
9
+ let :grid_var do
10
+ g = Brandmaker::GridVariable.new grid_variable_data
11
+ g.stub!(:custom_structure).and_return(nil)
12
+ g
13
+ end
14
+
15
+ it 'returns an array of values' do
16
+ grid_var.value.should == grid_var.actual_values
17
+ end
18
+
19
+ it 'returns the actual values' do
20
+ grid_var.should_receive(:actual_values).once
21
+ grid_var.should_not_receive(:custom_structure_values)
22
+ grid_var.value
23
+ end
24
+ end
25
+
26
+ context 'when the purpose is the email recipient' do
27
+ let :grid_var do
28
+ g = Brandmaker::GridVariable.new grid_variable_email_data
29
+ g.stub!(:custom_structure).and_return(nil)
30
+ g.stub(:purpose).and_return(VariablePurpose::EMAIL_RECIPIENT)
31
+ g
32
+ end
33
+
34
+ it 'returns an array of email addresses' do
35
+ grid_var.value.should == %w(immanuel.haeussermann@screenconcept.ch michael.friedli@gateb.com)
36
+ end
37
+
38
+ it 'parses the values' do
39
+ grid_var.should_receive(:extract_email).twice
40
+ grid_var.value
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'with a matching custom structure' do
46
+ it 'returns the custom structure values' do
47
+ grid_var = Brandmaker::GridVariable.new grid_variable_data
48
+ grid_var.stub(:custom_structure).and_return Brandmaker::CustomStructure.new({})
49
+ grid_var.should_receive(:custom_structure_values).once.and_return %w(A B)
50
+ grid_var.value.should == %w(A B)
51
+ end
52
+ end
53
+ end
54
+
55
+ describe '#actual_values' do
56
+ let :grid_var do
57
+ Brandmaker::GridVariable.new grid_variable_data
58
+ end
59
+
60
+ it 'does not fail' do
61
+ grid_var.stub(:rows).and_return [{:worthless => nil}]
62
+ expect { grid_var.actual_values }.to_not raise_error
63
+ end
64
+
65
+ it 'returns an array of values' do
66
+ grid_var.value.should == %w(Immanuel Michi)
67
+ end
68
+ end
69
+
70
+ describe '#custom_structure_values' do
71
+ let :custom_structure do
72
+ Brandmaker::CustomStructure.new :objects => [
73
+ {:name => 'Immanuel', :label => 'Immanuel X'},
74
+ {:name => 'Michi', :label => 'Michi X'}
75
+ ]
76
+ end
77
+
78
+ let :grid_var do
79
+ g = Brandmaker::GridVariable.new(grid_variable_data)
80
+ g.stub(:custom_structure).and_return custom_structure
81
+ g
82
+ end
83
+
84
+ it 'returns the mapped custom structure values' do
85
+ grid_var.custom_structure_values.should == ['Immanuel X', 'Michi X']
86
+ end
87
+ end
88
+
89
+ describe '#extract_email' do
90
+ it 'parses the email address from the value' do
91
+ grid_var = Brandmaker::GridVariable.new({})
92
+ grid_var.stub!(:custom_structure).and_return(nil)
93
+ address = grid_var.extract_email('Immanuel (immanuel.haeussermann@screenconcept.ch)')
94
+ address.should == 'immanuel.haeussermann@screenconcept.ch'
95
+ end
96
+ end
97
+
98
+ describe '#rows' do
99
+ context 'when there is only a single row returned as hash from the API' do
100
+ let :grid_var do
101
+ Brandmaker::GridVariable.new grid_variable_single_row_data
102
+ end
103
+
104
+ it 'returns an array' do
105
+ grid_var.rows.should be_a(Array)
106
+ end
107
+
108
+ it 'does not fail' do
109
+ expect { grid_var.rows }.to_not raise_error
110
+ end
111
+ end
112
+
113
+ context 'when there are multiple rows returns as array from the API' do
114
+ let :grid_var do
115
+ Brandmaker::GridVariable.new grid_variable_data
116
+ end
117
+
118
+ it 'returns an array' do
119
+ grid_var.rows.should be_a(Array)
120
+ end
121
+
122
+ it 'does not fail' do
123
+ expect { grid_var.rows }.to_not raise_error
124
+ end
125
+ end
126
+ end
127
+
128
+ describe '#custom_structure' do
129
+ context 'when the technical name is mapped' do
130
+ before do
131
+ Brandmaker::CustomStructure.should_receive(:find_all_custom_structures)
132
+ .and_return(mock(Object, :body => custom_structure_all_data))
133
+ end
134
+
135
+ it 'returns a custom structure' do
136
+ grid_var = Brandmaker::GridVariable.new({:technical_name => 'anbieter_hinzufgen'})
137
+ grid_var.custom_structure.should be_a(Brandmaker::CustomStructure)
138
+ end
139
+ end
140
+
141
+ context 'when the technical name is not mapped' do
142
+ before do
143
+ Brandmaker::CustomStructure.should_not_receive(:find_all_custom_structures)
144
+ end
145
+
146
+ it 'returns nil' do
147
+ grid_var = Brandmaker::GridVariable.new({:technical_name => 'anything'})
148
+ grid_var.custom_structure.should be_nil
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end