class ContentfulLite::Client

Attributes

environment[R]
preview[R]
space_id[R]

Public Class Methods

new(space_id:, access_token:, environment: nil, preview: false) click to toggle source

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

asset(id, query = {}) click to toggle source

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
assets(query = {}) click to toggle source

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_resource(raw) click to toggle source

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
entries(query = {}) click to toggle source

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
entry(id, query = {}) click to toggle source

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

create_url(endpoint) click to toggle source
# 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
error_class(status) click to toggle source
# File lib/contentful_lite/client.rb, line 106
def error_class(status)
  status == 404 ? NotFoundError : RequestError
end
parse_entry(hash) click to toggle source
# 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
request(endpoint, parameters) click to toggle source
# 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
request_headers() click to toggle source
# 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