Skip to content

Commit

Permalink
First commit of contentful_model in its own repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Jones committed Dec 1, 2014
0 parents commit 63fb953
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.bundle/
log/*.log
pkg/
test/dummy/db/*.sqlite3
test/dummy/db/*.sqlite3-journal
test/dummy/log/*.log
test/dummy/tmp/
test/dummy/.sass-cache
.idea/*
.idea/.*
14 changes: 14 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source "https://rubygems.org"

# Declare your gem's dependencies in contentful_rails.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use debugger
# gem 'debugger'
24 changes: 24 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
PATH
remote: .
specs:
contentful_model (0.0.1)
contentful
redcarpet

GEM
remote: https://rubygems.org/
specs:
contentful (0.4.0)
http (~> 0.6)
multi_json (~> 1)
http (0.6.3)
http_parser.rb (~> 0.6.0)
http_parser.rb (0.6.0)
multi_json (1.10.1)
redcarpet (3.2.1)

PLATFORMS
ruby

DEPENDENCIES
contentful_model!
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2014 YOURNAME

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# ContentfulModel

This is a thin wrapper around the [Contentful.rb](https://github.com/contentful/contentful.rb) api client library.

It allows you to inherit from `ContentfulModel::Base` and specify the content type id, and optionally, fields to coerce in a specific way.

# Usage

## Configure ContentfulModel

Configure ContentfulModel with a block. In a Rails app this is best done in an initializer:

```
ContentfulModel.configure do |config|
config.access_token = "your access token in here"
config.space = "your space id in here"
config.options = {
#extra options to send to the Contentful::Client
}
end
```

## Create a model class

Create a class which inherits from `ContentfulModel::Base`.

```
class Foo < ContentfulModel::Base
self.content_type = "content type id for this model"
end
```

ContentfulModel takes care of setting instance variables for each field in your model. You can optionally coerce fields to the right format - for example dates:

```
class Foo < ContentfulModel::Base
self.content_type = content type id for this model"
coerce_field birthday: :date
coerce_field store_id: :integer
end
```
32 changes: 32 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

require 'rdoc/task'

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ContentfulModel'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end




Bundler::GemHelper.install_tasks

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end


task default: :test
24 changes: 24 additions & 0 deletions contentful_model.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "contentful_model/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "contentful_model"
s.version = ContentfulModel::VERSION
s.authors = ["Error Creative Studio"]
s.email = ["[email protected]"]
s.homepage = "http://www.errorstudio.co.uk"
s.summary = "A thin wrapper for Contentful gem"
s.description = "A wrapper around the Contentful gem to give you a base class to inherit your models from"
s.license = "MIT"

s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]

s.add_dependency "contentful"
s.add_dependency "redcarpet"

#s.add_development_dependency "sqlite3"
end
41 changes: 41 additions & 0 deletions lib/contentful_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'contentful'
require "contentful_model/chainable_queries"
require "contentful_model/base"
require "contentful_model/query"

module ContentfulModel
class << self
#access the configuration class as ContentfulModel.configuration
attr_accessor :configuration

#block for configuration.
def configure
self.configuration ||= Configuration.new
yield(configuration)
end
end

class Configuration
attr_accessor :access_token, :space, :entry_mapping

def initialize
@entry_mapping ||= {}
end

#Rather than listing out all the possible attributes as setters, we have a catchall
#called 'options' which takes a hash and generates instance vars
#@param options [Hash]
def options=(options)
options.each do |k,v|
instance_variable_set(:"@#{k}",v)
end
end


# Return the Configuration object as a hash, with symbols as keys.
# @return [Hash]
def to_hash
Hash[instance_variables.map { |name| [name.to_s.gsub("@","").to_sym, instance_variable_get(name)] } ]
end
end
end
45 changes: 45 additions & 0 deletions lib/contentful_model/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module ContentfulModel
class Base < Contentful::Entry
include ContentfulModel::ChainableQueries

def initialize(*args)
super
self.class.coercions ||= {}
fields.each do |k,v|
self.class.send(:attr_accessor, k)
if self.class.coercions[k].nil?
instance_variable_set(:"@#{k}", v)
else
instance_variable_set(:"@#{k}", self.class::COERCIONS[self.class.coercions[k]].call(v))
end
end
end

class << self
attr_accessor :content_type_id, :coercions

def client
unless ContentfulModel.configuration.entry_mapping.has_key?(@content_type_id)
ContentfulModel.configuration.entry_mapping[@content_type_id] = Object.const_get(self.to_s.to_sym)
end

@@client ||= Contentful::Client.new(ContentfulModel.configuration.to_hash)
end

def content_type
client.content_type(@content_type_id)
end

def coerce_field(coercions_hash)
self.coercions = coercions_hash
end

end






end
end
47 changes: 47 additions & 0 deletions lib/contentful_model/chainable_queries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module ContentfulModel
module ChainableQueries
def self.included(base)
base.extend ClassMethods
base.class_eval do
attr_accessor :query
end
end

module ClassMethods
def all
raise ArgumentError, "You need to set self.content_type in your model class" if @content_type_id.nil?
@query ||= ContentfulModel::Query.new(self)
self
end

def first
@query ||= ContentfulModel::Query.new(self)
@query << {'limit' => 1}
self
end

def offset(n)
@query ||= ContentfulModel::Query.new(self)
@query << {'skip' => n}
self
end

def search(parameters)
@query ||= ContentfulModel::Query.new(self)
if parameters.is_a?(Hash)
parameters.each do |field, search|
@query << {"fields.#{field}[match]" => search}
end
elsif parameters.is_a?(String)
@query << {"query" => parameters}
end
self
end

def load
@query.execute
end
end

end
end
17 changes: 17 additions & 0 deletions lib/contentful_model/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module ContentfulModel
class Query
attr_accessor :parameters
def initialize(reference_class, parameters=nil)
@parameters = parameters || { 'content_type' => reference_class.content_type_id }
@client = reference_class.client
end

def <<(parameters)
@parameters.merge!(parameters)
end

def execute
@client.entries(@parameters)
end
end
end
3 changes: 3 additions & 0 deletions lib/contentful_model/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ContentfulModel
VERSION = "0.0.1"
end
4 changes: 4 additions & 0 deletions lib/tasks/contentful_rails_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :contentful_rails do
# # Task goes here
# end

0 comments on commit 63fb953

Please sign in to comment.