middleman-headless 0.3.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 60d9db815ea80cd14b4c15235a0bcb23dade91a5
4
+ data.tar.gz: bd21422ab42088fb4b7dcc052641d9f75661adb7
5
+ SHA512:
6
+ metadata.gz: 0f81a9b100c7a56e7eea9d5ec41479814fccc4f8fec9090e66848005eb526e5fac0a16b91140e29ce0595f41168b3e33d5fe86b43d825febd0c709b072bb34b1
7
+ data.tar.gz: 41cbd4124e95f43e1ff4366c0e3ebbcfaffa8fd69318fc4746cab985ae96301fe662d6a0d5b0138a0e28d760e1c696f73fcfd5fdb0ac29647d5fcd03c750b704
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ # Ignore bundler lock file
2
+ /Gemfile.lock
3
+
4
+ # Ignore pkg folder
5
+ /pkg
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Two Many Projects (Joël Gähwiler, Adina Renner)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # middleman-headless
2
+
3
+ **Middleman extension to load content from the Headless Content Management System.**
@@ -0,0 +1,6 @@
1
+ require 'middleman-core'
2
+
3
+ Middleman::Extensions.register :headless do
4
+ require 'middleman-headless/extension'
5
+ MiddlemanHeadless::Extension
6
+ end
@@ -0,0 +1,37 @@
1
+ require 'middleman-core'
2
+
3
+ require 'middleman-headless/interface'
4
+
5
+ module MiddlemanHeadless
6
+ class Extension < ::Middleman::Extension
7
+ option :address, 'https://0.0.0.0:4000', 'The Headless address'
8
+ option :app_key, nil, 'The applications key used for authentication'
9
+ option :app_secret, nil, 'The applications secret used for authentication"'
10
+ option :verify, true, 'Certificates are verified by default'
11
+ option :space, nil, 'The space to be used'
12
+ option :preview, false, 'Enable preview mode'
13
+
14
+ def initialize(app, options_hash={}, &block)
15
+ super
16
+ require 'oauth2'
17
+
18
+ app.before do
19
+ extensions[:headless].clear unless app.build?
20
+ end
21
+ end
22
+
23
+ def interface
24
+ @interface ||= Interface.new(options)
25
+ end
26
+
27
+ def clear
28
+ @interface = nil
29
+ end
30
+
31
+ helpers do
32
+ def headless
33
+ extensions[:headless].interface
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,203 @@
1
+ require 'active_support/core_ext/hash/indifferent_access'
2
+
3
+ module MiddlemanHeadless
4
+ class Interface
5
+ attr_reader :options
6
+
7
+ def initialize(options)
8
+ @options = options
9
+ @entries_cache = {}
10
+ @asset_cache = {}
11
+
12
+ @client = OAuth2::Client.new(
13
+ @options.app_key,
14
+ @options.app_secret,
15
+ site: @options.address,
16
+ token_url: '/auth/token',
17
+ ssl: {
18
+ verify: @options.verify
19
+ }
20
+ )
21
+
22
+ @access_token = @client.client_credentials.get_token scope: ''
23
+ end
24
+
25
+ def space
26
+ @space ||= Space.new(get("space/#{@options.space}").with_indifferent_access, self)
27
+ end
28
+
29
+ def entries(content_type)
30
+ content_type = content_type[:slug] if content_type.is_a?(Hash)
31
+ path = "entries/#{@options.space}/#{content_type}"
32
+ @entries_cache[content_type.to_sym] ||= get(path).map do |item|
33
+ Entry.new(item.with_indifferent_access, self)
34
+ end
35
+ end
36
+
37
+ def entry(content_type, id)
38
+ entries(content_type).find do |item|
39
+ item.id == id
40
+ end
41
+ end
42
+
43
+ def asset(id)
44
+ return nil if id.nil?
45
+ path = "asset/#{@options.space}/#{id}"
46
+ @asset_cache[id.to_sym] ||= Asset.new(get(path).with_indifferent_access, self)
47
+ end
48
+
49
+ def token
50
+ @access_token.token
51
+ end
52
+
53
+ def method_missing(key)
54
+ entries(key.to_s)
55
+ end
56
+
57
+ protected
58
+
59
+ def get(path)
60
+ path = "/content/#{path.to_s}"
61
+ path = "#{path}?preview=enabled" if @options.preview
62
+ JSON.parse(@access_token.get(path).body)
63
+ end
64
+ end
65
+
66
+ class Item
67
+ def initialize(data, interface)
68
+ @data = data
69
+ @interface = interface
70
+ end
71
+
72
+ def method_missing(key)
73
+ @data[key]
74
+ end
75
+ end
76
+
77
+ class Space < Item
78
+ def content_types
79
+ @data[:content_types].map do |item|
80
+ Item.new(item, @interface)
81
+ end
82
+ end
83
+
84
+ def languages
85
+ @data[:languages].map do |item|
86
+ Item.new(item, @interface)
87
+ end
88
+ end
89
+ end
90
+
91
+ class Entry
92
+ def initialize(data, interface)
93
+ @data = data
94
+ @interface = interface
95
+ end
96
+
97
+ def id
98
+ @data[:id]
99
+ end
100
+
101
+ def name
102
+ @data[:name]
103
+ end
104
+
105
+ def version(key)
106
+ return nil if @data[:versions][key].nil?
107
+ Version.new(@data[:versions][key], @interface)
108
+ end
109
+
110
+ def field(key)
111
+ v = version(I18n.locale)
112
+ return nil if v.nil?
113
+ v.field(key)
114
+ end
115
+
116
+ def asset(key)
117
+ @interface.asset(field(key))
118
+ end
119
+
120
+ def assets(key)
121
+ field(key).map do |value|
122
+ @interface.asset(value)
123
+ end
124
+ end
125
+
126
+ def reference(key, type=nil)
127
+ type = key if type.nil?
128
+ Reference.new(type, field(key), @interface)
129
+ end
130
+
131
+ def references(key, type=nil)
132
+ type = key if type.nil?
133
+ field(key).map do |value|
134
+ Reference.new(type, value, @interface)
135
+ end
136
+ end
137
+
138
+ def method_missing(key)
139
+ field(key)
140
+ end
141
+ end
142
+
143
+ class Version
144
+ def initialize(data, interface)
145
+ @data = data
146
+ @interface = interface
147
+ end
148
+
149
+ def field(key)
150
+ @data[:fields][key]
151
+ end
152
+
153
+ def updated_at
154
+ @data[:updated_at]
155
+ end
156
+
157
+ def published_at
158
+ @data[:published_at]
159
+ end
160
+
161
+ def method_missing(key)
162
+ field(key)
163
+ end
164
+ end
165
+
166
+ class Asset
167
+ def initialize(data, interface)
168
+ @data = data
169
+ @interface = interface
170
+ end
171
+
172
+ def key
173
+ @data[:key]
174
+ end
175
+
176
+ def name
177
+ @data[:name]
178
+ end
179
+
180
+ def url(options={})
181
+ opts = options.length > 0 ? "?#{options.to_query}" : ''
182
+ "#{@interface.options.address}/content/file/#{key}#{opts}"
183
+ end
184
+ end
185
+
186
+ class Reference
187
+ def initialize(type, id, interface)
188
+ @type = type
189
+ @id = id
190
+ @interface = interface
191
+ end
192
+
193
+ def entry
194
+ @interface.entry(@type, @id)
195
+ end
196
+
197
+ def method_missing(*args)
198
+ e = entry
199
+ return nil if e.nil?
200
+ entry.send(*args)
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'middleman-headless'
6
+ s.version = '0.3.0'
7
+ s.licenses = ['MIT']
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Joël Gähwiler']
10
+ s.email = ['joel@twomanyprojects.com']
11
+ s.homepage = 'https://github.com/twomanyprojects/middleman-headless'
12
+ s.summary = %q{Middleman extension to load content from the Headless Content Management System.}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ['lib']
18
+
19
+ s.add_runtime_dependency('middleman-core', ['~> 4.1'])
20
+ s.add_runtime_dependency('oauth2', ['~> 1.2'])
21
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-headless
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Joël Gähwiler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: middleman-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ description:
42
+ email:
43
+ - joel@twomanyprojects.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.md
51
+ - README.md
52
+ - lib/middleman-headless.rb
53
+ - lib/middleman-headless/extension.rb
54
+ - lib/middleman-headless/interface.rb
55
+ - middleman-headless.gemspec
56
+ homepage: https://github.com/twomanyprojects/middleman-headless
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.5.2
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Middleman extension to load content from the Headless Content Management
80
+ System.
81
+ test_files: []