contentstack 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.yardopts +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/LICENSE.txt +21 -0
- data/README.rdoc +61 -0
- data/contentstack.gemspec +26 -0
- data/lib/contentstack.rb +20 -0
- data/lib/contentstack/api.rb +54 -0
- data/lib/contentstack/asset.rb +67 -0
- data/lib/contentstack/asset_collection.rb +26 -0
- data/lib/contentstack/client.rb +29 -0
- data/lib/contentstack/content_type.rb +52 -0
- data/lib/contentstack/entry.rb +27 -0
- data/lib/contentstack/entry_collection.rb +43 -0
- data/lib/contentstack/error.rb +7 -0
- data/lib/contentstack/query.rb +589 -0
- data/lib/contentstack/version.rb +3 -0
- data/lib/util.rb +107 -0
- data/rakefile.rb +4 -0
- data/spec/asset_collection_spec.rb +16 -0
- data/spec/asset_spec.rb +48 -0
- data/spec/content_type_spec.rb +64 -0
- data/spec/contentstack_spec.rb +10 -0
- data/spec/entry_collection_spec.rb +42 -0
- data/spec/entry_spec.rb +23 -0
- data/spec/fixtures/asset.json +1 -0
- data/spec/fixtures/asset_collection.json +1 -0
- data/spec/fixtures/category_entry.json +1 -0
- data/spec/fixtures/category_entry_collection.json +1 -0
- data/spec/fixtures/category_entry_collection_without_count.json +1 -0
- data/spec/fixtures/content_types.json +1 -0
- data/spec/fixtures/product_entry.json +1 -0
- data/spec/fixtures/product_entry_collection.json +1 -0
- data/spec/query_spec.rb +188 -0
- data/spec/spec_helper.rb +146 -0
- metadata +163 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ee5dd72d68bfd14e2292a7dd9bd18f0f11fe6091
|
4
|
+
data.tar.gz: 9903cbb89a02c2775a24a238727203c4d9ec9e10
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: df87832f6264b3ac38745dd5cd4364e89c61c50f817f1321c979fae7f9d38d1b32a560784abfaf2ceaf9e558b30ebe033290d778b1eeae4bae408e5b03a609aa
|
7
|
+
data.tar.gz: 9d98b68aae934eef4b344e1d19022d3c2b0ed795013f1023a322182588478d40f73fd6387d3b5887e22921d2620aceb5960b54bf4e9dff1e5caac6c9e32b7b4b
|
data/.gitignore
ADDED
data/.yardopts
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at amit.erandole@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 amite
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
= Contentstack Ruby SDK
|
2
|
+
|
3
|
+
<i>Use Built.io Contentstack to power content for your Ruby projects</i>
|
4
|
+
|
5
|
+
Create Ruby based applications and use the Ruby SDK to fetch and deliver content from Built.io Contentstack. The SDK uses Content Delivery APIs.
|
6
|
+
|
7
|
+
== Getting Started
|
8
|
+
|
9
|
+
This guide will help you get started with our Ruby SDK to build apps powered by Built.io Contentstack.
|
10
|
+
|
11
|
+
=== SDK Installation and Setup
|
12
|
+
|
13
|
+
To use the Ruby SDK, download it using the gem install command
|
14
|
+
|
15
|
+
$ gem install contentstack
|
16
|
+
|
17
|
+
=== Initialize SDK
|
18
|
+
|
19
|
+
You will need to specify the API key, Access token, and Environment Name of your stack to initialize the SDK:
|
20
|
+
|
21
|
+
@stack = Contentstack::Client.new("site_api_key", "access_token", "enviroment_name")
|
22
|
+
|
23
|
+
Once you have initialized the SDK, you can start getting content in your app.
|
24
|
+
|
25
|
+
=== Basic Queries
|
26
|
+
|
27
|
+
==== Get a Single Entry
|
28
|
+
|
29
|
+
To get a single entry, you need to specify the content type as well as the uid of the entry.
|
30
|
+
|
31
|
+
entry = @stack.content_type('content_type_uid').entry("entry_uid").fetch
|
32
|
+
puts entry.get('title') # Use `get` method to retrieve field value by providing a field's unique ID
|
33
|
+
|
34
|
+
==== Get Multiple Entries
|
35
|
+
|
36
|
+
To retrieve multiple entries of a content type, you need to specify the content type uid. You can also specify search parameters to filter results.
|
37
|
+
|
38
|
+
@query = @stack.content_type('blog').query
|
39
|
+
@entries = @query.where('title', 'welcome')
|
40
|
+
.include_schema
|
41
|
+
.include_count
|
42
|
+
.fetch
|
43
|
+
|
44
|
+
puts "Total Entries -- #{@entries.count}"
|
45
|
+
@entries.each{|entry| puts "#{entry.get('title')}" }
|
46
|
+
|
47
|
+
|
48
|
+
== API Reference
|
49
|
+
|
50
|
+
Go through our SDK API Reference guide to know about the methods that can be used to query your content in Built.io Contentstack.
|
51
|
+
|
52
|
+
{Read Ruby SDK API Reference Guide}[http://www.rubydoc.info/gems/contentstack/0.0.1]
|
53
|
+
|
54
|
+
|
55
|
+
== Example Apps
|
56
|
+
|
57
|
+
To help you get started, we have created some sample applications that are powered by Built.io Contentstack Ruby SDK. Click on any of the links below to read the tutorials of the app, view app demo, or download the code from GitHub.
|
58
|
+
|
59
|
+
{Product Catalog}[#]
|
60
|
+
|
61
|
+
Built.io Contentstack provides Ruby SDK using which you can create applications based on Ruby, or any other web frameworks created using Ruby.
|
@@ -0,0 +1,26 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'contentstack/version'
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = %q{contentstack}
|
7
|
+
s.version = Contentstack::VERSION.dup
|
8
|
+
s.date = Time.now
|
9
|
+
s.authors = [%q{Rohit Sharma}]
|
10
|
+
s.email = ["rubygems@contentstack.com"]
|
11
|
+
|
12
|
+
s.license = "MIT"
|
13
|
+
s.homepage = "https://github.com/builtio-contentstack/contentstack-ruby"
|
14
|
+
|
15
|
+
s.summary = %q{Contentstack Ruby client for the Content Delivery API}
|
16
|
+
s.description = %q{Contentstack Ruby client for the Content Delivery API}
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency 'activesupport', "> 3.2.5"
|
22
|
+
|
23
|
+
s.add_development_dependency 'rspec'
|
24
|
+
s.add_development_dependency 'webmock'
|
25
|
+
s.add_development_dependency 'simplecov'
|
26
|
+
end
|
data/lib/contentstack.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
|
3
|
+
require "contentstack/version"
|
4
|
+
require "contentstack/client"
|
5
|
+
require "util"
|
6
|
+
|
7
|
+
|
8
|
+
# == Built.io Contentstack - Ruby SDK
|
9
|
+
# Built.io Contentstack is a content management system that facilitates the process of publication by separating the content from site-related programming and design.
|
10
|
+
# == Installation
|
11
|
+
# gem install contentstack
|
12
|
+
# == Initialize the Stack
|
13
|
+
# @stack = Contentstack::Client.new("site_api_key", "access_token", "enviroment_name")
|
14
|
+
# == Usage
|
15
|
+
# ==== Get single entry
|
16
|
+
# @stack.content_type('blog').entry('<entry_uid_here>').fetch
|
17
|
+
# ==== Query entries
|
18
|
+
# @stack.content_type('blog').query.regex('title', '.*hello.*').fetch
|
19
|
+
module Contentstack
|
20
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/json'
|
5
|
+
require 'open-uri'
|
6
|
+
|
7
|
+
module Contentstack
|
8
|
+
class API
|
9
|
+
def self.init_api(api_key, access_token, environment)
|
10
|
+
@host = "https://cdn.contentstack.io"
|
11
|
+
@api_version = '/v3'
|
12
|
+
@environment = environment
|
13
|
+
@headers = {api_key: api_key, access_token: access_token, user_agent: "ruby-sdk/#{Contentstack::VERSION}", environment: @environment}
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.fetch_content_types(uid="")
|
17
|
+
if !uid.nil? && !uid.empty?
|
18
|
+
path = "/content_types/#{uid}"
|
19
|
+
else
|
20
|
+
path = "/content_types"
|
21
|
+
end
|
22
|
+
send_request(path, {})
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.fetch_entries(content_type, query)
|
26
|
+
path = "/content_types/#{content_type}/entries"
|
27
|
+
send_request(path, query)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.fetch_entry(content_type, entry_uid)
|
31
|
+
path = "/content_types/#{content_type}/entries/#{entry_uid}"
|
32
|
+
send_request(path)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.get_assets(asset_uid=nil)
|
36
|
+
path = "/assets"
|
37
|
+
path += "/#{asset_uid}" if !asset_uid.nil?
|
38
|
+
send_request(path)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def self.send_request(path, q=nil)
|
43
|
+
q ||= {}
|
44
|
+
|
45
|
+
q.merge!(@headers)
|
46
|
+
q.merge!({timestamp: Time.now.to_i})
|
47
|
+
|
48
|
+
query = "?" + q.to_query
|
49
|
+
# puts "Request URL:- #{@host}#{@api_version}#{path}#{query} \n\n"
|
50
|
+
|
51
|
+
ActiveSupport::JSON.decode(open("#{@host}#{@api_version}#{path}#{query}").read)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Contentstack
|
2
|
+
|
3
|
+
# Asset class to fetch file details on Conentstack server.
|
4
|
+
class Asset
|
5
|
+
attr_reader :uid, :content_type, :filename, :file_size, :tags, :url
|
6
|
+
|
7
|
+
# @!attribute [r] uid
|
8
|
+
# Contentstack Asset UID for this asset
|
9
|
+
|
10
|
+
# @!attribute [r] content_type
|
11
|
+
# Content Type for the asset. image/png, image/jpeg, application/pdf, video/mp4 etc.
|
12
|
+
|
13
|
+
# @!attribute [r] filename
|
14
|
+
# Name of the asset.
|
15
|
+
|
16
|
+
# @!attribute [r] file_size
|
17
|
+
# Size of the asset.
|
18
|
+
|
19
|
+
# @!attribute [r] tags
|
20
|
+
# Array of tags assigned to the asset.
|
21
|
+
|
22
|
+
# @!attribute [r] url
|
23
|
+
# URL to fetch/render the asset
|
24
|
+
|
25
|
+
# Create instance of an Asset. Accepts either a uid of asset (String) or a complete asset JSON
|
26
|
+
# @param [String/Hash] attrs
|
27
|
+
# Usage for String parameter
|
28
|
+
# @asset = @stack.asset("some_asset_uid")
|
29
|
+
# @asset.fetch
|
30
|
+
#
|
31
|
+
# Usage for Hash parameter
|
32
|
+
# @asset = @stack.asset({
|
33
|
+
# :uid => "some_asset_uid",
|
34
|
+
# :content_type => "file_type", # image/png, image/jpeg, application/pdf, video/mp4 etc.
|
35
|
+
# :filename => "some_file_name",
|
36
|
+
# :file_size => "some_file_size",
|
37
|
+
# :tags => ["tag1", "tag2", "tag3"],
|
38
|
+
# :url => "file_url"
|
39
|
+
# })
|
40
|
+
# @asset.fetch
|
41
|
+
def initialize(attrs)
|
42
|
+
if attrs.class == String
|
43
|
+
@uid = attrs
|
44
|
+
else
|
45
|
+
attrs = attrs.symbolize_keys
|
46
|
+
@uid = attrs[:uid]
|
47
|
+
@content_type = attrs[:content_type]
|
48
|
+
@filename = attrs[:filename]
|
49
|
+
@file_size = attrs[:file_size]
|
50
|
+
@tags = attrs[:tags]
|
51
|
+
@url = attrs[:url]
|
52
|
+
end
|
53
|
+
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
# Fetch a particular asset using uid.
|
58
|
+
# @asset = @stack.asset('some_asset_uid')
|
59
|
+
# @asset.fetch
|
60
|
+
# puts @asset.url
|
61
|
+
def fetch
|
62
|
+
json = API.get_assets(@uid)
|
63
|
+
# puts "json -- #{json}"
|
64
|
+
self.class.new(json["asset"])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'contentstack/asset'
|
2
|
+
|
3
|
+
module Contentstack
|
4
|
+
# Asset class to fetch details of files on Conentstack server.
|
5
|
+
class AssetCollection
|
6
|
+
attr_reader :assets
|
7
|
+
|
8
|
+
def initialize(assets_array=nil)
|
9
|
+
if assets_array.nil?
|
10
|
+
@assets = []
|
11
|
+
return self
|
12
|
+
else
|
13
|
+
@assets = assets_array.collect{|a| Asset.new(a)}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Fetch assets uploaded to Contentstack
|
18
|
+
#
|
19
|
+
# Example:
|
20
|
+
# @assets = @stack.assets.fetch
|
21
|
+
def fetch
|
22
|
+
json = API.get_assets
|
23
|
+
self.class.new(json["assets"])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'contentstack/api'
|
2
|
+
require 'contentstack/content_type'
|
3
|
+
require 'contentstack/asset_collection'
|
4
|
+
|
5
|
+
module Contentstack
|
6
|
+
class Client
|
7
|
+
# Initialize "Built.io Contentstack" Client instance
|
8
|
+
def initialize(api_key, access_token, environment)
|
9
|
+
API.init_api(api_key, access_token, environment)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def content_types
|
14
|
+
ContentType.all
|
15
|
+
end
|
16
|
+
|
17
|
+
def content_type(uid)
|
18
|
+
ContentType.new({uid: uid})
|
19
|
+
end
|
20
|
+
|
21
|
+
def assets
|
22
|
+
AssetCollection.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def asset(uid)
|
26
|
+
Asset.new(uid)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'contentstack/query'
|
2
|
+
|
3
|
+
module Contentstack
|
4
|
+
class ContentType
|
5
|
+
[:title, :uid, :created_at, :updated_at, :attributes].each do |method_name|
|
6
|
+
if [:created_at, :updated_at].include?(method_name)
|
7
|
+
define_method method_name do
|
8
|
+
return Time.parse(@attributes[method_name]) if @attributes[method_name] && !@attributes[method_name].nil?
|
9
|
+
end
|
10
|
+
elsif :attributes == method_name
|
11
|
+
define_method :attributes do
|
12
|
+
{
|
13
|
+
title: self.title,
|
14
|
+
uid: self.uid,
|
15
|
+
created_at: self.created_at,
|
16
|
+
updated_at: self.updated_at,
|
17
|
+
schema: @attributes[:schema]
|
18
|
+
}
|
19
|
+
end
|
20
|
+
else
|
21
|
+
define_method method_name do
|
22
|
+
return @attributes[method_name]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(object)
|
28
|
+
@attributes = object.symbolize_keys
|
29
|
+
end
|
30
|
+
|
31
|
+
def query
|
32
|
+
Query.new(self.uid)
|
33
|
+
end
|
34
|
+
|
35
|
+
def entry(entry_uid)
|
36
|
+
Entry.new({uid: entry_uid}, self.uid)
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
def self.all
|
41
|
+
content_types = API.fetch_content_types["content_types"]
|
42
|
+
content_types.map do |content_type|
|
43
|
+
ContentType.new(content_type.inject({}){|clone,(k,v)| clone[k.to_sym] = v; clone})
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.find_by_uid(uid)
|
48
|
+
content_type = API.fetch_content_types(uid)["content_type"]
|
49
|
+
ContentType.new(content_type.inject({}){|clone,(k,v)| clone[k.to_sym] = v; clone})
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|