typekit-client 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -3
  3. data/.travis.yml +4 -0
  4. data/.yardopts +6 -0
  5. data/CHANGELOG.md +12 -0
  6. data/Guardfile +21 -3
  7. data/README.md +253 -248
  8. data/bin/typekit-client +16 -16
  9. data/bin/typekit-publisher +6 -5
  10. data/lib/typekit.rb +6 -2
  11. data/lib/typekit/client.rb +66 -24
  12. data/lib/typekit/collection.rb +6 -11
  13. data/lib/typekit/collection/base.rb +40 -0
  14. data/lib/typekit/collection/serialization.rb +17 -0
  15. data/lib/typekit/converter.rb +26 -0
  16. data/lib/typekit/converter/boolean.rb +12 -0
  17. data/lib/typekit/converter/collection.rb +13 -0
  18. data/lib/typekit/converter/datetime.rb +12 -0
  19. data/lib/typekit/converter/element.rb +13 -0
  20. data/lib/typekit/converter/errors.rb +12 -0
  21. data/lib/typekit/converter/unknown.rb +13 -0
  22. data/lib/typekit/core.rb +8 -10
  23. data/lib/typekit/element.rb +27 -0
  24. data/lib/typekit/element/association.rb +63 -0
  25. data/lib/typekit/element/base.rb +61 -0
  26. data/lib/typekit/element/persistence.rb +61 -0
  27. data/lib/typekit/element/query.rb +25 -0
  28. data/lib/typekit/element/serialization.rb +17 -0
  29. data/lib/typekit/error.rb +23 -0
  30. data/lib/typekit/helper.rb +59 -10
  31. data/lib/typekit/record.rb +18 -22
  32. data/lib/typekit/record/family.rb +24 -1
  33. data/lib/typekit/record/kit.rb +29 -1
  34. data/lib/typekit/record/library.rb +4 -1
  35. data/lib/typekit/record/variation.rb +13 -1
  36. data/lib/typekit/version.rb +1 -1
  37. data/spec/feature/create_kit_spec.rb +52 -0
  38. data/spec/feature/delete_kit_spec.rb +33 -0
  39. data/spec/feature/index_kits_spec.rb +55 -0
  40. data/spec/feature/publish_kit_spec.rb +15 -0
  41. data/spec/feature/show_family_spec.rb +75 -0
  42. data/spec/feature/show_kit_spec.rb +56 -0
  43. data/spec/feature/show_library_spec.rb +29 -0
  44. data/spec/feature/show_variation_spec.rb +18 -0
  45. data/spec/feature/update_kit_spec.rb +88 -0
  46. data/spec/fixture/cassette/create_kits_bad.yml +16 -0
  47. data/spec/fixture/cassette/create_kits_ok.yml +16 -0
  48. data/spec/fixture/cassette/delete_kits_xxx_not_found.yml +13 -0
  49. data/spec/{cassettes → fixture/cassette}/delete_kits_xxx_ok.yml +0 -3
  50. data/spec/fixture/cassette/index_kits_show_kit_xxx_ok.yml +25 -0
  51. data/spec/{cassettes → fixture/cassette}/index_kits_unauthorized.yml +0 -3
  52. data/spec/{cassettes → fixture/cassette}/show_families_xxx_found.yml +0 -3
  53. data/spec/{cassettes → fixture/cassette}/show_families_xxx_ok.yml +0 -3
  54. data/spec/{cassettes → fixture/cassette}/show_families_xxx_yyy_ok.yml +0 -3
  55. data/spec/fixture/cassette/show_families_yyy_update_kits_xxx_families_ok.yml +35 -0
  56. data/spec/fixture/cassette/show_kits_xxx_families_yyy_show_family_yyy_ok.yml +45 -0
  57. data/spec/fixture/cassette/show_kits_xxx_not_found.yml +13 -0
  58. data/spec/fixture/cassette/show_kits_xxx_show_family_yyy_ok.yml +46 -0
  59. data/spec/{cassettes → fixture/cassette}/show_libraries_xxx_ok.yml +0 -3
  60. data/spec/fixture/cassette/update_kits_xxx_empty_families_ok.yml +16 -0
  61. data/spec/fixture/cassette/update_kits_xxx_families_ok.yml +18 -0
  62. data/spec/fixture/cassette/update_kits_xxx_families_variations_ok.yml +18 -0
  63. data/spec/fixture/cassette/update_kits_xxx_name_ok.yml +17 -0
  64. data/spec/fixture/cassette/update_kits_xxx_publish_ok.yml +16 -0
  65. data/spec/fixture/record/article.rb +11 -0
  66. data/spec/fixture/record/section.rb +10 -0
  67. data/spec/lib/typekit/collection_spec.rb +22 -0
  68. data/spec/{typekit/processing → lib/typekit}/converter_spec.rb +13 -13
  69. data/spec/lib/typekit/element/association_spec.rb +96 -0
  70. data/spec/lib/typekit/element/base_spec.rb +70 -0
  71. data/spec/lib/typekit/element/persistence_spec.rb +97 -0
  72. data/spec/lib/typekit/element/serialization_spec.rb +28 -0
  73. data/spec/lib/typekit/element_spec.rb +58 -0
  74. data/spec/{typekit → lib/typekit}/helper_spec.rb +6 -8
  75. data/spec/lib/typekit/record_spec.rb +57 -0
  76. data/spec/spec_helper.rb +10 -3
  77. data/spec/support/common_helper.rb +5 -0
  78. data/spec/support/record_helper.rb +38 -0
  79. data/typekit-client.gemspec +11 -4
  80. metadata +164 -58
  81. data/lib/typekit/processing.rb +0 -8
  82. data/lib/typekit/processing/converter.rb +0 -28
  83. data/lib/typekit/processing/converter/boolean.rb +0 -14
  84. data/lib/typekit/processing/converter/collection.rb +0 -18
  85. data/lib/typekit/processing/converter/datetime.rb +0 -14
  86. data/lib/typekit/processing/converter/errors.rb +0 -24
  87. data/lib/typekit/processing/converter/record.rb +0 -16
  88. data/lib/typekit/processing/converter/unknown.rb +0 -15
  89. data/lib/typekit/processing/translator.rb +0 -13
  90. data/lib/typekit/record/base.rb +0 -52
  91. data/spec/cassettes/index_kits_ok.yml +0 -16
  92. data/spec/cassettes/show_kits_xxx_families_yyy_ok.yml +0 -16
  93. data/spec/cassettes/show_kits_xxx_ok.yml +0 -17
  94. data/spec/features/client/delete_kit_spec.rb +0 -13
  95. data/spec/features/client/index_kits_spec.rb +0 -25
  96. data/spec/features/client/show_family_spec.rb +0 -47
  97. data/spec/features/client/show_kit_spec.rb +0 -18
  98. data/spec/features/client/show_library_spec.rb +0 -18
  99. data/spec/features/client/show_variation_spec.rb +0 -22
  100. data/spec/support/resource_helper.rb +0 -34
  101. data/spec/typekit/record/base_spec.rb +0 -85
  102. data/spec/typekit/record_spec.rb +0 -80
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require 'fixture/record/article'
3
+ require 'fixture/record/section'
4
+
5
+ RSpec.describe Typekit::Element::Association do
6
+ let(:article_class) { Fixture::Record::Article }
7
+ let(:section_class) { Fixture::Record::Section }
8
+
9
+ describe '.has_many' do
10
+ let(:nested_collection_attributes) do
11
+ [{ title: 'First' }, { title: 'Second' }]
12
+ end
13
+
14
+ shared_examples 'an adequate collection accessor' do
15
+ it 'initializes the relation as a Collection' do
16
+ expect(subject.sections).to be_kind_of(Typekit::Collection::Base)
17
+ end
18
+
19
+ describe 'the Collection' do
20
+ it 'is based on the attributes' do
21
+ expect(subject.sections.map(&:title)).to \
22
+ contain_exactly('First', 'Second')
23
+ end
24
+ end
25
+ end
26
+
27
+ it 'defines a getter method' do
28
+ expect(article_class.new).to respond_to(:sections)
29
+ end
30
+
31
+ describe 'the getter method' do
32
+ context 'when the attributes of the association are given' do
33
+ subject { article_class.new(sections: nested_collection_attributes) }
34
+
35
+ it_behaves_like 'an adequate collection accessor'
36
+ end
37
+
38
+ context 'when the attributes of the association are not given' do
39
+ it 'returns an empty Collection when new' do
40
+ subject = article_class.new
41
+
42
+ expect(subject).not_to receive(:load!)
43
+ expect(subject.sections).to be_kind_of(Typekit::Collection::Base)
44
+ expect(subject.sections).to be_empty
45
+ end
46
+
47
+ it 'loads the association when not new' do
48
+ subject = article_class.new(id: 42)
49
+
50
+ expect(subject).to receive(:load!).and_throw(:break)
51
+ catch(:break) { subject.sections }
52
+ end
53
+ end
54
+ end
55
+
56
+ it 'defines a setter method' do
57
+ expect(article_class.new).to respond_to(:sections=)
58
+ end
59
+
60
+ describe 'the setter method' do
61
+ subject { article_class.new }
62
+
63
+ context 'when receives plain attributes' do
64
+ before(:example) { subject.sections = nested_collection_attributes }
65
+
66
+ it_behaves_like 'an adequate collection accessor'
67
+ end
68
+
69
+ context 'when receives arrays of Elements' do
70
+ before(:example) do
71
+ subject.sections = nested_collection_attributes.map do |attributes|
72
+ section_class.new(attributes)
73
+ end
74
+ end
75
+
76
+ it_behaves_like 'an adequate collection accessor'
77
+ end
78
+ end
79
+ end
80
+
81
+ describe '.belongs_to' do
82
+ it 'defines a getter method' do
83
+ expect(section_class.new).to respond_to(:article)
84
+ end
85
+
86
+ describe 'the getter method' do
87
+ context 'when the attributes of the association are not given' do
88
+ subject { section_class.new }
89
+
90
+ it 'returns nil' do
91
+ expect(subject.article).to be nil
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+ require 'fixture/record/article'
3
+ require 'fixture/record/section'
4
+
5
+ RSpec.describe Typekit::Element::Base do
6
+ let(:article_class) { Fixture::Record::Article }
7
+ let(:section_class) { Fixture::Record::Section }
8
+
9
+ describe '#new' do
10
+ it 'treats options as attributes' do
11
+ subject = article_class.new(title: 'Awesome')
12
+ expect { subject.title = 'Superb' }.to \
13
+ change { subject.title }.from('Awesome').to('Superb')
14
+ end
15
+
16
+ it 'symbolizes the names of the attributes' do
17
+ subject = article_class.new('title' => 'Awesome')
18
+ expect(subject.attributes.keys).to include(:title)
19
+ end
20
+
21
+ it 'appends the id attribute when missing' do
22
+ subject = article_class.new(title: 'Awesome')
23
+ expect(subject.id).to be nil
24
+ end
25
+
26
+ it 'does not affect the id attribute when present' do
27
+ subject = article_class.new(id: 42, title: 'Awesome')
28
+ expect(subject.id).to be 42
29
+ end
30
+ end
31
+
32
+ describe '#attributes' do
33
+ it 'returns all attributes' do
34
+ subject = article_class.new(id: 1, title: 'Awesome')
35
+ expect(subject.attributes).to eq(id: 1, title: 'Awesome')
36
+ end
37
+
38
+ it 'reflects attribute assignments' do
39
+ subject = article_class.new(id: 1)
40
+ subject.id = 42
41
+ expect(subject.attributes).to eq(id: 42)
42
+ end
43
+ end
44
+
45
+ describe '#attribute=' do
46
+ it 'assigns arbitrary attributes' do
47
+ subject = article_class.new
48
+ subject.fourty_second = 42
49
+
50
+ expect(subject.fourty_second).to be 42
51
+ expect(subject.attributes.keys).to include(:fourty_second)
52
+ end
53
+ end
54
+
55
+ describe '#become' do
56
+ subject { article_class.new(id: 1, title: 'Awesome') }
57
+
58
+ it 'copies the attributes of another element' do
59
+ another = article_class.new(id: 2, name: 'Superb')
60
+ subject.become(another)
61
+ expect(subject.attributes).to eq(id: 2, name: 'Superb')
62
+ end
63
+
64
+ it 'raises an exception when the classes do not match' do
65
+ another = section_class.new(name: 'Superb')
66
+ expect { subject.become(another) }.to \
67
+ raise_error(ArgumentError, /Invalid class/i)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+ require 'fixture/record/article'
3
+
4
+ RSpec.describe Typekit::Element::Persistence do
5
+ let(:article_class) { Fixture::Record::Article }
6
+ let(:client) { double }
7
+
8
+ describe '#new?' do
9
+ it 'returns true for new records' do
10
+ subject = article_class.new
11
+ expect(subject).to be_new
12
+ end
13
+
14
+ it 'returns false for records with ids' do
15
+ subject = article_class.new(id: 42)
16
+ expect(subject).not_to be_new
17
+ end
18
+ end
19
+
20
+ describe '#persistent?' do
21
+ it 'returns true for persistent records' do
22
+ subject = article_class.new(id: 42)
23
+ expect(subject).to be_persistent
24
+ end
25
+
26
+ it 'returns false for records without ids' do
27
+ subject = article_class.new
28
+ expect(subject).not_to be_persistent
29
+ end
30
+
31
+ it 'returns false for deleted records' do
32
+ subject = article_class.new(client, id: 42)
33
+ allow(client).to receive(:process).and_return(true)
34
+
35
+ subject.delete
36
+
37
+ expect(subject).not_to be_persistent
38
+ end
39
+ end
40
+
41
+ describe '#save' do
42
+ subject { article_class.new(client) }
43
+
44
+ context 'when successful' do
45
+ before(:example) do
46
+ allow(client).to \
47
+ receive(:process).and_return(article_class.new(id: 42))
48
+ end
49
+
50
+ it 'returns true' do
51
+ expect(subject.save).to be true
52
+ end
53
+
54
+ it 'marks records as not new' do
55
+ expect { subject.save }.to \
56
+ change { subject.new? }.from(true).to(false)
57
+ end
58
+
59
+ it 'marks records as persistent' do
60
+ expect { subject.save }.to \
61
+ change { subject.persistent? }.from(false).to(true)
62
+ end
63
+ end
64
+
65
+ context 'when unsuccessful' do
66
+ before(:example) do
67
+ allow(client).to \
68
+ receive(:process).and_raise(Typekit::ServerError.new(404))
69
+ end
70
+
71
+ it 'returns false' do
72
+ expect(subject.save).to be false
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '#delete' do
78
+ context 'when successful' do
79
+ it 'deletes and marks as deleted if not new' do
80
+ subject = article_class.new(client, id: 42)
81
+ allow(client).to receive(:process).and_return(true)
82
+
83
+ expect(subject).not_to be_deleted
84
+ expect(subject.delete).to be true
85
+ expect(subject).to be_deleted
86
+ end
87
+
88
+ it 'marks as deleted if new' do
89
+ subject = article_class.new
90
+
91
+ expect(subject).not_to be_deleted
92
+ expect(subject.delete).to be true
93
+ expect(subject).to be_deleted
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'fixture/record/article'
3
+ require 'fixture/record/section'
4
+
5
+ RSpec.describe Typekit::Element::Serialization do
6
+ let(:article_class) { Fixture::Record::Article }
7
+ let(:section_class) { Fixture::Record::Section }
8
+
9
+ describe '#serialize' do
10
+ let(:attributes) do
11
+ {
12
+ id: 1,
13
+ title: 'Conversation',
14
+ sections: [{ id: 1, content: 'Hello' }, { id: 2, content: 'Bye' }]
15
+ }
16
+ end
17
+
18
+ subject do
19
+ object = article_class.new(attributes)
20
+ expect(object.sections.first).to be_kind_of(section_class)
21
+ object
22
+ end
23
+
24
+ it 'returns a Hash' do
25
+ expect(subject.serialize).to eq(attributes)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'fixture/record/article'
3
+
4
+ RSpec.describe Typekit::Element do
5
+ include RecordHelper
6
+ extend RecordHelper
7
+
8
+ describe '.dictionary' do
9
+ it 'returns a hash whose keys are symbolic names of Record classes' do
10
+ expect(described_module.dictionary.keys).to \
11
+ include(*plural_record_symbols)
12
+ end
13
+
14
+ it 'returns a hash whose values are Record classes' do
15
+ expect(described_module.dictionary.values).to \
16
+ include(*record_classes)
17
+ end
18
+ end
19
+
20
+ describe '.classify' do
21
+ record_dictionary.each do |name, klass|
22
+ it "converts :#{name} into the corresponding Record class" do
23
+ expect(described_module.classify(name)).to eq(klass)
24
+ end
25
+
26
+ it "converts '#{name}' into the corresponding Record class" do
27
+ expect(described_module.classify(name.to_s)).to eq(klass)
28
+ end
29
+ end
30
+
31
+ plural_record_dictionary.each do |name, klass|
32
+ it "converts :#{name} into the corresponding Record class" do
33
+ expect(described_module.classify(name)).to eq(klass)
34
+ end
35
+
36
+ it "converts '#{name}' into the corresponding Record class" do
37
+ expect(described_module.classify(name.to_s)).to eq(klass)
38
+ end
39
+ end
40
+
41
+ it 'returns nil for unknown names' do
42
+ expect(described_module.classify('smile')).to be nil
43
+ end
44
+
45
+ it 'returns nil for nil' do
46
+ expect(described_module.classify(nil)).to be nil
47
+ end
48
+ end
49
+
50
+ describe '.build' do
51
+ let(:article_class) { Fixture::Record::Article }
52
+ subject { described_module.build(:articles) }
53
+
54
+ it 'creates an instance of an Element class' do
55
+ expect(subject).to be_an_instance_of(article_class)
56
+ end
57
+ end
58
+ end
@@ -1,8 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe Typekit::Helper do
4
- let(:subject_module) { Typekit::Helper }
5
-
6
4
  describe '.pluralize' do
