contentful_model 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3fd49f9a5ab20693fb5a3b33e2fa6060c26d748a
4
+ data.tar.gz: bfc894e3c1ecffe1b55b3bd70a6a61e65f46adf2
5
+ SHA512:
6
+ metadata.gz: dc6c95c19d7fda48f82dbdfd5d4f701d689017bb5a47b992a5122132fcb114cfb933056ce287a75ce78d169e21f22075c9bbbd8341800a8f3da628578f83cf31
7
+ data.tar.gz: cdfa041cca206a8906469118311070fce55279290b001a26cd9293426c20ff0365b0e2d3615bee313d0f059e69f321f9f2414226d6c8818db5bc8a296e0d0778
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Error Creative Studio
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ContentfulModel'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,73 @@
1
+ module ContentfulModel
2
+ class Base < Contentful::Entry
3
+ include ContentfulModel::ChainableQueries
4
+
5
+ def initialize(*args)
6
+ super
7
+ self.class.coercions ||= {}
8
+ end
9
+
10
+ #use method_missing to call fields on the model
11
+ def method_missing(method)
12
+ result = fields[:"#{method}"]
13
+ if result.nil?
14
+ raise NoMethodError, "No method or attribute #{method} for #{self}"
15
+ else
16
+ if self.class.coercions[method].nil?
17
+ return result
18
+ else
19
+ return self.class::COERCIONS[self.class.coercions[method]].call(result)
20
+ end
21
+ end
22
+ end
23
+
24
+ def cache_key(*timestamp_names)
25
+ if timestamp_names.present?
26
+ raise ArgumentError, "ContentfulModel::Base models don't support named timestamps."
27
+ end
28
+
29
+ "#{self.class.to_s.underscore}/#{self.id}-#{self.updated_at.utc.to_s(:number)}"
30
+
31
+ end
32
+
33
+ def save
34
+ raise NotImplementedError, "Saving models isn't implemented; we need to use the Contentful Management API for that. Pull requests welcome!"
35
+ end
36
+
37
+ alias_method :create, :save
38
+
39
+ class << self
40
+ attr_accessor :content_type_id, :coercions
41
+
42
+ def descendents
43
+ ObjectSpace.each_object(Class).select { |klass| klass < self }
44
+ end
45
+
46
+ def add_entry_mapping
47
+ unless ContentfulModel.configuration.entry_mapping.has_key?(@content_type_id)
48
+ ContentfulModel.configuration.entry_mapping[@content_type_id] = Object.const_get(self.to_s.to_sym)
49
+ end
50
+ end
51
+
52
+ def client
53
+ self.add_entry_mapping
54
+ @@client ||= Contentful::Client.new(ContentfulModel.configuration.to_hash)
55
+ end
56
+
57
+ def content_type
58
+ client.content_type(@content_type_id)
59
+ end
60
+
61
+ def coerce_field(coercions_hash)
62
+ self.coercions = coercions_hash
63
+ end
64
+
65
+ end
66
+
67
+
68
+
69
+
70
+
71
+
72
+ end
73
+ end
@@ -0,0 +1,74 @@
1
+ module ContentfulModel
2
+ module ChainableQueries
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ base.class_eval do
6
+ cattr_accessor :query
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def get_query
12
+ @query ||= ContentfulModel::Query.new(self)
13
+ end
14
+
15
+ def all
16
+ get_query
17
+ raise ArgumentError, "You need to set self.content_type in your model class" if @content_type_id.nil?
18
+ self
19
+ end
20
+
21
+ def first
22
+ get_query
23
+ @query << {'limit' => 1}
24
+ @query.execute.first
25
+ end
26
+
27
+ def offset(n)
28
+ get_query
29
+ puts @query.inspect
30
+ @query << {'skip' => n}
31
+ self
32
+ end
33
+
34
+ alias_method :skip, :offset
35
+
36
+ def find_by(*args)
37
+ get_query
38
+ args.each do |query|
39
+ #query is a hash
40
+ if query.values.first.is_a?(Array) #we need to do an 'in' query
41
+ @query << {"fields.#{query.keys.first}[in]" => query.values.first.join(",")}
42
+ else
43
+ @query << {"fields.#{query.keys.first}" => query.values.first}
44
+ end
45
+ end
46
+ self
47
+ end
48
+
49
+ def search(parameters)
50
+ get_query
51
+ if parameters.is_a?(Hash)
52
+ parameters.each do |field, search|
53
+ @query << {"fields.#{field}[match]" => search}
54
+ end
55
+ elsif parameters.is_a?(String)
56
+ @query << {"query" => parameters}
57
+ end
58
+ self
59
+ end
60
+
61
+ def load
62
+ @query.execute
63
+ end
64
+
65
+ def find(id)
66
+ get_query
67
+ @query << {'sys.id' => id}
68
+ @query.execute.first
69
+ end
70
+
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,17 @@
1
+ module ContentfulModel
2
+ class Query
3
+ attr_accessor :parameters
4
+ def initialize(reference_class, parameters=nil)
5
+ @parameters = parameters || { 'content_type' => reference_class.content_type_id }
6
+ @client = reference_class.client
7
+ end
8
+
9
+ def <<(parameters)
10
+ @parameters.merge!(parameters)
11
+ end
12
+
13
+ def execute
14
+ @client.entries(@parameters)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module ContentfulModel
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,42 @@
1
+ require 'contentful'
2
+ require "contentful_model/query"
3
+ require "contentful_model/chainable_queries"
4
+ require "contentful_model/base"
5
+ require "active_support"
6
+
7
+ module ContentfulModel
8
+ class << self
9
+ #access the configuration class as ContentfulModel.configuration
10
+ attr_accessor :configuration
11
+
12
+ #block for configuration.
13
+ def configure
14
+ self.configuration ||= Configuration.new
15
+ yield(configuration)
16
+ end
17
+ end
18
+
19
+ class Configuration
20
+ attr_accessor :access_token, :space, :entry_mapping
21
+
22
+ def initialize
23
+ @entry_mapping ||= {}
24
+ end
25
+
26
+ #Rather than listing out all the possible attributes as setters, we have a catchall
27
+ #called 'options' which takes a hash and generates instance vars
28
+ #@param options [Hash]
29
+ def options=(options)
30
+ options.each do |k,v|
31
+ instance_variable_set(:"@#{k}",v)
32
+ end
33
+ end
34
+
35
+
36
+ # Return the Configuration object as a hash, with symbols as keys.
37
+ # @return [Hash]
38
+ def to_hash
39
+ Hash[instance_variables.map { |name| [name.to_s.gsub("@","").to_sym, instance_variable_get(name)] } ]
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :contentful_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contentful_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Error Creative Studio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: contentful
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redcarpet
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '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'
55
+ description: A wrapper around the Contentful gem to give you a base class to inherit
56
+ your models from
57
+ email:
58
+ - hosting@errorstudio.co.uk
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - MIT-LICENSE
64
+ - Rakefile
65
+ - lib/contentful_model.rb
66
+ - lib/contentful_model/base.rb
67
+ - lib/contentful_model/chainable_queries.rb
68
+ - lib/contentful_model/query.rb
69
+ - lib/contentful_model/version.rb
70
+ - lib/tasks/contentful_rails_tasks.rake
71
+ homepage: https://github.com/errorstudio/contentful_model
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: A thin wrapper for Contentful gem
95
+ test_files: []