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',
}
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.
28
29
30
31
32
33
|
# File 'lib/contentful/management/client.rb', line 28
def initialize(access_token = nil, configuration = {})
@configuration = default_configuration.merge(configuration)
@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
|
#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
162
163
164
|
# File 'lib/contentful/management/client.rb', line 162
def self.shared_instance
Thread.current[:client]
end
|
Instance Method Details
#api_version ⇒ Object
59
60
61
|
# File 'lib/contentful/management/client.rb', line 59
def api_version
configuration[:api_version]
end
|
125
126
127
|
# File 'lib/contentful/management/client.rb', line 125
def
Hash['Content-Type', "application/vnd.contentful.management.v#{ api_version }+json"]
end
|
121
122
123
|
# File 'lib/contentful/management/client.rb', line 121
def
Hash['Authorization', "Bearer #{ access_token }"]
end
|
#base_url ⇒ Object
109
110
111
|
# File 'lib/contentful/management/client.rb', line 109
def base_url
"#{ protocol }://#{ configuration[:api_url]}/spaces"
end
|
79
80
81
82
83
|
# File 'lib/contentful/management/client.rb', line 79
def
self.content_type_id = nil
self.version = nil
self.organization_id = nil
end
|
141
142
143
|
# File 'lib/contentful/management/client.rb', line 141
def (content_type_id)
Hash['X-Contentful-Content-Type', content_type_id]
end
|
#default_configuration ⇒ Object
63
64
65
|
# File 'lib/contentful/management/client.rb', line 63
def default_configuration
DEFAULT_CONFIGURATION.dup
end
|
#default_locale ⇒ Object
113
114
115
|
# File 'lib/contentful/management/client.rb', line 113
def default_locale
configuration[:default_locale]
end
|
#delete(request) ⇒ Object
85
86
87
88
89
|
# File 'lib/contentful/management/client.rb', line 85
def delete(request)
execute_request(request) do |url|
self.class.delete_http(url, {}, )
end
end
|
#execute_request(request) ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/contentful/management/client.rb', line 71
def execute_request(request)
request_url = request.url
url = request.absolute? ? request_url : base_url + request_url
raw_response = yield(url)
Response.new(raw_response, request)
end
|
#get(request) ⇒ Object
91
92
93
94
95
|
# File 'lib/contentful/management/client.rb', line 91
def get(request)
execute_request(request) do |url|
self.class.get_http(url, request.query, )
end
end
|
133
134
135
|
# File 'lib/contentful/management/client.rb', line 133
def (organization_id)
Hash['X-Contentful-Organization', organization_id]
end
|
#post(request) ⇒ Object
97
98
99
100
101
|
# File 'lib/contentful/management/client.rb', line 97
def post(request)
execute_request(request) do |url|
self.class.post_http(url, request.query, )
end
end
|
#protocol ⇒ Object
117
118
119
|
# File 'lib/contentful/management/client.rb', line 117
def protocol
configuration[:secure] ? 'https' : 'http'
end
|
#put(request) ⇒ Object
103
104
105
106
107
|
# File 'lib/contentful/management/client.rb', line 103
def put(request)
execute_request(request) do |url|
self.class.put_http(url, request.query, )
end
end
|
#register_dynamic_entry(key, klass) ⇒ Object
67
68
69
|
# File 'lib/contentful/management/client.rb', line 67
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.
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/contentful/management/client.rb', line 150
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
end
|
#update_dynamic_entry_cache!(content_types) ⇒ Object
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/contentful/management/client.rb', line 48
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.
44
45
46
|
# File 'lib/contentful/management/client.rb', line 44
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
36
37
38
39
40
|
# File 'lib/contentful/management/client.rb', line 36
def update_dynamic_entry_cache_for_spaces!(spaces)
spaces.each do |space|
update_dynamic_entry_cache_for_space!(space)
end
end
|
#user_agent ⇒ Object
129
130
131
|
# File 'lib/contentful/management/client.rb', line 129
def user_agent
Hash['User-Agent', "RubyContenfulManagementGem/#{ Contentful::Management::VERSION }"]
end
|
137
138
139
|
# File 'lib/contentful/management/client.rb', line 137
def (version)
Hash['X-Contentful-Version', version]
end
|
145
146
147
|
# File 'lib/contentful/management/client.rb', line 145
def
Hash['Content-Length', 0]
end
|