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
}

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.



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

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



181
182
183
# File 'lib/contentful/management/client.rb', line 181

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

Instance Method Details

#accept_encoding_header(encoding) ⇒ Object



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

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

#api_versionObject



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

def api_version
  configuration[:api_version]
end

#api_version_headerObject



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

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

#authentication_headerObject



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

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

#base_urlObject



123
124
125
# File 'lib/contentful/management/client.rb', line 123

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

#clear_headersObject



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

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

#content_type_header(content_type_id) ⇒ Object



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

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

#default_configurationObject



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

def default_configuration
  DEFAULT_CONFIGURATION.dup
end

#default_localeObject



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

def default_locale
  configuration[:default_locale]
end

#delete(request) ⇒ Object



99
100
101
102
103
# File 'lib/contentful/management/client.rb', line 99

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

#execute_request(request) ⇒ Object



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

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
  Response.new(raw_response, request)
end

#get(request) ⇒ Object



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

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

#gzip_encodedObject



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

def gzip_encoded
  configuration[:gzip_encoded]
end

#organization_header(organization_id) ⇒ Object



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

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

#post(request) ⇒ Object



111
112
113
114
115
# File 'lib/contentful/management/client.rb', line 111

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

#protocolObject



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

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

#put(request) ⇒ Object



117
118
119
120
121
# File 'lib/contentful/management/client.rb', line 117

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



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

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.



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/contentful/management/client.rb', line 168

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



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

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

#update_dynamic_entry_cache!(content_types) ⇒ Object



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

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.



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

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



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

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

#user_agentObject



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

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

#version_header(version) ⇒ Object



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

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

#zero_length_headerObject



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

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