7
5
  {
8
6
  'kit' => 'kits',
@@ -14,8 +12,8 @@ RSpec.describe Typekit::Helper do
14
12
  'variation' => 'variations',
15
13
  'variations' => 'variations'
16
14
  }.each do |k, v|
17
- it "returns #{ v } for #{ k }" do
18
- expect(subject_module.pluralize(k)).to eq(v)
15
+ it "returns #{v} for #{k}" do
16
+ expect(described_module.pluralize(k)).to eq(v)
19
17
  end
20
18
  end
21
19
  end
@@ -31,16 +29,16 @@ RSpec.describe Typekit::Helper do
31
29
  'variation' => 'variation',
32
30
  'variations' => 'variation'
33
31
  }.each do |k, v|
34
- it "returns #{ v } for #{ k }" do
35
- expect(subject_module.singularize(k)).to eq(v)
32
+ it "returns #{v} for #{k}" do
33
+ expect(described_module.singularize(k)).to eq(v)
36
34
  end
37
35
  end
38
36
  end
39
37
 
40
38
  describe '.symbolize_keys' do
41
39
  it 'does what it says' do
42
- expect(subject_module.symbolize_keys('a' => { 'b' => 1 }, 'd' => 2)).to \
43
- eq(a: { 'b' => 1 }, d: 2)
40
+ result = described_module.symbolize_keys('a' => { 'b' => 1 }, 'd' => 2)
41
+ expect(result).to eq(a: { 'b' => 1 }, d: 2)
44
42
  end
