typekit-client 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -1
  3. data/README.md +1 -4
  4. data/bin/typekit +0 -8
  5. data/lib/typekit.rb +3 -5
  6. data/lib/typekit/client.rb +28 -21
  7. data/lib/typekit/core.rb +23 -8
  8. data/lib/typekit/helper.rb +0 -40
  9. data/lib/typekit/processing.rb +0 -1
  10. data/lib/typekit/processing/converter.rb +2 -1
  11. data/lib/typekit/processing/converter/unknown.rb +2 -1
  12. data/lib/typekit/processing/translator.rb +6 -9
  13. data/lib/typekit/record.rb +2 -2
  14. data/lib/typekit/version.rb +1 -1
  15. data/spec/spec_helper.rb +1 -0
  16. data/spec/typekit/client_spec.rb +1 -3
  17. data/spec/typekit/helper_spec.rb +0 -88
  18. data/spec/typekit/processing/converter_spec.rb +1 -2
  19. data/spec/typekit/record/base_spec.rb +0 -1
  20. data/spec/typekit/record_spec.rb +0 -1
  21. data/typekit-client.gemspec +3 -3
  22. metadata +18 -41
  23. data/lib/typekit/configuration.rb +0 -16
  24. data/lib/typekit/configuration/base.rb +0 -21
  25. data/lib/typekit/configuration/default.rb +0 -38
  26. data/lib/typekit/connection.rb +0 -10
  27. data/lib/typekit/connection/adaptor.rb +0 -13
  28. data/lib/typekit/connection/adaptor/standard.rb +0 -32
  29. data/lib/typekit/connection/dispatcher.rb +0 -17
  30. data/lib/typekit/connection/request.rb +0 -26
  31. data/lib/typekit/connection/response.rb +0 -16
  32. data/lib/typekit/processing/parser.rb +0 -14
  33. data/lib/typekit/processing/parser/json.rb +0 -15
  34. data/lib/typekit/processing/parser/yaml.rb +0 -15
  35. data/lib/typekit/routing.rb +0 -9
  36. data/lib/typekit/routing/mapper.rb +0 -43
  37. data/lib/typekit/routing/node.rb +0 -5
  38. data/lib/typekit/routing/node/base.rb +0 -46
  39. data/lib/typekit/routing/node/collection.rb +0 -34
  40. data/lib/typekit/routing/node/operation.rb +0 -32
  41. data/lib/typekit/routing/node/root.rb +0 -8
  42. data/lib/typekit/routing/node/scope.rb +0 -19
  43. data/lib/typekit/routing/proxy.rb +0 -17
  44. data/spec/typekit/configuration_spec.rb +0 -50
  45. data/spec/typekit/connection/adaptor_spec.rb +0 -24
  46. data/spec/typekit/connection/dispatcher_spec.rb +0 -36
  47. data/spec/typekit/connection/request_spec.rb +0 -13
  48. data/spec/typekit/connection/response_spec.rb +0 -18
  49. data/spec/typekit/processing/parser_spec.rb +0 -23
  50. data/spec/typekit/routing/mapper_spec.rb +0 -177
  51. data/spec/typekit/routing/node_spec.rb +0 -61
