middleman-dato 0.0.1.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed9565a5d3246be461c39df52db283447c82ef46
4
+ data.tar.gz: 20d797f3c0f3af1a314748be70f640d875001608
5
+ SHA512:
6
+ metadata.gz: 95f56a75ae771fe6815c033339baeeb5d54be9905159f615b4efc7cdc51b4464ba0404a8f18910202b4903d3bf2850743f3f153ddfd76dccd51228ceffa77fe1
7
+ data.tar.gz: e8e6431de61cc373adfdd3874af67d8129892066db65522d264bd2cd4d2781b44e576f0bd08d8aa832eed8cfbebb235081fffda6403f1add0d781ab5b8890981
@@ -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,19 @@
1
+ # If you do not have OpenSSL installed, update
2
+ # the following line to use "http://" instead
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in middleman-dato.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem 'rake'
10
+ gem 'rdoc'
11
+ gem 'yard'
12
+ end
13
+
14
+ group :test do
15
+ gem 'cucumber'
16
+ gem 'fivemat'
17
+ gem 'aruba'
18
+ gem 'rspec'
19
+ end
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'cucumber/rake/task'
5
+
6
+ Cucumber::Rake::Task.new(:cucumber, 'Run features that should pass') do |t|
7
+ t.cucumber_opts = "--color --tags ~@wip --strict --format #{ENV['CUCUMBER_FORMAT'] || 'Fivemat'}"
8
+ end
9
+
10
+ require 'rake/clean'
11
+
12
+ task test: ['cucumber']
13
+
14
+ task default: :test
@@ -0,0 +1,45 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+ require "json"
4
+
5
+ module Dato
6
+ class Client
7
+ def initialize(host, domain, token)
8
+ @host, @domain, @token = host, domain, token
9
+ end
10
+
11
+ def space
12
+ get("space").body
13
+ end
14
+
15
+ def records
16
+ get("records").body
17
+ end
18
+
19
+ def get(*args)
20
+ connection.get(*args)
21
+ rescue Faraday::ClientError => e
22
+ body = JSON.parse(e.response[:body])
23
+ puts JSON.pretty_generate(body)
24
+ raise e
25
+ end
26
+
27
+ def connection
28
+ options = {
29
+ url: @host,
30
+ headers: {
31
+ "Content-Type" => "application/json",
32
+ "Accept" => "application/json",
33
+ "X-Space-Domain" => @domain,
34
+ "Authorization" => "Api-Key #{@token}"
35
+ }
36
+ }
37
+ @connection ||= Faraday.new(options) do |c|
38
+ c.request :json
39
+ c.response :json, content_type: /\bjson$/
40
+ c.response :raise_error
41
+ c.adapter :net_http
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ require "imgix"
2
+
3
+ module Dato
4
+ class File
5
+ attr_reader :attributes
6
+
7
+ def initialize(data)
8
+ @attributes = data.with_indifferent_access
9
+ end
10
+
11
+ def file
12
+ @file ||= Imgix::Client.new(host: 'dato-images.imgix.net')
13
+ .path(attributes[:path])
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,43 @@
1
+ require 'middleman-core'
2
+ require 'dato/repo'
3
+ require 'ostruct'
4
+
5
+ module Dato
6
+ class MiddlemanExtension < ::Middleman::Extension
7
+ option :domain, nil, 'Space domain'
8
+ option :token, nil, 'Space API token'
9
+ option :api_host, 'http://dato-api.herokuapp.com', 'Space API token'
10
+
11
+ attr_reader :records
12
+
13
+ def initialize(app, options_hash={}, &block)
14
+ super
15
+
16
+ Repo.instance.connection_options = options
17
+ Repo.instance.sync!
18
+
19
+ app.before do
20
+ unless build?
21
+ print "Syncing Dato space... "
22
+ Repo.instance.sync!
23
+ puts "done."
24
+ end
25
+ true
26
+ end
27
+
28
+ app.send :include, InstanceMethods
29
+ end
30
+
31
+ module InstanceMethods
32
+ def dato
33
+ OpenStruct.new(Repo.instance.records_per_content_type)
34
+ end
35
+ end
36
+
37
+ helpers do
38
+ def dato
39
+ OpenStruct.new(Repo.instance.records_per_content_type)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,51 @@
1
+ require "dato/file"
2
+
3
+ module Dato
4
+ class Record
5
+ attr_reader :attributes, :fields, :singleton
6
+
7
+ def initialize(attributes, content_type)
8
+ @attributes = attributes.with_indifferent_access
9
+ @singleton = content_type[:singleton]
10
+ @fields = content_type[:fields].with_indifferent_access
11
+ end
12
+
13
+ def respond_to?(method, include_private = false)
14
+ if @attributes.has_key?(method)
15
+ true
16
+ else
17
+ super
18
+ end
19
+ end
20
+
21
+ def read_attribute(name)
22
+ attribute = @attributes[name]
23
+
24
+ attribute = if fields[name][:localized]
25
+ attribute[I18n.locale]
26
+ else
27
+ attribute
28
+ end
29
+
30
+ if %w(image file).include? fields[name][:field_type]
31
+ Dato::File.new(attribute)
32
+ elsif fields[name][:field_type] == "date"
33
+ Date.parse(attribute)
34
+ else
35
+ attribute
36
+ end
37
+ end
38
+
39
+ def id
40
+ @attributes[:id]
41
+ end
42
+
43
+ def method_missing(method, *arguments, &block)
44
+ if @attributes.has_key?(method) && arguments.size == 0
45
+ read_attribute(method.to_sym)
46
+ else
47
+ super
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,103 @@
1
+ require 'singleton'
2
+ require 'active_support/inflector/methods'
3
+ require 'dato/client'
4
+ require 'dato/record'
5
+
6
+ module Dato
7
+ class Repo
8
+ include Singleton
9
+
10
+ attr_reader :client, :content_types, :records_per_content_type
11
+
12
+ def connection_options=(options)
13
+ @client = Client.new(
14
+ options[:api_host],
15
+ options[:domain],
16
+ options[:token]
17
+ )
18
+ end
19
+
20
+ def sync!
21
+ @content_types = prepare_content_types(client.space)
22
+ @records_per_content_type = group_by_content_type(client.records)
23
+ end
24
+
25
+ def prepare_content_types(data)
26
+ data = data.with_indifferent_access
27
+
28
+ content_types = Hash[
29
+ data[:data][:links][:content_types][:linkage]
30
+ .map do |content_type|
31
+ [
32
+ content_type[:id],
33
+ content_type_data(content_type[:id], data[:included])
34
+ ]
35
+ end
36
+ ]
37
+ end
38
+
39
+ def content_type_data(content_type, data)
40
+ content_type = data
41
+ .find do |item|
42
+ item[:type] == "content_type" && item[:id] == content_type
43
+ end
44
+
45
+ field_ids = content_type[:links][:fields][:linkage]
46
+ .map { |field| field[:id] }
47
+
48
+ fields = Hash[
49
+ data
50
+ .select do |item|
51
+ item[:type] == "field" && field_ids.include?(item[:id])
52
+ end
53
+ .map do |field|
54
+ [
55
+ field[:attributes][:api_key],
56
+ field[:attributes]
57
+ ]
58
+ end
59
+ ]
60
+
61
+ {
62
+ singleton: content_type[:attributes][:singleton],
63
+ fields: fields
64
+ }
65
+ end
66
+
67
+ def group_by_content_type(data)
68
+ Hash[
69
+ data
70
+ .with_indifferent_access[:data]
71
+ .group_by do |record|
72
+ record[:links][:content_type][:linkage][:id]
73
+ end
74
+ .map do |content_type, records|
75
+ if content_types[content_type][:singleton]
76
+ [ content_type, normalize_record(records.first) ]
77
+ else
78
+ [ content_type.pluralize, group_by_id(records) ]
79
+ end
80
+ end
81
+ ]
82
+ end
83
+
84
+ def group_by_id(records)
85
+ Hash[
86
+ records
87
+ .group_by { |record| record[:id] }
88
+ .map do |id, records|
89
+ [ id, normalize_record(records.first) ]
90
+ end
91
+ ]
92
+ end
93
+
94
+ def normalize_record(record)
95
+ content_type = record[:links][:content_type][:linkage][:id]
96
+
97
+ Record.new(
98
+ record[:attributes].merge(id: record[:id]),
99
+ content_types[content_type]
100
+ )
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ require "dato/middleman_extension"
2
+
3
+ Dato::MiddlemanExtension.register(:dato)
@@ -0,0 +1 @@
1
+ require 'middleman-dato'
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "middleman-dato"
6
+ s.version = "0.0.1.rc1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Stefano Verna"]
9
+ s.email = ["s.verna@cantierecreativo.net"]
10
+ s.homepage = "http://cantierecreativo.net"
11
+ s.summary = %q{Fetches data from a Dato space}
12
+ s.description = %q{Fetches data from a Dato space}
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", [">= 3.3.12"])
20
+ s.add_runtime_dependency("faraday", [">= 0.9.0"])
21
+ s.add_runtime_dependency("faraday_middleware", [">= 0.9.0"])
22
+ s.add_runtime_dependency("imgix", [">= 0.3.1"])
23
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-dato
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Stefano Verna
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-24 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: 3.3.12
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.3.12
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: imgix
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.3.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.3.1
69
+ description: Fetches data from a Dato space
70
+ email:
71
+ - s.verna@cantierecreativo.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Rakefile
79
+ - lib/dato/client.rb
80
+ - lib/dato/file.rb
81
+ - lib/dato/middleman_extension.rb
82
+ - lib/dato/record.rb
83
+ - lib/dato/repo.rb
84
+ - lib/middleman-dato.rb
85
+ - lib/middleman_extension.rb
86
+ - middleman-dato.gemspec
87
+ homepage: http://cantierecreativo.net
88
+ licenses: []
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.3.1
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Fetches data from a Dato space
110
+ test_files: []
111
+ has_rdoc: