Class: Contentful::Management::Client

Inherits:
Object
  • Object
show all
Extended by:
HTTPClient
Defined in:
lib/contentful/management/client.rb

Constant Summary collapse

DEFAULT_CONFIGURATION =
{
    api_url: 'api.contentful.com',
    api_version: '1',
    secure: true,
    default_locale: 'en-US',
    gzip_encoded: false,
    logger: false,
    log_level: Logger::INFO,
    raise_errors: false
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HTTPClient

delete_http, get_http, post_http, put_http

Constructor Details

#initialize(access_token = nil, configuration = {}) ⇒ Client

Returns a new instance of Client.



32
33
34
35
36
37
38
# File 'lib/contentful/management/client.rb', line 32

def initialize(access_token = nil, configuration = {})
  @configuration = default_configuration.merge(configuration)
  setup_logger
  @access_token = access_token
  @dynamic_entry_cache = {}
  Thread.current[:client] = self
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



18
19
20
# File 'lib/contentful/management/client.rb', line 18

def access_token
  @access_token
end

#configurationObject (readonly)

Returns the value of attribute configuration.



18
19
20
# File 'lib/contentful/management/client.rb', line 18

def configuration
  @configuration
end

#content_type_idObject

Returns the value of attribute content_type_id.



19
20
21
# File 'lib/contentful/management/client.rb', line 19

def content_type_id
  @content_type_id
end

#dynamic_entry_cacheObject

Returns the value of attribute dynamic_entry_cache.



19
20
21
# File 'lib/contentful/management/client.rb', line 19

def dynamic_entry_cache
  @dynamic_entry_cache
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/contentful/management/client.rb', line 18

def logger
  @logger
end

#organization_idObject

Returns the value of attribute organization_id.



19
20
21
# File 'lib/contentful/management/client.rb', line 19

def organization_id
  @organization_id
end

#versionObject

Returns the value of attribute version.



19
20
21
# File 'lib/contentful/management/client.rb', line 19

def version
  @version
end

#zero_lengthObject

Returns the value of attribute zero_length.



19
20
21
# File 'lib/contentful/management/client.rb', line 19

def zero_length
  @zero_length
end

Class Method Details

.shared_instanceObject



184
185
186
# File 'lib/contentful/management/client.rb', line 184

def self.shared_instance
  Thread.current[:client]
end

Instance Method Details

#accept_encoding_header(encoding) ⇒ Object



166
167
168
# File 'lib/contentful/management/client.rb', line 166

def accept_encoding_header(encoding)
  Hash['Accept-Encoding', encoding]
end

#api_versionObject



68
69
70
# File 'lib/contentful/management/client.rb', line 68

def api_version
  configuration[:api_version]
end

#api_version_headerObject



142
143
144
# File 'lib/contentful/management/client.rb', line 142

def api_version_header
  Hash['Content-Type', "application/vnd.contentful.management.v#{ api_version }+json"]
end

#authentication_headerObject



138
139
140
# File 'lib/contentful/management/client.rb', line 138

def authentication_header
  Hash['Authorization', "Bearer #{ access_token }"]
end

#base_urlObject



126
127
128
# File 'lib/contentful/management/client.rb', line 126

def base_url
  "#{ protocol }://#{ configuration[:api_url]}/spaces"
end

#clear_headersObject



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

def clear_headers
  self.content_type_id = nil
  self.version = nil
  self.organization_id = nil
end

#content_type_header(content_type_id) ⇒ Object



158
159
160
# File 'lib/contentful/management/client.rb', line 158

def content_type_header(content_type_id)
  Hash['X-Contentful-Content-Type', content_type_id]
end

#default_configurationObject



76
77
78
# File 'lib/contentful/management/client.rb', line 76

def default_configuration
  DEFAULT_CONFIGURATION.dup
end

#default_localeObject



130
131
132
# File 'lib/contentful/management/client.rb', line 130

def default_locale
  configuration[:default_locale]
end

#delete(request) ⇒ Object



102
103
104
105
106
# File 'lib/contentful/management/client.rb', line 102

def delete(request)
  execute_request(request) do |url|
    self.class.delete_http(url, {}, request_headers)
  end
end

#execute_request(request) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/contentful/management/client.rb', line 84

def execute_request(request)
  request_url = request.url
  url = request.absolute? ? request_url : base_url + request_url
  logger.info(request: {url: url, query: request.query, header: request_headers}) if logger
  raw_response = yield(url)
  logger.debug(response: raw_response) if logger
  clear_headers
  result = Response.new(raw_response, request)
  fail result.object if result.object.is_a?(Error) && configuration[:raise_errors]
  result
end

#get(request) ⇒ Object



108
109
110
111
112
# File 'lib/contentful/management/client.rb', line 108

def get(request)
  execute_request(request) do |url|
    self.class.get_http(url, request.query, request_headers)
  end
end

#gzip_encodedObject



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

def gzip_encoded
  configuration[:gzip_encoded]
end

#organization_header(organization_id) ⇒ Object



150
151
152
# File 'lib/contentful/management/client.rb', line 150

def organization_header(organization_id)
  Hash['X-Contentful-Organization', organization_id]
end

#post(request) ⇒ Object



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

def post(request)
  execute_request(request) do |url|
    self.class.post_http(url, request.query, request_headers)
  end
end

#protocolObject



134
135
136
# File 'lib/contentful/management/client.rb', line 134

def protocol
  configuration[:secure] ? 'https' : 'http'
end

#put(request) ⇒ Object



120
121
122
123
124
# File 'lib/contentful/management/client.rb', line 120

def put(request)
  execute_request(request) do |url|
    self.class.put_http(url, request.query, request_headers)
  end
end

#register_dynamic_entry(key, klass) ⇒ Object



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

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

#request_headersObject

XXX: headers should be supplied differently, maybe through the request object.



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/contentful/management/client.rb', line 171

def request_headers
  headers = {}
  headers.merge! user_agent
  headers.merge! authentication_header
  headers.merge! api_version_header
  headers.merge! organization_header(organization_id) if organization_id
  headers.merge! version_header(version) if version
  headers.merge! zero_length_header if zero_length
  headers.merge! content_type_header(content_type_id) if content_type_id
  headers.merge! accept_encoding_header('gzip') if gzip_encoded
  headers
end

#setup_loggerObject



40
41
42
43
# File 'lib/contentful/management/client.rb', line 40

def setup_logger
  @logger = configuration[:logger]
  logger.level = configuration[:log_level] if logger
end

#update_dynamic_entry_cache!(content_types) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/contentful/management/client.rb', line 57

def update_dynamic_entry_cache!(content_types)
  @dynamic_entry_cache = Hash[
      content_types.map do |ct|
        [
            ct.id.to_sym,
            DynamicEntry.create(ct)
        ]
      end
  ]
end

#update_dynamic_entry_cache_for_space!(space) ⇒ Object

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



53
54
55
# File 'lib/contentful/management/client.rb', line 53

def update_dynamic_entry_cache_for_space!(space)
  update_dynamic_entry_cache!(space.content_types.all)
end

#update_dynamic_entry_cache_for_spaces!(spaces) ⇒ Object



45
46
47
48
49
# File 'lib/contentful/management/client.rb', line 45

def update_dynamic_entry_cache_for_spaces!(spaces)
  spaces.each do |space|
    update_dynamic_entry_cache_for_space!(space)
  end
end

#user_agentObject



146
147
148
# File 'lib/contentful/management/client.rb', line 146

def user_agent
  Hash['User-Agent', "RubyContentfulManagementGem/#{ Contentful::Management::VERSION }"]
end

#version_header(version) ⇒ Object



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

def version_header(version)
  Hash['X-Contentful-Version', version]
end

#zero_length_headerObject



162
163
164
# File 'lib/contentful/management/client.rb', line 162

def zero_length_header
  Hash['Content-Length', 0]
end