Class: ContentfulLite::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful_lite/client.rb

Defined Under Namespace

Classes: NotFoundError, RequestError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(space_id:, access_token:, environment: nil, preview: false) ⇒ Client

Creates the Contentful Client

Parameters:

  • space_id (String)

    the Contentful Space Id you want to connect to.

  • access_token (String)

    The secret access token to access the api

  • environment (String, nil) (defaults to: nil)

    To allow querying to a non-master environment

  • preview (Boolean) (defaults to: false)

    True if you want to get draft entries



23
24
25
26
27
28
# 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

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



16
17
18
# File 'lib/contentful_lite/client.rb', line 16

def environment
  @environment
end

#previewObject (readonly)

Returns the value of attribute preview.



16
17
18
# File 'lib/contentful_lite/client.rb', line 16

def preview
  @preview
end

#space_idObject (readonly)

Returns the value of attribute space_id.



16
17
18
# File 'lib/contentful_lite/client.rb', line 16

def space_id
  @space_id
end

Instance Method Details

#asset(id, query = {}) ⇒ ContentfulLite::Asset

Gets a single asset from Contentful API

Parameters:

  • id (String)

    Unique id of the Contentful asset

  • query (Hash) (defaults to: {})

    any query params accepted by Contentful API

Returns:



49
50
51
# File 'lib/contentful_lite/client.rb', line 49

def asset(id, query = {})
  ContentfulLite::Asset.new(request("assets/#{id}", query))
end

#assets(query = {}) ⇒ ContentfulLite::AssetsArray

Gets an array of assets from Contentful API

Parameters:

  • query (Hash) (defaults to: {})

    any query params accepted by Contentful API

Returns:



56
57
58
# File 'lib/contentful_lite/client.rb', line 56

def assets(query = {})
  ContentfulLite::AssetsArray.new(request(:assets, query))
end

#build_resource(raw) ⇒ ContentfulLite::Entry, ...

Build an entry resource from a raw Contentful API response

Parameters:

  • raw (Hash)

    a JSON parsed response from Contentful API

Returns:



63
64
65
66
67
68
69
70
71
72
# 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 = {}) ⇒ ContentfulLite::EntriesArray

Gets an array of entries from Contentful API

Parameters:

  • query (Hash) (defaults to: {})

    any query params accepted by Contentful API

Returns:



33
34
35
# File 'lib/contentful_lite/client.rb', line 33

def entries(query = {})
  ContentfulLite::EntriesArray.new(request(:entries, query))
end

#entry(id, query = {}) ⇒ ContentfulLite::Entry

Gets a single entry from Contentful API

Parameters:

  • id (String)

    Unique id of the Contentful entry

  • query (Hash) (defaults to: {})

    any query params accepted by Contentful API

Returns:



41
42
43
# File 'lib/contentful_lite/client.rb', line 41

def entry(id, query = {})
  parse_entry request("entries/#{id}", query)
end