Git Product home page Git Product logo

beret's Introduction

Beret

A Ruby utility for consuming and manipulating Colonel Kurtz data

Installation

Add this line to your application's Gemfile:

gem 'beret'

And then execute:

$ bundle

Or install it yourself as:

$ gem install beret

Usage

Beret lets you easily work with Colonel Kurtz-generated JSON data in your Ruby application.

Examples

# Grab your CK JSON from wherever it lives.
ck_json = "[{\"content\":{\"text\":\"This is some text.\"},\"type\":\"redactor\",\"blocks\":[{\"type\":\"image\", \"content\":{\"photo_id\":\"42\"}},{\"type\":\"avatar\", \"content\":{\"photo_id\":\"144\"}}]}]"

# Initialization
beret = Beret.new(ck_json)

# Finding values
beret.find(:photo_id) # => [<Beret::SearchResult value="42">, <Beret::SearchResult value="144">]
beret.find(:photo_id, in: [:image]) # => [<Beret::SearchResult value="42">]
beret.find(:photo_id, in: [:avatar]) # => [<Beret::SearchResult value="144">]
beret.find(:photo_id, in: [:some_other_block_type]) # => []

# Using SearchResult objects
result = beret.find(:photo_id).first
result.value # => "42"
result.field_name # => "photo_id"
result.block # => <Beret::Block>
result.block.attributes # => { "photo_id" => "42" }
result.block.type # => "image"
result.block.parent # => <Beret::Block>

beret.find(:photo_id, in: [:image]) do |value, block|
  # You can also replace the contents of an entire block with
  # Beret::Block#update_content
  image = Image.find(value)
  block.update_content(image.to_json)
end

# In-place updating
beret.update(:photo_id, in: [:image]) do |value, block|
  # Transform the value any way you'd like here.
  # In this example, we convert a plain old ID to a SGID
  Image.find(value).to_sgid
end

# Retrieving JSON (useful for saving or returning the net result of an in-place update)
beret.to_json

beret's People

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.