45
43
  end
46
44
  end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+ require 'fixture/record/article'
3
+
4
+ RSpec.describe Typekit::Record do
5
+ include RecordHelper
6
+ extend RecordHelper
7
+
8
+ describe '.identify' do
9
+ record_dictionary.each do |name, klass|
10
+ it "converts :#{name} into :element" do
11
+ expect(described_module.identify(name)).to eq(:element)
12
+ end
13
+
14
+ it "converts '#{name}' into :element" do
15
+ expect(described_module.identify(name.to_s)).to eq(:element)
16
+ end
17
+ end
18
+
19
+ plural_record_dictionary.each do |name, klass|
20
+ it "converts :#{name} into :collection" do
21
+ expect(described_module.identify(name)).to eq(:collection)
22
+ end
23
+
24
+ it "converts '#{name}' into :collection" do
25
+ expect(described_module.identify(name.to_s)).to eq(:collection)
26
+ end
27
+ end
28
+
29
+ it 'returns nil for unknown names' do
30
+ expect(described_module.identify('smile')).to be nil
31
+ end
32
+
33
+ it 'returns nil for nil' do
34
+ expect(described_module.identify(nil)).to be nil
35
+ end
36
+ end
37
+
38
+ describe '.build' do
39
+ let(:article_class) { Fixture::Record::Article }
40
+
41
+ let(:client) { double }
42
+ let(:element_class) { described_module.build(:articles, client) }
43
+
44
+ it 'creates a new Element class' do
45
+ expect(element_class < article_class).to be true
46
+ end
47
+
48
+ describe 'an instance of the new Element class' do
49
+ subject { element_class.new }
50
+
51
+ it 'makes use of the provided Client' do
52
+ expect(client).to receive(:process).and_return(element_class.new)
53
+ subject.save
54
+ end
55
+ end
56
+ end
57
+ end