Class: Contentful::Client

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

Overview

The client object is initialized with a space and a key and then used for querying resources from this space. See README for details

Constant Summary collapse

DEFAULT_CONFIGURATION =
{
  secure: true,
  raise_errors: true,
  dynamic_entries: :manual,
  api_url: 'cdn.contentful.com',
  api_version: 1,
  authentication_mechanism: :header,
  resource_builder: ResourceBuilder,
  resource_mapping: {},
  raw_mode: false,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_configuration = {}) ⇒ Client

Returns a new instance of Client.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/contentful/client.rb', line 31

def initialize(given_configuration = {})
  @configuration = default_configuration.merge(given_configuration)
  normalize_configuration!
  validate_configuration!

  if configuration[:dynamic_entries] == :auto
    update_dynamic_entry_cache!
  else
    @dynamic_entry_cache = {}
  end
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



23
24
25
# File 'lib/contentful/client.rb', line 23

def configuration
  @configuration
end

#dynamic_entry_cacheObject (readonly)

Returns the value of attribute dynamic_entry_cache.



23
24
25
# File 'lib/contentful/client.rb', line 23

def dynamic_entry_cache
  @dynamic_entry_cache
end

Class Method Details

.get_http(url, query, headers = {}) ⇒ Object

Wraps the actual HTTP request



27
28
29
# File 'lib/contentful/client.rb', line 27

def self.get_http(url, query, headers = {})
  HTTP[headers].get(url, params: query)
end

Instance Method Details

#asset(id, query = {}) ⇒ Object

Gets a specific asset Takes an id and an optional hash of query options Returns a Contentful::Asset



86
87
88
# File 'lib/contentful/client.rb', line 86

def asset(id, query = {})
  Request.new(self, '/assets', query, id).get
end

#assets(query = {}) ⇒ Object

Gets a collection of assets Takes an optional hash of query options Returns a Contentful::Array of Contentful::Asset



93
94
95
# File 'lib/contentful/client.rb', line 93

def assets(query = {})
  Request.new(self, '/assets', query).get
end

#base_urlObject

Returns the base url for all of the client’s requests



98
99
100
# File 'lib/contentful/client.rb', line 98

def base_url
  "http#{configuration[:secure] ? 's' : ''}://#{configuration[:api_url]}/spaces/#{configuration[:space]}"
end

#content_type(id, query = {}) ⇒ Object

Gets a specific content type Takes an id and an optional hash of query options Returns a Contentful::ContentType



58
59
60
# File 'lib/contentful/client.rb', line 58

def content_type(id, query = {})
  Request.new(self, '/content_types', query, id).get
end

#content_types(query = {}) ⇒ Object

Gets a collection of content types Takes an optional hash of query options Returns a Contentful::Array of Contentful::ContentType



65
66
67
# File 'lib/contentful/client.rb', line 65

def content_types(query = {})
  Request.new(self, '/content_types', query).get
end

#default_configurationObject

Returns the default configuration



44
45
46
# File 'lib/contentful/client.rb', line 44

def default_configuration
  DEFAULT_CONFIGURATION
end

#entries(query = {}) ⇒ Object

Gets a collection of entries Takes an optional hash of query options Returns a Contentful::Array of Contentful::Entry



79
80
81
# File 'lib/contentful/client.rb', line 79

def entries(query = {})
  Request.new(self, '/entries', query).get
end

#entry(id, query = {}) ⇒ Object

Gets a specific entry Takes an id and an optional hash of query options Returns a Contentful::Entry



72
73
74
# File 'lib/contentful/client.rb', line 72

def entry(id, query = {})
  Request.new(self, '/entries', query, id).get
end

#get(request, build_resource = true) ⇒ Object

Get a Contentful::Request object Set second parameter to false to deactivate Resource building and return Response objects instead



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/contentful/client.rb', line 123

def get(request, build_resource = true)
  response = Response.new(
    self.class.get_http(
      base_url + request.url,
      request_query(request.query),
      request_headers,
    ), request
  )

  return response if !build_resource || configuration[:raw_mode]

  result = configuration[:resource_builder].new(self, response, configuration[:resource_mapping]).run
  raise result if result.is_a?(Error) && configuration[:raise_errors]
  result
end

#register_dynamic_entry(key, klass) ⇒ Object

Use this method to manually register a dynamic entry See examples/dynamic_entries.rb



154
155
156
# File 'lib/contentful/client.rb', line 154

def register_dynamic_entry(key, klass)
  @dynamic_entry_cache[key.to_sym] = klass
end

#request_headersObject

Returns the headers used for the HTTP requests



103
104
105
106
107
108
109
# File 'lib/contentful/client.rb', line 103

def request_headers
  headers = { "User-Agent" => "RubyContentfulGem/#{Contentful::VERSION}" }
  headers["Authorization"] = "Bearer #{configuration[:access_token]}" if configuration[:authentication_mechanism] == :header
  headers["Content-Type"]  = "application/vnd.contentful.delivery.v#{configuration[:api_version].to_i}+json" if configuration[:api_version]

  headers
end

#request_query(query) ⇒ Object

Patches a query hash with the client configurations for queries



112
113
114
115
116
117
118
# File 'lib/contentful/client.rb', line 112

def request_query(query)
  if configuration[:authentication_mechanism] == :query_string
    query["access_token"] = configuration[:access_token]
  end

  query
end

#space(query = {}) ⇒ Object

Gets the client’s space Takes an optional hash of query options Returns a Contentful::Space



51
52
53
# File 'lib/contentful/client.rb', line 51

def space(query = {})
  Request.new(self, '', query).get
end

#update_dynamic_entry_cache!Object

Use this method together with the client’s :dynamic_entries configuration. See README for details.



141
142
143
144
145
146
147
148
149
150
# File 'lib/contentful/client.rb', line 141

def update_dynamic_entry_cache!
  @dynamic_entry_cache = Hash[
    content_types(limit: 0).map{ |ct|
      [
        ct.id.to_sym,
        DynamicEntry.create(ct),
      ]
    }
  ]
end