Was this article helpful?
Thanks for your feedback
This guide will help you get started with Contentstack Ruby SDK to build apps powered by Contentstack.
Contentstack offers two regions (US and European) as data centers to store customers' account details and data. Both regions are independent of each other and therefore have a dedicated set of instructions to use SDKs offered by Contentstack.
To use SDKs for the European region, you will have to make certain changes in the configuration of the SDK, as detailed below, and the rest of the instructions remain the same.
To use the Ruby SDK, download it using the gem install command:
$ gem install contentstack
Let's get started with the implementation.
To initialize the SDK, yYou will need to specify the stack’s API key, delivery token, and name of the environment where you will publish the content.
@stack = Contentstack::Client.new("site_api_key", "delivery_token", "enviroment_name")
For Setting the European Region:
To set European region, refer to the code below:
@stack = Contentstack::Client.new("API_key", "delivery_token", "environment_id",{"region": Contentstack::Region::EU})
Once you have initialized the SDK, you can start getting content in your app.
Contentstack SDKs let you interact with the Content Delivery APIs and retrieve content from Contentstack. They are read-only in nature. The SDKs fetch and deliver content from the nearest server via Fastly, our powerful and robust CDN.
To get a single entry, specify the content type as well as the uid of the entry:
entry = @stack.content_type('content_type_uid').entry("entry_uid").fetch() puts entry.get('title') # Use `get` method to retrieve field value by providing a field's unique ID
To retrieve multiple entries of a content type, specify the content type UID. You can also specify search parameters to filter results:
@query = @stack.content_type('blog').query @entries = @query.where('title', 'welcome') .include_schema .include_count .fetch puts "Total Entries -- #{@entries.count}" @entries.each{|entry| puts "#{entry.get('title')}" }
To retrieve localized versions of entries, you can use the query attribute:
entry = @stack.content_type('content_type_uid').query.locale('locale_code').fetch()
Note: Currently, the above query works in case of retrieving localized versions of multiple entries only.
These were examples of some of the basic queries of the SDK. For advanced queries, refer to Contentstack Ruby API reference.
Note: Currently, the Ruby SDK does not support multiple content types referencing in a single query. For more information on how to query entries and assets, refer the Queries section of our Content Delivery API documentation.
In a single instance, the Get Multiple Entries query will retrieve only the first 100 items of the specified content type. You can paginate and retrieve the rest of the items in batches using the skip and limit parameters in subsequent requests.
@stack = Contentstack::Client.new("site_api_key", "delivery_token", "environment") @entries = @stack.content_type('category').query .limit(20) .skip(50) .fetch
Was this article helpful?
Thanks for your feedback