class ContentfulLite::Client
Attributes
Public Class Methods
Creates the Contentful Client
@param space_id
[String] the Contentful Space Id you want to connect to. @param access_token [String] The secret access token to access the api @param environment [String, nil] To allow querying to a non-master environment @param preview [Boolean] True if you want to get draft entries
# File lib/contentful_lite/client.rb, line 23 def initialize(space_id:, access_token:, environment: nil, preview: false) @space_id = space_id @environment = environment @preview = preview @access_token = access_token end
Public Instance Methods
Gets a single asset from Contentful API @param id [String] Unique id of the Contentful asset @param query [Hash] any query params accepted by Contentful API @return [ContentfulLite::Asset]
# File lib/contentful_lite/client.rb, line 49 def asset(id, query = {}) ContentfulLite::Asset.new(request("assets/#{id}", query)) end
Gets an array of assets from Contentful API @param query [Hash] any query params accepted by Contentful API @return [ContentfulLite::AssetsArray]
# File lib/contentful_lite/client.rb, line 56 def assets(query = {}) ContentfulLite::AssetsArray.new(request(:assets, query)) end
Build an entry resource from a raw Contentful API response @param raw [Hash] a JSON parsed response from Contentful API @return [ContentfulLite::Entry,ContentfulLite::Asset,ContentfulLite::DeletedEntry]
# File lib/contentful_lite/client.rb, line 63 def build_resource(raw) case raw['sys']['type'] when 'Entry' parse_entry(raw) when 'Asset' ContentfulLite::Asset.new(raw) when 'DeletedEntry' ContentfulLite::DeletedEntry.new(raw) end end
Gets an array of entries from Contentful API @param query [Hash] any query params accepted by Contentful API @return [ContentfulLite::EntriesArray]
# File lib/contentful_lite/client.rb, line 33 def entries(query = {}) ContentfulLite::EntriesArray.new(request(:entries, query)) end
Gets a single entry from Contentful API @param id [String] Unique id of the Contentful entry @param query [Hash] any query params accepted by Contentful API @return [ContentfulLite::Entry]
# File lib/contentful_lite/client.rb, line 41 def entry(id, query = {}) parse_entry request("entries/#{id}", query) end
Private Instance Methods
# File lib/contentful_lite/client.rb, line 81 def create_url(endpoint) "https://#{preview ? 'preview' : 'cdn'}.contentful.com/spaces/#{space_id}/" + ( environment.nil? ? '' : "environments/#{environment}/" ) + endpoint.to_s end
# File lib/contentful_lite/client.rb, line 106 def error_class(status) status == 404 ? NotFoundError : RequestError end
# File lib/contentful_lite/client.rb, line 76 def parse_entry(hash) klass = ContentfulLite::Entry.get_class(hash['sys']['contentType']['sys']['id']) klass.new(hash) end
# File lib/contentful_lite/client.rb, line 87 def request(endpoint, parameters) parameters.transform_keys!(&:to_s) parameters.transform_values! { |value| value.is_a?(::Array) ? value.join(',') : value } response = HTTP[request_headers].get(create_url(endpoint), params: parameters) body = response.to_s body = Zlib::GzipReader.new(StringIO.new(body)).read if response.headers['Content-Encoding'].eql?('gzip') JSON.parse(body).tap do |parsed| raise error_class(response.status).new(response, parsed) if response.status != 200 end end
# File lib/contentful_lite/client.rb, line 98 def request_headers { 'Authorization' => "Bearer #{@access_token}", 'Content-Type' => 'application/vnd.contentful.delivery.v1+json', 'Accept-Encoding' => 'gzip' } end