Skip to content

microcmsio/microcms-ruby-sdk

Repository files navigation

microCMS Ruby SDK

microCMS Ruby SDK.

Tutorial

See official tutorial.

Installation

Add this line to your application's Gemfile:

gem 'microcms-ruby-sdk'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install microcms-ruby-sdk

Usage

Import

require 'microcms'

Create client object

MicroCMS.service_domain = 'YOUR_DOMAIN'
MicroCMS.api_key = 'YOUR_API_KEY'

Note that the YOUR_DOMAIN is the subdomain name of your service (not the FQDN).

Get content list

puts MicroCMS.list('endpoint')

Get content list with parameters

puts MicroCMS.list(
    'endpoint',
    {
        draft_key: "abcd",
        limit: 100,
        offset: 1,
        orders: ['updatedAt'],
        q: 'Hello',
        fields: %w[id title],
        ids: ['foo'],
        filters: 'publishedAt[greater_than]2021-01-01',
        depth: 1,
    },
)

Get single content

puts MicroCMS.get('endpoint', 'ruby')

Get single content with parameters

puts MicroCMS.get(
    'endpoint',
    'ruby',
    {
        draft_key: 'abcdef1234',
        fields: %w[title publishedAt],
        depth: 1,
    },
)

Get object form content

puts MicroCMS.get('endpoint')

Create content

puts MicroCMS.create('endpoint', { text: 'Hello, microcms-ruby-sdk!' })

Create content with specified ID

puts MicroCMS.create(
    'endpoint',
    {
        id: 'my-content-id',
        text: 'Hello, microcms-ruby-sdk!',
    },
)

Create draft content

puts MicroCMS.create(
    'endpoint',
    {
        id: 'my-content-id',
        text: 'Hello, microcms-ruby-sdk!',
    },
    { status: 'draft' },
)

Update content

puts MicroCMS.update(
    'endpoint',
    {
        id: 'microcms-ruby-sdk',
        text: 'Hello, microcms-ruby-sdk update method!',
    },
)

Update object form content

puts MicroCMS.update('endpoint', { text: 'Hello, microcms-ruby-sdk update method!' })

Delete content

MicroCMS.delete('endpoint', 'microcms-ruby-sdk')

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/microcmsio/microcms-ruby-sdk.