Class: Contentful::Client
- Inherits:
-
Object
- Object
- Contentful::Client
- 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: {}, entry_mapping: {}, default_locale: 'en-US', raw_mode: false, gzip_encoded: false, logger: false, log_level: Logger::INFO, proxy_host: nil, proxy_username: nil, proxy_password: nil, proxy_port: nil }
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#dynamic_entry_cache ⇒ Object
readonly
Returns the value of attribute dynamic_entry_cache.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
Class Method Summary collapse
-
.get_http(url, query, headers = {}, proxy = {}) ⇒ Object
Wraps the actual HTTP request via proxy.
Instance Method Summary collapse
-
#asset(id, query = {}) ⇒ Object
Gets a specific asset Takes an id and an optional hash of query options Returns a Contentful::Asset.
-
#assets(query = {}) ⇒ Object
Gets a collection of assets Takes an optional hash of query options Returns a Contentful::Array of Contentful::Asset.
-
#base_url ⇒ Object
Returns the base url for all of the client’s requests.
-
#content_type(id, query = {}) ⇒ Object
Gets a specific content type Takes an id and an optional hash of query options Returns a Contentful::ContentType.
-
#content_types(query = {}) ⇒ Object
Gets a collection of content types Takes an optional hash of query options Returns a Contentful::Array of Contentful::ContentType.
-
#default_configuration ⇒ Object
Returns the default configuration.
-
#entries(query = {}) ⇒ Object
Gets a collection of entries Takes an optional hash of query options Returns a Contentful::Array of Contentful::Entry.
-
#entry(id, query = {}) ⇒ Object
Gets a specific entry Takes an id and an optional hash of query options Returns a Contentful::Entry.
-
#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.
-
#initialize(given_configuration = {}) ⇒ Client
constructor
A new instance of Client.
- #proxy_params ⇒ Object
-
#register_dynamic_entry(key, klass) ⇒ Object
Use this method to manually register a dynamic entry See examples/dynamic_entries.rb.
-
#request_headers ⇒ Object
Returns the headers used for the HTTP requests.
-
#request_query(query) ⇒ Object
Patches a query hash with the client configurations for queries.
- #setup_logger ⇒ Object
-
#space(query = {}) ⇒ Object
Gets the client’s space Takes an optional hash of query options Returns a Contentful::Space.
-
#sync(options = { initial: true }) ⇒ Object
Create a new synchronisation object Takes sync options or a sync_url You will need to call #each_page or #first_page on it.
-
#update_dynamic_entry_cache! ⇒ Object
Use this method together with the client’s :dynamic_entries configuration.
Constructor Details
#initialize(given_configuration = {}) ⇒ Client
Returns a new instance of Client.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/contentful/client.rb', line 46 def initialize(given_configuration = {}) @configuration = default_configuration.merge(given_configuration) normalize_configuration! validate_configuration! setup_logger if configuration[:dynamic_entries] == :auto update_dynamic_entry_cache! else @dynamic_entry_cache = {} end end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
35 36 37 |
# File 'lib/contentful/client.rb', line 35 def configuration @configuration end |
#dynamic_entry_cache ⇒ Object (readonly)
Returns the value of attribute dynamic_entry_cache.
35 36 37 |
# File 'lib/contentful/client.rb', line 35 def dynamic_entry_cache @dynamic_entry_cache end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
35 36 37 |
# File 'lib/contentful/client.rb', line 35 def logger @logger end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
35 36 37 |
# File 'lib/contentful/client.rb', line 35 def proxy @proxy end |
Class Method Details
.get_http(url, query, headers = {}, proxy = {}) ⇒ Object
Wraps the actual HTTP request via proxy
38 39 40 41 42 43 44 |
# File 'lib/contentful/client.rb', line 38 def self.get_http(url, query, headers = {}, proxy = {}) if proxy[:host] HTTP[headers].via(proxy[:host], proxy[:port], proxy[:username], proxy[:password]).get(url, params: query) else HTTP[headers].get(url, params: query) end 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
116 117 118 |
# File 'lib/contentful/client.rb', line 116 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
123 124 125 |
# File 'lib/contentful/client.rb', line 123 def assets(query = {}) Request.new(self, '/assets', query).get end |
#base_url ⇒ Object
Returns the base url for all of the client’s requests
128 129 130 |
# File 'lib/contentful/client.rb', line 128 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
88 89 90 |
# File 'lib/contentful/client.rb', line 88 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
95 96 97 |
# File 'lib/contentful/client.rb', line 95 def content_types(query = {}) Request.new(self, '/content_types', query).get end |
#default_configuration ⇒ Object
Returns the default configuration
74 75 76 |
# File 'lib/contentful/client.rb', line 74 def default_configuration DEFAULT_CONFIGURATION.dup end |
#entries(query = {}) ⇒ Object
Gets a collection of entries Takes an optional hash of query options Returns a Contentful::Array of Contentful::Entry
109 110 111 |
# File 'lib/contentful/client.rb', line 109 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
102 103 104 |
# File 'lib/contentful/client.rb', line 102 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
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/contentful/client.rb', line 152 def get(request, build_resource = true) url = request.absolute? ? request.url : base_url + request.url logger.info(request: { url: url, query: request.query, header: request_headers }) if logger response = Response.new( self.class.get_http( url, request_query(request.query), request_headers, proxy_params ), request ) return response if !build_resource || configuration[:raw_mode] logger.debug(response: response) if logger result = configuration[:resource_builder].new( self, response, configuration[:resource_mapping], configuration[:entry_mapping], configuration[:default_locale] ).run fail result if result.is_a?(Error) && configuration[:raise_errors] result end |
#proxy_params ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/contentful/client.rb', line 64 def proxy_params { host: configuration[:proxy_host], port: configuration[:proxy_port], username: configuration[:proxy_username], password: configuration[:proxy_password] } end |
#register_dynamic_entry(key, klass) ⇒ Object
Use this method to manually register a dynamic entry See examples/dynamic_entries.rb
193 194 195 |
# File 'lib/contentful/client.rb', line 193 def register_dynamic_entry(key, klass) @dynamic_entry_cache[key.to_sym] = klass end |
#request_headers ⇒ Object
Returns the headers used for the HTTP requests
133 134 135 136 137 138 139 |
# File 'lib/contentful/client.rb', line 133 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['Accept-Encoding'] = 'gzip' if configuration[:gzip_encoded] headers end |
#request_query(query) ⇒ Object
Patches a query hash with the client configurations for queries
142 143 144 145 146 147 |
# File 'lib/contentful/client.rb', line 142 def request_query(query) if configuration[:authentication_mechanism] == :query_string query['access_token'] = configuration[:access_token] end query end |
#setup_logger ⇒ Object
59 60 61 62 |
# File 'lib/contentful/client.rb', line 59 def setup_logger @logger = configuration[:logger] logger.level = configuration[:log_level] if logger end |
#space(query = {}) ⇒ Object
Gets the client’s space Takes an optional hash of query options Returns a Contentful::Space
81 82 83 |
# File 'lib/contentful/client.rb', line 81 def space(query = {}) Request.new(self, '', query).get end |
#sync(options = { initial: true }) ⇒ Object
Create a new synchronisation object Takes sync options or a sync_url You will need to call #each_page or #first_page on it
200 201 202 |
# File 'lib/contentful/client.rb', line 200 def sync( = { initial: true }) Sync.new(self, ) end |
#update_dynamic_entry_cache! ⇒ Object
Use this method together with the client’s :dynamic_entries configuration. See README for details.
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/contentful/client.rb', line 180 def update_dynamic_entry_cache! @dynamic_entry_cache = Hash[ content_types(limit: 1000).map do |ct| [ ct.id.to_sym, DynamicEntry.create(ct) ] end ] end |