Class: Contentful::Management::Client
- Inherits:
-
Object
- Object
- Contentful::Management::Client
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_token ⇒ Object
Returns the value of attribute access_token.
18
19
20
|
# File 'lib/contentful/management/client.rb', line 18
def access_token
@access_token
end
|
#configuration ⇒ Object
Returns the value of attribute configuration.
18
19
20
|
# File 'lib/contentful/management/client.rb', line 18
def configuration
@configuration
end
|
#content_type_id ⇒ Object
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_cache ⇒ Object
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
|
#logger ⇒ Object
Returns the value of attribute logger.
18
19
20
|
# File 'lib/contentful/management/client.rb', line 18
def logger
@logger
end
|
#organization_id ⇒ Object
Returns the value of attribute organization_id.
19
20
21
|
# File 'lib/contentful/management/client.rb', line 19
def organization_id
@organization_id
end
|
#version ⇒ Object
Returns the value of attribute version.
19
20
21
|
# File 'lib/contentful/management/client.rb', line 19
def version
@version
end
|
#zero_length ⇒ Object
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_instance ⇒ Object
184
185
186
|
# File 'lib/contentful/management/client.rb', line 184
def self.shared_instance
Thread.current[:client]
end
|
Instance Method Details
166
167
168
|
# File 'lib/contentful/management/client.rb', line 166
def (encoding)
Hash['Accept-Encoding', encoding]
end
|
#api_version ⇒ Object
68
69
70
|
# File 'lib/contentful/management/client.rb', line 68
def api_version
configuration[:api_version]
end
|
142
143
144
|
# File 'lib/contentful/management/client.rb', line 142
def
Hash['Content-Type', "application/vnd.contentful.management.v#{ api_version }+json"]
end
|
138
139
140
|
# File 'lib/contentful/management/client.rb', line 138
def
Hash['Authorization', "Bearer #{ access_token }"]
end
|
#base_url ⇒ Object
126
127
128
|
# File 'lib/contentful/management/client.rb', line 126
def base_url
"#{ protocol }://#{ configuration[:api_url]}/spaces"
end
|
96
97
98
99
100
|
# File 'lib/contentful/management/client.rb', line 96
def
self.content_type_id = nil
self.version = nil
self.organization_id = nil
end
|
158
159
160
|
# File 'lib/contentful/management/client.rb', line 158
def (content_type_id)
Hash['X-Contentful-Content-Type', content_type_id]
end
|
#default_configuration ⇒ Object
76
77
78
|
# File 'lib/contentful/management/client.rb', line 76
def default_configuration
DEFAULT_CONFIGURATION.dup
end
|
#default_locale ⇒ Object
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, {}, )
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: }) if logger
raw_response = yield(url)
logger.debug(response: raw_response) if logger
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, )
end
end
|
#gzip_encoded ⇒ Object
72
73
74
|
# File 'lib/contentful/management/client.rb', line 72
def gzip_encoded
configuration[:gzip_encoded]
end
|
150
151
152
|
# File 'lib/contentful/management/client.rb', line 150
def (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, )
end
end
|
#protocol ⇒ Object
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, )
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
|
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
= {}
.merge! user_agent
.merge!
.merge!
.merge! (organization_id) if organization_id
.merge! (version) if version
.merge! if zero_length
.merge! (content_type_id) if content_type_id
.merge! ('gzip') if gzip_encoded
end
|
#setup_logger ⇒ Object
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_agent ⇒ Object
146
147
148
|
# File 'lib/contentful/management/client.rb', line 146
def user_agent
Hash['User-Agent', "RubyContentfulManagementGem/#{ Contentful::Management::VERSION }"]
end
|
154
155
156
|
# File 'lib/contentful/management/client.rb', line 154
def (version)
Hash['X-Contentful-Version', version]
end
|
162
163
164
|
# File 'lib/contentful/management/client.rb', line 162
def
Hash['Content-Length', 0]
end
|