Skip to content

Commit 8d81e51

Browse files
committed
Endpoint now works with both query and params
Endpoint now works with: - both query and params - no query or params - only params but no query - only query but no params client = Contentstack::Client.new(access_token: ENV['ACCESS_TOKEN'], api_key: ENV['API_KEY'], environment: ENV['STACK_ENV']) entries = client.entries(content_type: 'shirts', params: { limit: 5 } ) entries = client.entries(content_type: 'shirts', params: { include: 'brands' }) entries = client.entries(content_type: 'shirts', params: { include: 'brands, shirt_sizes' }) entries = client.entries(content_type: 'shirts', params: { include: 'brands, shirt_sizes', limit: 5 } ) entries = client.entries(content_type: 'shirts', params: { include: 'brands', limit: 5 } ) entries = client.entries(content_type: 'shirts') entries = client.entries(content_type: 'shirts', params: { include: 'brands', limit: 5, skip: 2 } ) entries = client.entries(content_type: 'shirts', params: { include: 'brands', asc: 'price' } ) entries = client.entries(content_type: 'shirts', params: { include: 'brands', desc: 'price' } entries = client.entries(content_type: 'shirts', query: { title: "Mayra Women's Denim Shirt", "price": 449 } entries = client.entries(content_type: 'shirts', params: { include: 'brands, shirt_sizes', limit: 5 }, query: { title: "Mayra Women's Denim Shirt", "price": 449 } ) entries.each do |entry| puts "#{entry.title} #{entry.price}" end
1 parent 11333f9 commit 8d81e51

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

lib/contentstack/request.rb

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
require "byebug"
2+
require "json"
23
require 'typhoeus'
4+
require "uri"
35
require "active_support/core_ext/hash"
46

57
module Contentstack
68
class Request
79

8-
attr_reader :client, :query, :endpoint_with_params
9-
attr_accessor :endpoint, :params
10+
attr_reader :client
11+
attr_accessor :endpoint, :params, :query
1012

11-
def initialize(client, endpoint, params={})
12-
# puts endpoint
13+
def initialize(client, endpoint, params={}, query={})
1314
@client = client
14-
@endpoint = endpoint
15-
@endpoint = build_query(params, endpoint + "?") unless params.empty?
15+
@params = params
16+
@query = query
17+
@endpoint = build_endpoint(endpoint)
1618
end
1719

1820
# Delegates the actual HTTP work to the client
1921
def fetch
2022
client.fetch(self)
2123
end
2224

25+
def build_endpoint(initial_endpoint)
26+
return initial_endpoint if no_params_or_query
27+
return initial_endpoint += '?' + build_query(query) if query_but_no_params
28+
return initial_endpoint += '?' + build_params(params) if params_but_no_query
29+
# go ahead and build endpoint with both params and query
30+
initial_endpoint += '?' + build_params(params) + '&' + build_query(query)
31+
end
32+
2333
private
2434

25-
def build_query(params, endpoint)
35+
def no_params_or_query
36+
params.empty? && query.empty?
37+
end
38+
39+
def query_but_no_params
40+
!! query && params.empty?
41+
end
42+
43+
def params_but_no_query
44+
!! params && query.empty?
45+
end
46+
47+
def build_query(query)
48+
encoded = URI.encode(query.to_json)
49+
"query=#{encoded}"
50+
end
51+
52+
def build_params(params)
2653
limit = Hash[limit: params[:limit] || 0]
2754
skip = Hash[skip: params[:skip] || 0]
2855
asc = Hash[asc: params[:asc] || ""]
@@ -33,24 +60,12 @@ def build_query(params, endpoint)
3360

3461
references = build_references(params[:include]) || ""
3562

36-
query_string = references.empty? ? "#{endpoint}#{encoded}" : "#{endpoint}#{references}&#{encoded}"
63+
query_string = references.empty? ? "#{encoded}" : "#{references}&#{encoded}"
3764
end
3865

3966
def build_references(references)
4067
[references].join(",").split(",").map{ |ref| "include[]=#{ref}" }.join("&").gsub(/\s+/, "")
4168
end
4269

43-
44-
def normalize_query(query)
45-
Hash[
46-
query.map do |key, value|
47-
[
48-
key.to_sym,
49-
value.is_a?(::Array) ? value.join(',') : value
50-
]
51-
end
52-
]
53-
end
54-
5570
end
5671
end

0 commit comments

Comments
 (0)