Skip to content

Commit ce0873b

Browse files
committed
entry resources can now be built
entry resources can now be built entries class not includes all methods on collections added by the enumerable module. This means you can now send messages like these to fetched entry objects: `entries.count` `entries.take(2)` `entries.first`
1 parent 91c41b1 commit ce0873b

File tree

6 files changed

+77
-13
lines changed

6 files changed

+77
-13
lines changed

contentstack.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Gem::Specification.new do |spec|
3838
spec.add_development_dependency 'rspec', '~> 3.5'
3939
spec.add_development_dependency 'vcr', '~> 3.0', '>= 3.0.3'
4040
spec.add_development_dependency 'awesome_print'
41+
spec.add_development_dependency 'rb-readline'
42+
spec.add_development_dependency 'byebug'
4143
spec.add_development_dependency 'webmock', '~> 2.3', '>= 2.3.1'
4244
spec.add_development_dependency 'dotenv', '~> 2.1', '>= 2.1.1'
4345
spec.add_development_dependency 'yard'

lib/contentstack/entries.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require_relative 'entry'
2+
3+
module Contentstack
4+
class Entries
5+
include Enumerable
6+
7+
attr_reader :body, :entries
8+
9+
def initialize(body)
10+
@body = body
11+
@entries = body.map { |entry| Entry.new(entry) }
12+
end
13+
14+
def each(&block)
15+
entries.each(&block)
16+
end
17+
end
18+
end

lib/contentstack/entry.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
module Contentstack
4+
class Entry
5+
6+
def initialize(attributes)
7+
@attributes = attributes
8+
end
9+
10+
def properties
11+
@attributes.keys
12+
end
13+
14+
def is_entry?
15+
true
16+
end
17+
end
18+
end

lib/contentstack/query.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "byebug"
2+
3+
module Contentstack
4+
class Query
5+
attr_reader :data
6+
7+
def initialize(data, query)
8+
@data = data
9+
@query = query
10+
end
11+
12+
def all
13+
data
14+
end
15+
16+
end
17+
end

lib/contentstack/request.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
require_relative "response"
21

32
require 'typhoeus'
43

54
module Contentstack
65
class Request
76

8-
attr_reader :endpoint
7+
attr_reader :endpoint, :client
98

10-
def initialize(client, endpoint)
9+
def initialize(client, endpoint, query={})
1110
# puts endpoint
1211
@client = client
1312
@endpoint = endpoint
13+
14+
@query = (normalize_query(query) if query && !query.empty?)
1415
end
1516

17+
# Delegates the actual HTTP work to the client
1618
def fetch
17-
response = Typhoeus::Request.new(
18-
endpoint,
19-
headers: {
20-
api_key: @client.headers[:api_key],
21-
access_token: @client.headers[:access_token],
22-
accept_encoding: "gzip" }
23-
).run
24-
25-
Response.new(response.body)
19+
client.fetch(self)
20+
end
21+
22+
private
23+
24+
def normalize_query(query)
25+
Hash[
26+
query.map do |key, value|
27+
[
28+
key.to_sym,
29+
value.is_a?(::Array) ? value.join(',') : value
30+
]
31+
end
32+
]
2633
end
2734

2835
end

lib/contentstack/response.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "multi_json"
2+
require_relative 'entries'
23

34
module Contentstack
45

@@ -15,7 +16,7 @@ def initialize(body)
1516
end
1617

1718
def entries
18-
@body[:entries]
19+
Entries.new(@body[:entries])
1920
end
2021

2122
def content_types
@@ -37,6 +38,7 @@ def assets
3738
def asset
3839
@body[:asset]
3940
end
41+
4042
end
4143

4244
end

0 commit comments

Comments
 (0)