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
}
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_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
181
182
183
|
# File 'lib/contentful/management/client.rb', line 181
def self.shared_instance
Thread.current[:client]
end
|
Instance Method Details
163
164
165
|
# File 'lib/contentful/management/client.rb', line 163
def (encoding)
Hash['Accept-Encoding', encoding]
end
|
#api_version ⇒ Object
67
68
69
|
# File 'lib/contentful/management/client.rb', line 67
def api_version
configuration[:api_version]
end
|
139
140
141
|
# File 'lib/contentful/management/client.rb', line 139
def
Hash['Content-Type', "application/vnd.contentful.management.v#{ api_version }+json"]
end
|
135
136
137
|
# File 'lib/contentful/management/client.rb', line 135
def
Hash['Authorization', "Bearer #{ access_token }"]
end
|
#base_url ⇒ Object
123
124
125
|
# File 'lib/contentful/management/client.rb', line 123
def base_url
"#{ protocol }://#{ configuration[:api_url]}/spaces"
end
|
93
94
95
96
97
|
# File 'lib/contentful/management/client.rb', line 93
def
self.content_type_id = nil
self.version = nil
self.organization_id = nil
end
|
155
156
157
|
# File 'lib/contentful/management/client.rb', line 155
def (content_type_id)
Hash['X-Contentful-Content-Type', content_type_id]
end
|
#default_configuration ⇒ Object
75
76
77
|
# File 'lib/contentful/management/client.rb', line 75
def default_configuration
DEFAULT_CONFIGURATION.dup
end
|
#default_locale ⇒ Object
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, {}, )
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: }) if logger
raw_response = yield(url)
logger.debug(response: raw_response) if logger
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, )
end
end
|
#gzip_encoded ⇒ Object
71
72
73
|
# File 'lib/contentful/management/client.rb', line 71
def gzip_encoded
configuration[:gzip_encoded]
end
|
147
148
149
|
# File 'lib/contentful/management/client.rb', line 147
def (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, )
end
end
|
#protocol ⇒ Object
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, )
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
|
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
= {}
.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
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_agent ⇒ Object
143
144
145
|
# File 'lib/contentful/management/client.rb', line 143
def user_agent
Hash['User-Agent', "RubyContenfulManagementGem/#{ Contentful::Management::VERSION }"]
end
|
151
152
153
|
# File 'lib/contentful/management/client.rb', line 151
def (version)
Hash['X-Contentful-Version', version]
end
|
159
160
161
|
# File 'lib/contentful/management/client.rb', line 159
def
Hash['Content-Length', 0]
end
|