Skip to content

Commit 75456d6

Browse files
committed
An authenticated client can now get data from all 6 endpoints of the Contentstack Delivery API
client = Contentstack::Client.new(access_token: ENV['ACCESS_TOKEN'], api_key: ENV['API_KEY'], environment: ENV['STACK_ENV']) ap client.entries(content_type: 'shirts') ap client.content_types ap client.get_content_type('shirts') ap client.assets ap client.asset(asset_uid: 'blt3e16641c68ba0b25') ap client.entry(content_type: 'shirts', entry_uid: 'blt6deb4788e4d7f16b')
1 parent 4379f41 commit 75456d6

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

contentstack.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
3737
spec.add_development_dependency 'rake', '~> 12.0'
3838
spec.add_development_dependency 'rspec', '~> 3.5'
3939
spec.add_development_dependency 'vcr', '~> 3.0', '>= 3.0.3'
40+
spec.add_development_dependency 'awesome_print'
4041
spec.add_development_dependency 'webmock', '~> 2.3', '>= 2.3.1'
4142
spec.add_development_dependency 'dotenv', '~> 2.1', '>= 2.1.1'
4243
end

lib/contentstack.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ module Contentstack
99
# See README for details
1010
end
1111

12-
# require 'debugger'
12+
require 'debugger'

lib/contentstack/request.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require_relative "response"
2+
3+
require 'typhoeus'
4+
5+
module Contentstack
6+
class Request
7+
8+
attr_reader :endpoint
9+
10+
def initialize(client, endpoint)
11+
@client = client
12+
@endpoint = endpoint
13+
end
14+
15+
def fetch
16+
response = Typhoeus::Request.new(
17+
endpoint,
18+
headers: {
19+
api_key: @client.headers[:api_key],
20+
access_token: @client.headers[:access_token],
21+
accept_encoding: "gzip" }
22+
).run
23+
24+
Response.new(response.body)
25+
end
26+
27+
end
28+
end

lib/contentstack/response.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
1+
require "multi_json"
12

23
module Contentstack
34

45
class Response
56
attr_reader :body
67

78
def initialize(body)
8-
@body = body
9+
@body = begin
10+
MultiJson.load(body, :symbolize_keys => true)
11+
rescue MultiJson::ParseError => exception
12+
exception.data # => "{invalid json}"
13+
exception.cause # => JSON::ParserError: 795: unexpected token at '{invalid json}'
14+
end
915
end
1016

1117
def entries
1218
@body[:entries]
1319
end
20+
21+
def content_types
22+
@body[:content_types]
23+
end
24+
25+
def content_type
26+
@body[:content_type]
27+
end
28+
29+
def entry
30+
@body[:entry]
31+
end
32+
33+
def assets
34+
@body[:assets]
35+
end
36+
37+
def asset
38+
@body[:asset]
39+
end
1440
end
1541

1642
end

0 commit comments

Comments
 (0)