Code Monkey home page Code Monkey logo

buttercms-ruby's Introduction

ButterCMS API Ruby Client

Documentation

For a comprehensive list of examples, check out the API documentation.

Setup

To setup your project, follow these steps:

  1. Install using gem install buttercms-ruby or by adding to your Gemfile:
gem 'buttercms-ruby'
  1. Set your API token.
require 'buttercms-ruby'

ButterCMS::api_token = "YourToken"

# Fetch content from test mode (eg. for your staging website)
# ButterCMS::test_mode = true

# Set read timeout (Default is 5.0)
# ButterCMS::read_timeout = 5.0

Pages

https://buttercms.com/docs/api/?ruby#pages

params = {page: 1, page_size: 10, locale: 'en', preview: 1, fields.headline: 'foo bar', levels: 2} # optional
pages = ButterCMS::Page.list('news', params)
page = ButterCMS::Page.get('news', 'hello-world', params)

Collections

https://buttercms.com/docs/api/?ruby#retrieve-a-collection

params = {page: 1, page_size: 10, locale: 'en', preview: 1, fields.headline: 'foo bar', levels: 2} # optional
ButterCMS::Content.fetch(['testimonials'], params)

# Test mode can be used to setup a staging website for previewing Collections or for testing content during local development. To fetch content from test mode add the following configuration:
ButterCMS::test_mode = true

Blog Engine

https://buttercms.com/docs/api/?ruby#blog-engine

posts = ButterCMS::Post.all({:page => 1, :page_size => 10})
puts posts.first.title
puts posts.meta.next_page

posts = ButterCMS::Post.search("my favorite post", {page: 1, page_size: 10})
puts posts.first.title

post = ButterCMS::Post.find("post-slug")
puts post.title

# Create a Post.
ButterCMS::write_api_token = "YourWriteToken"
ButterCMS::Post.create({
  slug: 'blog-slug',
  title: 'blog-title'
})

# Update a Post
ButterCMS::Post.update('blog-slug', {
  title: 'blog-title-v2'
})

# Create a page
ButterCMS::Page.create({
  slug: 'page-slug',
  title: 'page-title',
  status: 'published',
  "page-type": 'page_type',
  fields: {
    meta_title: 'test meta title'
  }
})

# update a Page
ButterCMS::Page.update('page-slug-2', {
  status: 'published',
  fields: {
    meta_title: 'test meta title'
  }
})



author = ButterCMS::Author.find("author-slug")
puts author.first_name

category = ButterCMS::Category.find("category-slug")
puts category.name

tags = ButterCMS::Tag.all
p tags

rss_feed = ButterCMS::Feed.find(:rss)
puts rss_feed.data

Fallback Data Store

This client supports automatic fallback to a data store when API requests fail. When a data store is set, on every successful API request the response is written to the data store. When a subsequent API request fails, the client attempts to fallback to the value in the data store. Currently, Redis and YAML Store are supported.

# Use YAMLstore
ButterCMS::data_store = :yaml, "/File/Path/For/buttercms.store"

# Use Redis
ButterCMS::data_store = :redis, ENV['REDIS_URL']

# Set logger (optional)
ButterCMS::logger = MyLogger.new

Other

View Ruby Blog engine and Full CMS for other examples of using ButterCMS with Ruby.

buttercms-ruby's People

Contributors

burlesona avatar chasecoleman avatar gaffneyc avatar jakelumetta avatar kevinjcoleman avatar rogerjin12 avatar tskulkarni25 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.