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',
}

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_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

#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



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

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

Instance Method Details

#api_versionObject



59
60
61
# File 'lib/contentful/management/client.rb', line 59

def api_version
  configuration[:api_version]
end

#api_version_headerObject



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

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

#authentication_headerObject



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

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

#base_urlObject



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

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

#clear_headersObject



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

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

#content_type_header(content_type_id) ⇒ Object



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

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

#default_configurationObject



63
64
65
# File 'lib/contentful/management/client.rb', line 63

def default_configuration
  DEFAULT_CONFIGURATION.dup
end

#default_localeObject



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, {}, request_headers)
  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)
  clear_headers
  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, request_headers)
  end
end

#organization_header(organization_id) ⇒ Object



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

def organization_header(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, request_headers)
  end
end

#protocolObject



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, request_headers)
  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

#request_headersObject

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 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
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_agentObject



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

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

#version_header(version) ⇒ Object



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

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

#zero_length_headerObject



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

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