@@ -1,5 +0,0 @@
1
- require_relative 'node/base'
2
- require_relative 'node/root'
3
- require_relative 'node/scope'
4
- require_relative 'node/collection'
5
- require_relative 'node/operation'
@@ -1,46 +0,0 @@
1
- module Typekit
2
- module Routing
3
- module Node
4
- class Base
5
- def append(child)
6
- children << child
7
- end
8
-
9
- def assemble(request, path)
10
- process(request, path)
11
- return authorize(request) if path.empty?
12
- lookup!(path.first).assemble(request, path)
13
- end
14
-
15
- def match(name)
16
- end
17
-
18
- def process(request, path)
19
- end
20
-
21
- def permitted?(request)
22
- end
23
-
24
- protected
25
-
26
- def children
27
- @children ||= []
28
- end
29
-
30
- def lookup(name)
31
- children.find { |c| c.match(name) }
32
- end
33
-
34
- def lookup!(name)
35
- lookup(name) or raise Error, 'Not found'
36
- end
37
-
38
- def authorize(request)
39
- raise Error, 'Not permitted' unless permitted?(request)
40
- request.sign(self)
41
- request
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,34 +0,0 @@
1
- module Typekit
2
- module Routing
3
- module Node
4
- class Collection < Base
5
- def initialize(name, only: nil)
6
- @name = name
7
- @actions = only && Array(only) || Typekit.actions
8
- unless (@actions - Typekit.actions).empty?
9
- raise Error, 'Not supported'
10
- end
11
- end
12
-
13
- def match(name)
14
- @name == name
15
- end
16
-
17
- def process(request, path)
18
- request << path.shift # @name
19
- return request if path.empty?
20
- request << path.shift # id
21
- end
22
-
23
- def permitted?(request)
24
- return false unless @actions.include?(request.action)
25
-
26
- id_present = request.path.last != @name
27
- member_action = Helper.member_action?(request.action)
28
-
29
- id_present == member_action
30
- end
31
- end
32
- end
33
- end
34
- end
@@ -1,32 +0,0 @@
1
- module Typekit
2
- module Routing
3
- module Node
4
- class Operation < Base
5
- def initialize(name, action:, on:, **options)
6
- # TODO: how about on == :collection?
7
- unless Typekit.actions.include?(action) && on == :member
8
- raise Error, 'Not supported'
9
- end
10
- @name = name
11
- @action = action
12
- end
13
-
14
- def match(name)
15
- if @name.is_a?(String) && @name =~ /^:/
16
- true
17
- else
18
- @name == name
19
- end
20
- end
21
-
22
- def process(request, path)
23
- request << path.shift # @name
24
- end
25
-
26
- def permitted?(request)
27
- @action == request.action
28
- end
29
- end
30
- end
31
- end
32
- end
@@ -1,8 +0,0 @@
1
- module Typekit
2
- module Routing
3
- module Node
4
- class Root < Base
5
- end
6
- end
7
- end
8
- end
@@ -1,19 +0,0 @@
1
- module Typekit
2
- module Routing
3
- module Node
4
- class Scope < Base
5
- def initialize(path)
6
- @path = Array(path)
7
- end
8
-
9
- def match(name)
10
- !lookup(name).nil?
11
- end
12
-
13
- def process(request, path)
14
- @path.each { |chunk| request << chunk }
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,17 +0,0 @@
1
- module Typekit
2
- module Routing
3
- class Proxy
4
- def initialize(owner, **options)
5
- @owner = owner
6
- @options = options
7
- end
8
-
9
- def method_missing(name, *arguments, **options, &block)
10
- name = :"define_#{ name }"
11
- return super unless @owner.respond_to?(name)
12
- # NOTE: https://bugs.ruby-lang.org/issues/9776
13
- @owner.send(name, *arguments, **options, **@options, &block)
14
- end
15
- end
16
- end
17
- end
@@ -1,50 +0,0 @@
1
- require 'spec_helper'
2
- require 'typekit'
3
-
4
- describe Typekit::Configuration do
5
- let(:subject_module) { Typekit::Configuration }
6
-
7
- def build(name, **options)
8
- subject_module.build(name, **options)
9
- end
10
-
11
- describe '.build' do
12
- it 'requies an API token to be given' do
13
- expect { build(:default) }.to \
14
- raise_error(subject_module::Error, /Not enough arguments/i)
15
- end
16
-
17
- it 'returns registeries of settings' do
18
- expect(build(:default, token: 'nekot')).to \
19
- be_kind_of(subject_module::Base)
20
- end
21
-
22
- it 'raises exceptions when encouters unknown registeries' do
23
- expect { build(:awesome, token: 'nekot') }.to \
24
- raise_error(subject_module::Error, /Unknown configuration/i)
25
- end
26
- end
27
-
28
- describe 'Base' do
29
- describe '#mapper' do
30
- it 'returns a Mapper' do
31
- expect(build(:default, token: 'nekot').mapper).to \
32
- be_kind_of(Typekit::Routing::Mapper)
33
- end
34
- end
35
-
36
- describe '#dispatcher' do
37
- it 'returns a Dispatcher' do
38
- expect(build(:default, token: 'nekot').dispatcher).to \
39
- be_kind_of(Typekit::Connection::Dispatcher)
40
- end
41
- end
42
-
43
- describe '#translator' do
44
- it 'returns a Translator' do
45
- expect(build(:default, token: 'nekot').translator).to \
46
- be_kind_of(Typekit::Processing::Translator)
47
- end
48
- end
49
- end
50
- end
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
- require 'typekit'
3
-
4
- describe Typekit::Connection::Adaptor do
5
- let(:address) { 'https://typekit.com/api/v1/json/kits' }
6
-
7
- [ 'Standard' ].each do |adaptor|
8
- subject { Typekit::Connection::Adaptor.const_get(adaptor).new }
9
-
10
- describe "#{ adaptor }.process" do
11
- it 'returns the code, headers, and body of the response' do
12
- stub_http_request(:get, address).to_return(
13
- code: '200', body: 'Hej!', headers: { 'a' => 'b' } )
14
- response = subject.process(:get, address)
15
- expect(response).to eq([ '200', { 'a' => [ 'b' ] }, 'Hej!' ])
16
- end
17
-
18
- it 'raises exceptions when encounters unknown methods' do
19
- expect { subject.process(:smile, address) }.to \
20
- raise_error(Typekit::Connection::Error, /Invalid method/i)
21
- end
22
- end
23
- end
24
- end
@@ -1,36 +0,0 @@
1
- require 'spec_helper'
2
- require 'typekit'
3
-
4
- describe Typekit::Connection::Dispatcher do
5
- extend RESTHelper
6
-
7
- let(:token) { 'arbitrary' }
8
- let(:address) { 'https://typekit.com/api/v1/json/kits' }
9
- let(:subject) { Typekit::Connection::Dispatcher.new(token: token) }
10
-
11
- def create_request(action)
12
- double('Request', action: action, address: address, parameters: {})
13
- end
14
-
15
- describe '#process' do
16
- restful_actions.each do |action|
17
- method = rest_http_dictionary[action]
18
-
19
- context "when sending #{ action } Requests" do
20
- it 'sets the token header' do
21
- stub = stub_http_request(method, address)
22
- response = subject.process(create_request(action))
23
- expect(stub).to have_requested(method, address).
24
- with(:headers => { 'X-Typekit-Token' => token })
25
- end
26
-
27
- it 'returns Responses' do
28
- stub_http_request(method, address).
29
- to_return(code: '200', body: 'Hej!')
30
- response = subject.process(create_request(action))
31
- expect([ response.code, response.body ]).to eq([ 200, 'Hej!' ])
32
- end
33
- end
34
- end
35
- end
36
- end
@@ -1,13 +0,0 @@
1
- require 'spec_helper'
2
- require 'typekit'
3
-
4
- describe Typekit::Connection::Request do
5
- subject { Typekit::Connection::Request.new(action: :show) }
6
-
7
- describe '#address' do
8
- it 'assembles the address' do
9
- [ :kits, 'xxx', :families, 'yyy' ].each { |chunk| subject << chunk }
10
- expect(subject.address).to eq('kits/xxx/families/yyy')
11
- end
12
- end
13
- end
@@ -1,18 +0,0 @@
1
- require 'spec_helper'
2
- require 'typekit'
3
-
4
- describe Typekit::Connection::Response do
5
- let(:subject_class) { Typekit::Connection::Response }
6
-
7
- def create(code: 200, body: '')
8
- subject_class.new(code: code, body: body)
9
- end
10
-
11
- it 'is considered to be successful for HTTP OK' do
12
- expect(create(code: 200)).to be_success
13
- end
14
-
15
- it 'is considered to be successful for HTTP Found' do
16
- expect(create(code: 302)).to be_success
17
- end
18
- end
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
- require 'typekit'
3
-
4
- describe Typekit::Processing::Parser do
5
- let(:subject_class) { Typekit::Processing::Parser }
6
-
7
- it 'supports JSON' do
8
- subject = subject_class.build(:json)
9
- result = subject.process('{ "kits": [] }')
10
- expect(result).to eq("kits" => [])
11
- end
12
-
13
- it 'supports YAML' do
14
- subject = subject_class.build(:yaml)
15
- result = subject.process("---\nkits: []")
16
- expect(result).to eq("kits" => [])
17
- end
18
-
19
- it 'does not support XML' do
20
- expect { subject_class.build(:xml) }.to \
21
- raise_error(Typekit::Processing::Error, /Unknown format/i)
22
- end
23
- end
@@ -1,177 +0,0 @@
1
- require 'spec_helper'
2
- require 'typekit'
3
-
4
- describe Typekit::Routing::Mapper do
5
- extend RESTHelper
6
-
7
- def create_request(action)
8
- # TODO: mock?
9
- Typekit::Connection::Request.new(action: action)
10
- end
11
-
12
- describe '#define' do
13
- it 'declares new resources' do
14
- subject.define { resources(:kits) }
15
- request = subject.trace(create_request(:index), [ :kits ])
16
- expect(request.address).to eq('kits')
17
- end
18
-
19
- it 'declares nested resources' do
20
- subject.define { resources(:kits) { resources(:families) } }
21
- request = subject.trace(create_request(:show),
22
- [ :kits, 'xxx', :families, 'yyy' ])
23
- expect(request.address).to eq('kits/xxx/families/yyy')
24
- end
25
-
26
- it 'declares scoped resources' do
27
- subject.define do
28
- scope 'https://typekit.com/api' do
29
- scope [ 'v1', :json ] do
30
- resources(:kits) { resources(:families) }
31
- end
32
- end
33
- end
34
- request = subject.trace(create_request(:show),
35
- [ :kits, 'xxx', :families, 'yyy' ])
36
- expect(request.address).to eq(
37
- 'https://typekit.com/api/v1/json/kits/xxx/families/yyy')
38
- end
39
-
40
- it 'declares custom operations' do
41
- subject.define { resources(:kits) { show(:published, on: :member) } }
42
- request = subject.trace(create_request(:show),
43
- [ :kits, 'xxx', :published ])
44
- expect(request.address).to eq('kits/xxx/published')
45
- end
46
-
47
- it 'declares custom operations with variable names' do
48
- subject.define do
49
- resources :kits do
50
- resources :families do
51
- show ':variant', on: :member
52
- end
53
- end
54
- end
55
- request = subject.trace(create_request(:show),
56
- [ :kits, 'xxx', :families, 'yyy', 'zzz' ])
57
- expect(request.address).to eq('kits/xxx/families/yyy/zzz')
58
- end
59
- end
60
-
61
- describe '#trace' do
62
- shared_examples 'adequate resource tracer' do
63
- restful_member_actions.each do |action|
64
- it "assembles addresses of #{ action } Requests" do
65
- request = subject.trace(create_request(action),
66
- [ *subject_path, 'xxx' ])
67
- expect(request.address).to eq("#{ subject_address }/xxx")
68
- end
69
- end
70
-
71
- restful_collection_actions.each do |action|
72
- it "assembles addresses of #{ action } Requests" do
73
- request = subject.trace(create_request(action), subject_path)
74
- expect(request.address).to eq(subject_address)
75
- end
76
- end
77
-
78
- restful_member_actions.each do |action|
79
- it "raises exceptions for #{ action } actions to collections" do
80
- expect do
81
- subject.trace(create_request(action), subject_path)
82
- end.to raise_error(Typekit::Routing::Error, /Not permitted/i)
83
- end
84
- end
85
-
86
- restful_collection_actions.each do |action|
87
- it "raises exceptions for #{ action } actions to members" do
88
- expect do
89
- subject.trace(create_request(action), [ *subject_path, 'xxx' ])
90
- end.to raise_error(Typekit::Routing::Error, /Not permitted/i)
91
- end
92
- end
93
- end
94
-
95
- context 'when working with plain collections' do
96
- before(:each) do
97
- subject.define { resources(:kits) }
98
- end
99
-
100
- let(:subject_path) { [ :kits ] }
101
- let(:subject_address) { 'kits' }
102
-
103
- it_behaves_like 'adequate resource tracer'
104
- end
105
-
106
- context 'when working with nested collections' do
107
- before(:each) do
108
- subject.define do
109
- resources :kits do
110
- resources :families
111
- end
112
- end
113
- end
114
-
115
- let(:subject_path) { [ :kits, 'yyy', :families ] }
116
- let(:subject_address) { 'kits/yyy/families' }
117
-
118
- it_behaves_like 'adequate resource tracer'
119
- end
120
-
121
- family_actions = [ :show, :update, :delete ]
122
-
123
- context "when only #{ family_actions.join(', ') } are allowed" do
124
- before(:each) do
125
- subject.define do
126
- resources :kits do
127
- resources :families, only: family_actions
128
- end
129
- end
130
- end
131
-
132
- (restful_collection_actions - family_actions).each do |action|
133
- it "raises expections for #{ action } actions" do
134
- expect do
135
- subject.trace(create_request(action),
136
- [ :kits, 'xxx', :families ])
137
- end.to raise_error(Typekit::Routing::Error, /Not permitted/i)
138
- end
139
- end
140
-
141
- (restful_member_actions - family_actions).each do |action|
142
- it "raises expections for #{ action } actions" do
143
- expect do
144
- subject.trace(create_request(action),
145
- [ :kits, 'xxx', :families, 'yyy' ])
146
- end.to raise_error(Typekit::Routing::Error, /Not permitted/i)
147
- end
148
- end
149
- end
150
-
151
- restful_actions.each do |action|
152
- it "assembles addresses for custom #{ action } actions" do
153
- subject.define do
154
- resources :kits do
155
- send(action, :havefun, on: :member)
156
- end
157
- end
158
- request = subject.trace(create_request(action),
159
- [ :kits, 'xxx', :havefun ])
160
- expect(request.address).to eq('kits/xxx/havefun')
161
- end
162
- end
163
-
164
- it 'does not support reopening of resource declarations' do
165
- subject.define do
166
- resources :kits
167
- resources :kits do
168
- resources :families
169
- end
170
- end
171
- expect do
172
- subject.trace(create_request(:show),
173
- [ :kits, 'xxx', :families, 'yyy' ])
174
- end.to raise_error(Typekit::Routing::Error, /Not found/i)
175
- end
176
- end
177
- end