contentful-management 0.0.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +24 -0
- data/.reek +3 -0
- data/.rspec +3 -0
- data/.rubocop.yml +16 -0
- data/.travis.yml +9 -0
- data/CHANGELOG.md +2 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +408 -0
- data/Rakefile +37 -0
- data/bin/cma-console +8 -0
- data/contentful-management.gemspec +33 -0
- data/examples/blog.rb +82 -0
- data/examples/content_types.rb +45 -0
- data/examples/create_space.rb +35 -0
- data/lib/contentful/management.rb +12 -0
- data/lib/contentful/management/array.rb +35 -0
- data/lib/contentful/management/asset.rb +163 -0
- data/lib/contentful/management/client.rb +160 -0
- data/lib/contentful/management/content_type.rb +180 -0
- data/lib/contentful/management/dynamic_entry.rb +71 -0
- data/lib/contentful/management/entry.rb +213 -0
- data/lib/contentful/management/error.rb +62 -0
- data/lib/contentful/management/field.rb +34 -0
- data/lib/contentful/management/file.rb +15 -0
- data/lib/contentful/management/http_client.rb +22 -0
- data/lib/contentful/management/link.rb +23 -0
- data/lib/contentful/management/locale.rb +48 -0
- data/lib/contentful/management/location.rb +15 -0
- data/lib/contentful/management/request.rb +82 -0
- data/lib/contentful/management/resource.rb +166 -0
- data/lib/contentful/management/resource/array_like.rb +37 -0
- data/lib/contentful/management/resource/fields.rb +69 -0
- data/lib/contentful/management/resource/refresher.rb +21 -0
- data/lib/contentful/management/resource/system_properties.rb +56 -0
- data/lib/contentful/management/resource_builder.rb +269 -0
- data/lib/contentful/management/response.rb +72 -0
- data/lib/contentful/management/space.rb +159 -0
- data/lib/contentful/management/support.rb +24 -0
- data/lib/contentful/management/version.rb +6 -0
- data/spec/fixtures/vcr_cassettes/array_page_1.yml +1337 -0
- data/spec/fixtures/vcr_cassettes/asset/all.yml +611 -0
- data/spec/fixtures/vcr_cassettes/asset/archive.yml +228 -0
- data/spec/fixtures/vcr_cassettes/asset/archive_published.yml +177 -0
- data/spec/fixtures/vcr_cassettes/asset/archived_false.yml +228 -0
- data/spec/fixtures/vcr_cassettes/asset/archived_true.yml +228 -0
- data/spec/fixtures/vcr_cassettes/asset/create.yml +150 -0
- data/spec/fixtures/vcr_cassettes/asset/create_with_already_used_id.yml +56 -0
- data/spec/fixtures/vcr_cassettes/asset/create_with_custom_id.yml +150 -0
- data/spec/fixtures/vcr_cassettes/asset/destroy.yml +159 -0
- data/spec/fixtures/vcr_cassettes/asset/destroy_published.yml +175 -0
- data/spec/fixtures/vcr_cassettes/asset/find.yml +108 -0
- data/spec/fixtures/vcr_cassettes/asset/find_not_found.yml +64 -0
- data/spec/fixtures/vcr_cassettes/asset/image_url.yml +133 -0
- data/spec/fixtures/vcr_cassettes/asset/locale.yml +110 -0
- data/spec/fixtures/vcr_cassettes/asset/publish.yml +228 -0
- data/spec/fixtures/vcr_cassettes/asset/publish_already_published.yml +176 -0
- data/spec/fixtures/vcr_cassettes/asset/published_false.yml +228 -0
- data/spec/fixtures/vcr_cassettes/asset/published_true.yml +226 -0
- data/spec/fixtures/vcr_cassettes/asset/save_update.yml +242 -0
- data/spec/fixtures/vcr_cassettes/asset/set_locale.yml +110 -0
- data/spec/fixtures/vcr_cassettes/asset/unarchive.yml +228 -0
- data/spec/fixtures/vcr_cassettes/asset/unarchive_already_unarchive.yml +168 -0
- data/spec/fixtures/vcr_cassettes/asset/unarchive_already_unarchived.yml +168 -0
- data/spec/fixtures/vcr_cassettes/asset/unpublish.yml +228 -0
- data/spec/fixtures/vcr_cassettes/asset/unpublish_already_unpublished.yml +168 -0
- data/spec/fixtures/vcr_cassettes/asset/update_file.yml +207 -0
- data/spec/fixtures/vcr_cassettes/asset/update_to_specified_locale.yml +224 -0
- data/spec/fixtures/vcr_cassettes/asset/update_with_default_locale_without_file.yml +214 -0
- data/spec/fixtures/vcr_cassettes/content_type/activate.yml +246 -0
- data/spec/fixtures/vcr_cassettes/content_type/activate_with_invalid_version.yml +186 -0
- data/spec/fixtures/vcr_cassettes/content_type/activated_false.yml +246 -0
- data/spec/fixtures/vcr_cassettes/content_type/activated_true.yml +257 -0
- data/spec/fixtures/vcr_cassettes/content_type/all.yml +190 -0
- data/spec/fixtures/vcr_cassettes/content_type/create.yml +87 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_content_type_with_id.yml +87 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_with_Array_field.yml +94 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_with_Boolean_field.yml +94 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_with_Date_field.yml +94 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_with_Integer_field.yml +94 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_with_Link_field.yml +95 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_with_Location_field.yml +94 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_with_Number_field.yml +94 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_with_Object_field.yml +94 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_with_Symbol_field.yml +94 -0
- data/spec/fixtures/vcr_cassettes/content_type/create_with_Text_field.yml +94 -0
- data/spec/fixtures/vcr_cassettes/content_type/deactivate.yml +246 -0
- data/spec/fixtures/vcr_cassettes/content_type/deactivate_already_deactivated.yml +176 -0
- data/spec/fixtures/vcr_cassettes/content_type/deactivate_with_entries.yml +236 -0
- data/spec/fixtures/vcr_cassettes/content_type/deactivate_with_version_change.yml +248 -0
- data/spec/fixtures/vcr_cassettes/content_type/destroy.yml +148 -0
- data/spec/fixtures/vcr_cassettes/content_type/destroy_activated.yml +164 -0
- data/spec/fixtures/vcr_cassettes/content_type/entry/create.yml +1344 -0
- data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_camel_case_id_to_multiple_locales.yml +235 -0
- data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_entries.yml +398 -0
- data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_entries_for_multiple_locales.yml +3157 -0
- data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_multiple_locales.yml +230 -0
- data/spec/fixtures/vcr_cassettes/content_type/fields/add.yml +345 -0
- data/spec/fixtures/vcr_cassettes/content_type/fields/create.yml +353 -0
- data/spec/fixtures/vcr_cassettes/content_type/fields/create_array_with_params.yml +228 -0
- data/spec/fixtures/vcr_cassettes/content_type/fields/create_with_params.yml +221 -0
- data/spec/fixtures/vcr_cassettes/content_type/fields/destroy.yml +349 -0
- data/spec/fixtures/vcr_cassettes/content_type/fields/update_field.yml +358 -0
- data/spec/fixtures/vcr_cassettes/content_type/find.yml +180 -0
- data/spec/fixtures/vcr_cassettes/content_type/find_not_found.yml +64 -0
- data/spec/fixtures/vcr_cassettes/content_type/save_new.yml +876 -0
- data/spec/fixtures/vcr_cassettes/content_type/save_updated.yml +345 -0
- data/spec/fixtures/vcr_cassettes/content_type/save_with_added_field.yml +217 -0
- data/spec/fixtures/vcr_cassettes/content_type/update.yml +175 -0
- data/spec/fixtures/vcr_cassettes/content_type/update_change_field_name.yml +222 -0
- data/spec/fixtures/vcr_cassettes/content_type/update_remove_field.yml +231 -0
- data/spec/fixtures/vcr_cassettes/content_type/update_with_fields.yml +222 -0
- data/spec/fixtures/vcr_cassettes/content_type/update_with_one_new_field.yml +251 -0
- data/spec/fixtures/vcr_cassettes/entry/all.yml +139 -0
- data/spec/fixtures/vcr_cassettes/entry/archive.yml +222 -0
- data/spec/fixtures/vcr_cassettes/entry/archive_published.yml +187 -0
- data/spec/fixtures/vcr_cassettes/entry/archived_false.yml +178 -0
- data/spec/fixtures/vcr_cassettes/entry/archived_true.yml +248 -0
- data/spec/fixtures/vcr_cassettes/entry/create.yml +245 -0
- data/spec/fixtures/vcr_cassettes/entry/create_test.yml +150 -0
- data/spec/fixtures/vcr_cassettes/entry/create_with_asset.yml +403 -0
- data/spec/fixtures/vcr_cassettes/entry/create_with_assets.yml +419 -0
- data/spec/fixtures/vcr_cassettes/entry/create_with_entries.yml +418 -0
- data/spec/fixtures/vcr_cassettes/entry/create_with_entry.yml +493 -0
- data/spec/fixtures/vcr_cassettes/entry/create_with_location.yml +259 -0
- data/spec/fixtures/vcr_cassettes/entry/create_with_symbols.yml +271 -0
- data/spec/fixtures/vcr_cassettes/entry/destory_published.yml +172 -0
- data/spec/fixtures/vcr_cassettes/entry/destroy.yml +156 -0
- data/spec/fixtures/vcr_cassettes/entry/find.yml +129 -0
- data/spec/fixtures/vcr_cassettes/entry/find_not_found.yml +64 -0
- data/spec/fixtures/vcr_cassettes/entry/publish.yml +248 -0
- data/spec/fixtures/vcr_cassettes/entry/publish_already_published.yml +186 -0
- data/spec/fixtures/vcr_cassettes/entry/published_false.yml +248 -0
- data/spec/fixtures/vcr_cassettes/entry/published_true.yml +257 -0
- data/spec/fixtures/vcr_cassettes/entry/save_update.yml +211 -0
- data/spec/fixtures/vcr_cassettes/entry/unarchive.yml +248 -0
- data/spec/fixtures/vcr_cassettes/entry/unarchive_already_unarchived.yml +178 -0
- data/spec/fixtures/vcr_cassettes/entry/unpublish.yml +248 -0
- data/spec/fixtures/vcr_cassettes/entry/unpublish_already_unpublished.yml +178 -0
- data/spec/fixtures/vcr_cassettes/entry/update.yml +626 -0
- data/spec/fixtures/vcr_cassettes/entry/update_unlocalized_field.yml +308 -0
- data/spec/fixtures/vcr_cassettes/entry/update_with_custom_locale.yml +332 -0
- data/spec/fixtures/vcr_cassettes/get_request.yml +37 -0
- data/spec/fixtures/vcr_cassettes/locale/all_for_space.yml +321 -0
- data/spec/fixtures/vcr_cassettes/locale/create_for_space.yml +95 -0
- data/spec/fixtures/vcr_cassettes/locale/find.yml +95 -0
- data/spec/fixtures/vcr_cassettes/locale/find_for_space_not_found.yml +67 -0
- data/spec/fixtures/vcr_cassettes/space/all.yml +1201 -0
- data/spec/fixtures/vcr_cassettes/space/asset/all.yml +475 -0
- data/spec/fixtures/vcr_cassettes/space/asset/assets.yml +826 -0
- data/spec/fixtures/vcr_cassettes/space/asset/create.yml +508 -0
- data/spec/fixtures/vcr_cassettes/space/asset/create_with_multiple_locales.yml +1062 -0
- data/spec/fixtures/vcr_cassettes/space/asset/find.yml +634 -0
- data/spec/fixtures/vcr_cassettes/space/content_type/all.yml +470 -0
- data/spec/fixtures/vcr_cassettes/space/content_type/content_types.yml +341 -0
- data/spec/fixtures/vcr_cassettes/space/content_type/create.yml +425 -0
- data/spec/fixtures/vcr_cassettes/space/content_type/find.yml +493 -0
- data/spec/fixtures/vcr_cassettes/space/create.yml +85 -0
- data/spec/fixtures/vcr_cassettes/space/create_when_limit_has_been_reached.yml +67 -0
- data/spec/fixtures/vcr_cassettes/space/create_with_unknown_organization.yml +65 -0
- data/spec/fixtures/vcr_cassettes/space/create_without_organization.yml +83 -0
- data/spec/fixtures/vcr_cassettes/space/destory.yml +196 -0
- data/spec/fixtures/vcr_cassettes/space/entry/all.yml +634 -0
- data/spec/fixtures/vcr_cassettes/space/entry/entries.yml +498 -0
- data/spec/fixtures/vcr_cassettes/space/entry/find.yml +624 -0
- data/spec/fixtures/vcr_cassettes/space/find.yml +270 -0
- data/spec/fixtures/vcr_cassettes/space/find_not_found.yml +67 -0
- data/spec/fixtures/vcr_cassettes/space/locale/all.yml +690 -0
- data/spec/fixtures/vcr_cassettes/space/locale/create.yml +572 -0
- data/spec/fixtures/vcr_cassettes/space/locale/create_with_the_same_code.yml +585 -0
- data/spec/fixtures/vcr_cassettes/space/locale/find.yml +572 -0
- data/spec/fixtures/vcr_cassettes/space/locale/find_not_found.yml +580 -0
- data/spec/fixtures/vcr_cassettes/space/locale/locales.yml +480 -0
- data/spec/fixtures/vcr_cassettes/space/locale/update.yml +700 -0
- data/spec/fixtures/vcr_cassettes/space/save_new_space.yml +67 -0
- data/spec/fixtures/vcr_cassettes/space/save_update_space.yml +352 -0
- data/spec/fixtures/vcr_cassettes/space/update.yml +350 -0
- data/spec/fixtures/vcr_cassettes/space/update_with_the_same_data.yml +350 -0
- data/spec/lib/contentful/management/array_spec.rb +59 -0
- data/spec/lib/contentful/management/asset_spec.rb +321 -0
- data/spec/lib/contentful/management/client_spec.rb +80 -0
- data/spec/lib/contentful/management/content_type_spec.rb +455 -0
- data/spec/lib/contentful/management/entry_spec.rb +308 -0
- data/spec/lib/contentful/management/locale_spec.rb +53 -0
- data/spec/lib/contentful/management/space_spec.rb +317 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/support/vcr.rb +17 -0
- metadata +554 -0
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'bundler'
|
8
|
+
rescue LoadError => e
|
9
|
+
warn e.message
|
10
|
+
warn 'Run `gem install bundler` to install Bundler.'
|
11
|
+
exit 1
|
12
|
+
end
|
13
|
+
|
14
|
+
begin
|
15
|
+
Bundler.setup(:development)
|
16
|
+
rescue Bundler::BundlerError => e
|
17
|
+
warn e.message
|
18
|
+
warn 'Run `bundle install` to install missing gems.'
|
19
|
+
exit e.status_code
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rspec/core/rake_task'
|
23
|
+
RSpec::Core::RakeTask.new
|
24
|
+
|
25
|
+
require 'rubocop/rake_task'
|
26
|
+
RuboCop::RakeTask.new
|
27
|
+
|
28
|
+
require 'reek/rake/task'
|
29
|
+
Reek::Rake::Task.new do |t|
|
30
|
+
t.fail_on_error = false
|
31
|
+
end
|
32
|
+
|
33
|
+
desc 'Run specs, rubocop and reek'
|
34
|
+
task ci: %w(spec reek rubocop)
|
35
|
+
|
36
|
+
task test: :spec
|
37
|
+
task default: :spec
|
data/bin/cma-console
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'contentful/management/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'contentful-management'
|
8
|
+
spec.version = Contentful::Management::VERSION
|
9
|
+
spec.authors = ['Piotr Protas','Tomasz Warkocki','Contentful GmbH (Andreas Tiefenthaler)']
|
10
|
+
spec.email = ['piotrek@codequest.com','warkocz@gmail.com', 'rubygems@contentful.com']
|
11
|
+
spec.summary = %q{contentful management api}
|
12
|
+
spec.description = %q{Ruby client for the https://www.contentful.com Content Management API}
|
13
|
+
spec.homepage = 'https://github.com/contentful/contentful-management.rb'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'http', '~> 0.6'
|
22
|
+
spec.add_dependency 'multi_json', '~> 1'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3'
|
28
|
+
spec.add_development_dependency 'rspec-its'
|
29
|
+
spec.add_development_dependency 'rubocop'
|
30
|
+
spec.add_development_dependency 'reek'
|
31
|
+
spec.add_development_dependency 'vcr'
|
32
|
+
spec.add_development_dependency 'webmock', '~> 1', '>= 1.17.3'
|
33
|
+
end
|
data/examples/blog.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
###
|
2
|
+
# This should be and behave like a living example that grows with the code itself.
|
3
|
+
#
|
4
|
+
# TODO:
|
5
|
+
# - localisation
|
6
|
+
# - create links between posts and categories
|
7
|
+
# - link assets to posts
|
8
|
+
# - unpublish all posts
|
9
|
+
# - update assets
|
10
|
+
# - remove assets
|
11
|
+
|
12
|
+
require 'pry'
|
13
|
+
|
14
|
+
access_token = 'access token'
|
15
|
+
organization = 'organization id'
|
16
|
+
|
17
|
+
require 'contentful/management'
|
18
|
+
|
19
|
+
client = Contentful::Management::Client.new(access_token)
|
20
|
+
|
21
|
+
blog_space = Contentful::Management::Space.create(name: 'Blog', organization_id: organization)
|
22
|
+
# blog_space = Contentful::Management::Space.find('zlso53r1zad6')
|
23
|
+
|
24
|
+
blog_space.locales.create(name: 'English', code: 'en-US')
|
25
|
+
blog_space.locales.create(name: 'German', code: 'de-DE')
|
26
|
+
|
27
|
+
category_type = blog_space.content_types.create(name: 'Category')
|
28
|
+
category_type.fields.create(id: 'category_name', name: 'Category Name', type: 'Text', localized: true)
|
29
|
+
category_type.fields.create(id: 'category_description', name: 'Category Description', type: 'Text', localized: true)
|
30
|
+
|
31
|
+
post_type = blog_space.content_types.create(name: 'Post')
|
32
|
+
post_type.fields.create(id: 'post_title', name: 'Post Title', type: 'Text', localized: true)
|
33
|
+
post_type.fields.create(id: 'post_author', name: 'Post Author', type: 'Text', localized: true)
|
34
|
+
post_type.fields.create(id: 'post_body', name: 'Post Body', type: 'Text', localized: true)
|
35
|
+
|
36
|
+
categories = Contentful::Management::Field.new
|
37
|
+
categories.id = 'post_category'
|
38
|
+
categories.type = 'link'
|
39
|
+
categories.link_type = 'Entry'
|
40
|
+
post_type.fields.create(id: 'post_categories', name: 'Post Categories', type: 'Array', items: categories)
|
41
|
+
|
42
|
+
# category_type = blog_space.content_types.find('3bMvEx9ROwEQCmyGcYOI8c')
|
43
|
+
# post_type = blog_space.content_types.find('1D93omOM0YocSsGwY8wY6u')
|
44
|
+
|
45
|
+
post_type.activate
|
46
|
+
category_type.activate
|
47
|
+
|
48
|
+
sleep 10 # prevent race conditions
|
49
|
+
|
50
|
+
puts "creating categories"
|
51
|
+
entries = []
|
52
|
+
entries << category_type.entries.create(category_name: 'Misc', category_description: 'Misc stuff')
|
53
|
+
entries << category_type.entries.create(category_name: 'Serious', category_description: 'Serious stuff')
|
54
|
+
|
55
|
+
# TODO: add links to categories
|
56
|
+
entries << post_type.entries.create(post_title: 'First Post', post_author: 'Andy', post_body: 'Letterpress sustainable authentic, disrupt semiotics actually kitsch. Direct trade Cosby sweater Austin, Pitchfork flexitarian small batch authentic roof party 8-bit YOLO literally Neutra pour-over American Apparel dreamcatcher. High Life distillery cliche YOLO, flexitarian four loko put a bird on it plaid Marfa Shoreditch seitan Echo Park bicycle rights Pinterest PBR. Drinking vinegar Banksy gastropub, stumptown occupy farm-to-table Blue Bottle tattooed Truffaut single-origin coffee iPhone locavore pug. Blue Bottle cray quinoa farm-to-table Bushwick tousled. Kitsch beard tousled, American Apparel XOXO vegan readymade Pitchfork church-key 3 wolf moon direct trade lo-fi. Food truck try-hard deep v salvia raw denim.', locale: 'en-US')
|
57
|
+
entries << post_type.entries.create(post_title: 'Second Post', post_author: 'Andy', post_body: "Fixie hashtag pour-over disrupt raw denim bespoke semiotics, typewriter Shoreditch messenger bag Thundercats chillwave DIY. Forage hoodie squid hella Kickstarter Thundercats, Banksy scenester flannel disrupt PBR&B fap chia. Small batch Brooklyn Williamsburg hella viral banh mi selfies organic. Semiotics Brooklyn skateboard cray messenger bag Echo Park. PBR&B High Life wolf meh, typewriter Bushwick VHS Cosby sweater mlkshk farm-to-table. Fingerstache readymade PBR literally, fixie letterpress swag fap. Authentic distillery banh mi, forage pork belly craft beer locavore DIY 90's iPhone 3 wolf moon synth.", locale: 'de-DE')
|
58
|
+
|
59
|
+
asset = blog_space.assets.new
|
60
|
+
|
61
|
+
asset.title_with_locales = {'en-US' => 'title picture 1', 'de-DE' => 'title picture 2'}
|
62
|
+
asset.description_with_locales = {'en-US' => 'space_asset_desc', 'de-DE' => 'space_asset_desc'}
|
63
|
+
|
64
|
+
file1 = Contentful::Management::File.new
|
65
|
+
file1.properties[:contentType] = 'image/jpeg'
|
66
|
+
file1.properties[:fileName] = 'pic1.jpg'
|
67
|
+
file1.properties[:upload] = 'https://upload.wikimedia.org/wikipedia/commons/c/c7/Gasometer_Berlin_Sch%C3%B6neberg_2011.jpg'
|
68
|
+
|
69
|
+
file2 = Contentful::Management::File.new
|
70
|
+
file2.properties[:contentType] = 'image/jpeg'
|
71
|
+
file2.properties[:fileName] = 'pic1.jpg'
|
72
|
+
file2.properties[:upload] = 'https://upload.wikimedia.org/wikipedia/commons/b/bd/Gasometer_Schoeneberg_innen.jpg'
|
73
|
+
|
74
|
+
asset.file_with_locales = {'en-US' => file1, 'de-DE' => file2}
|
75
|
+
asset.save
|
76
|
+
|
77
|
+
entries.map(&:publish)
|
78
|
+
|
79
|
+
puts "Press any key to destroy space"
|
80
|
+
gets
|
81
|
+
|
82
|
+
blog_space.destroy
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'pry'
|
2
|
+
|
3
|
+
access_token = 'access token'
|
4
|
+
organization = 'organization id'
|
5
|
+
|
6
|
+
require 'contentful/management'
|
7
|
+
|
8
|
+
|
9
|
+
client = Contentful::Management::Client.new(access_token)
|
10
|
+
space = Contentful::Management::Space.create(name: 'MySpace', organization_id: organization)
|
11
|
+
|
12
|
+
p space.content_types
|
13
|
+
|
14
|
+
type1 = space.content_types.create(name: 'ContentType 1')
|
15
|
+
|
16
|
+
p space.content_types.all
|
17
|
+
|
18
|
+
field = Contentful::Management::Field.new
|
19
|
+
field.id = 'such_content'
|
20
|
+
field.name = 'Such Content'
|
21
|
+
field.type = 'Text' #content types maybe as symbol?
|
22
|
+
|
23
|
+
type2 = space.content_types.new
|
24
|
+
type2.name = 'ContentType 2'
|
25
|
+
type2.fields = [field]
|
26
|
+
type2.save
|
27
|
+
|
28
|
+
field2 = Contentful::Management::Field.new
|
29
|
+
field2.id = 'wow_content'
|
30
|
+
field2.name = 'Wow Content'
|
31
|
+
field2.type = 'Location' #content types maybe as symbol?
|
32
|
+
|
33
|
+
|
34
|
+
type2.update(name: 'whoat', fields: [field, field2])
|
35
|
+
|
36
|
+
type2.fields.add(field2)
|
37
|
+
|
38
|
+
type2.activate
|
39
|
+
p type2.active?
|
40
|
+
type2.deactivate
|
41
|
+
|
42
|
+
type1.destroy
|
43
|
+
|
44
|
+
#clean up afterwards!
|
45
|
+
space.destroy
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'pry'
|
2
|
+
|
3
|
+
access_token = 'access token'
|
4
|
+
organization = 'organization id'
|
5
|
+
|
6
|
+
require 'contentful/management'
|
7
|
+
|
8
|
+
client = Contentful::Management::Client.new(access_token)
|
9
|
+
|
10
|
+
spaces = Contentful::Management::Space.all
|
11
|
+
|
12
|
+
my_space = Contentful::Management::Space.create(name: 'MySpace', organization_id: organization)
|
13
|
+
|
14
|
+
my_space.update(name: 'MyNewSpace')
|
15
|
+
|
16
|
+
dat_space = Contentful::Management::Space.find(my_space.id)
|
17
|
+
|
18
|
+
|
19
|
+
dat_space.locales.create(name: 'English', code: 'en-US')
|
20
|
+
|
21
|
+
locales = dat_space.locales
|
22
|
+
# locales.map(&:destroy) # not implemented yet?
|
23
|
+
|
24
|
+
dat_space.destroy
|
25
|
+
|
26
|
+
|
27
|
+
# XXX: This does not set the organization_id when a space should be created
|
28
|
+
#
|
29
|
+
your_space = Contentful::Management::Space.new
|
30
|
+
your_space.organization = organization
|
31
|
+
your_space.name = 'YourSpace'
|
32
|
+
your_space.save
|
33
|
+
|
34
|
+
|
35
|
+
your_space.destroy
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'contentful/management/version'
|
3
|
+
require 'contentful/management/support'
|
4
|
+
require 'contentful/management/resource'
|
5
|
+
require 'contentful/management/client'
|
6
|
+
require 'contentful/management/space'
|
7
|
+
|
8
|
+
module Contentful
|
9
|
+
module Management
|
10
|
+
# Your code goes here...
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require_relative 'resource'
|
3
|
+
require_relative 'resource/array_like'
|
4
|
+
|
5
|
+
module Contentful
|
6
|
+
module Management
|
7
|
+
# Resource Class for Arrays (e.g. search results)
|
8
|
+
# https://www.contentful.com/developers/documentation/content-delivery-api/#arrays
|
9
|
+
# It also provides an #each method and includes Ruby's Enumerable module (gives you methods like #min, #first, etc)
|
10
|
+
class Array
|
11
|
+
DEFAULT_LIMIT = 100
|
12
|
+
|
13
|
+
include Contentful::Management::Resource
|
14
|
+
include Contentful::Management::Resource::SystemProperties
|
15
|
+
include Contentful::Management::Resource::ArrayLike
|
16
|
+
|
17
|
+
property :total, :integer
|
18
|
+
property :limit, :integer
|
19
|
+
property :skip, :integer
|
20
|
+
property :items
|
21
|
+
|
22
|
+
# Simplifies pagination
|
23
|
+
def next_page
|
24
|
+
if request
|
25
|
+
new_skip = (skip || 0) + (limit || DEFAULT_LIMIT)
|
26
|
+
new_request = request.copy
|
27
|
+
new_request.query[:skip] = new_skip
|
28
|
+
new_request.get
|
29
|
+
else
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require_relative 'resource'
|
3
|
+
require_relative 'resource/fields'
|
4
|
+
|
5
|
+
module Contentful
|
6
|
+
module Management
|
7
|
+
class Asset
|
8
|
+
|
9
|
+
FIELDS_COERCIONS = {
|
10
|
+
title: :hash,
|
11
|
+
description: :hash,
|
12
|
+
file: Contentful::Management::File
|
13
|
+
}
|
14
|
+
|
15
|
+
def self.fields_coercions
|
16
|
+
FIELDS_COERCIONS
|
17
|
+
end
|
18
|
+
|
19
|
+
include Contentful::Management::Resource
|
20
|
+
include Contentful::Management::Resource::Fields
|
21
|
+
include Contentful::Management::Resource::SystemProperties
|
22
|
+
include Contentful::Management::Resource::Refresher
|
23
|
+
|
24
|
+
def self.all(space_id)
|
25
|
+
request = Request.new("/#{ space_id }/assets")
|
26
|
+
response = request.get
|
27
|
+
result = ResourceBuilder.new(self, response, {}, {})
|
28
|
+
result.run
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.find(space_id, asset_id)
|
32
|
+
request = Request.new("/#{ space_id }/assets/#{ asset_id }")
|
33
|
+
response = request.get
|
34
|
+
result = ResourceBuilder.new(self, response, {}, {})
|
35
|
+
result.run
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.create(space_id, attributes)
|
39
|
+
asset = self.new
|
40
|
+
asset.instance_variable_set(:@fields, attributes[:fields] || {})
|
41
|
+
asset.locale = attributes[:locale] if attributes[:locale]
|
42
|
+
asset.title = attributes[:title] if attributes[:title]
|
43
|
+
asset.description = attributes[:description] if attributes[:description]
|
44
|
+
asset.file = attributes[:file] if attributes[:file]
|
45
|
+
|
46
|
+
request = Request.new("/#{ space_id }/assets/#{ attributes[:id] || ''}", { fields: asset.fields_for_query })
|
47
|
+
response = attributes[:id].nil? ? request.post : request.put
|
48
|
+
result = ResourceBuilder.new(self, response, {}, {}).run
|
49
|
+
result.process_files if result.is_a? self
|
50
|
+
result
|
51
|
+
end
|
52
|
+
|
53
|
+
def process_files
|
54
|
+
instance_variable_get(:@fields).keys.each do |locale|
|
55
|
+
request = Request.new("/#{ space.id }/assets/#{ id }/files/#{ locale }/process", {}, id = nil, version: sys[:version])
|
56
|
+
request.put
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def update(attributes)
|
61
|
+
self.title = attributes[:title] if attributes[:title]
|
62
|
+
self.description = attributes[:description] if attributes[:description]
|
63
|
+
self.file = attributes[:file] if attributes[:file]
|
64
|
+
request = Request.new("/#{ space.id }/assets/#{ id }", { fields: fields_for_query }, id = nil, version: sys[:version])
|
65
|
+
response = request.put
|
66
|
+
result = ResourceBuilder.new(self, response, {}, {}).run
|
67
|
+
refresh_data(result)
|
68
|
+
end
|
69
|
+
|
70
|
+
def save
|
71
|
+
if id.nil?
|
72
|
+
new_instance = self.class.create(self.sys[:space].id, { fields: instance_variable_get(:@fields) })
|
73
|
+
refresh_data(new_instance)
|
74
|
+
else
|
75
|
+
update(title: title, description: description, file: file)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def destroy
|
80
|
+
request = Request.new("/#{ space.id }/assets/#{ id }")
|
81
|
+
response = request.delete
|
82
|
+
if response.status == :no_content
|
83
|
+
return true
|
84
|
+
else
|
85
|
+
result = ResourceBuilder.new(self, response, {}, {})
|
86
|
+
result.run
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def publish
|
91
|
+
request = Request.new("/#{ space.id }/assets/#{ id }/published", {}, id = nil, version: sys[:version])
|
92
|
+
response = request.put
|
93
|
+
result = ResourceBuilder.new(self, response, {}, {}).run
|
94
|
+
refresh_data(result)
|
95
|
+
end
|
96
|
+
|
97
|
+
def unpublish
|
98
|
+
request = Request.new("/#{ space.id }/assets/#{ id }/published", {}, id = nil, version: sys[:version])
|
99
|
+
response = request.delete
|
100
|
+
result = ResourceBuilder.new(self, response, {}, {}).run
|
101
|
+
refresh_data(result)
|
102
|
+
end
|
103
|
+
|
104
|
+
def archive
|
105
|
+
request = Request.new("/#{ space.id }/assets/#{ id }/archived", {}, id = nil, version: sys[:version])
|
106
|
+
response = request.put
|
107
|
+
result = ResourceBuilder.new(self, response, {}, {}).run
|
108
|
+
refresh_data(result)
|
109
|
+
end
|
110
|
+
|
111
|
+
def unarchive
|
112
|
+
request = Request.new("/#{ space.id }/assets/#{ id }/archived", {}, id = nil, version: sys[:version])
|
113
|
+
response = request.delete
|
114
|
+
result = ResourceBuilder.new(self, response, {}, {}).run
|
115
|
+
refresh_data(result)
|
116
|
+
end
|
117
|
+
|
118
|
+
def published?
|
119
|
+
!sys[:publishedAt].nil?
|
120
|
+
end
|
121
|
+
|
122
|
+
def archived?
|
123
|
+
!sys[:archivedAt].nil?
|
124
|
+
end
|
125
|
+
|
126
|
+
def locale
|
127
|
+
sys && sys[:locale] ? sys[:locale] : default_locale
|
128
|
+
end
|
129
|
+
|
130
|
+
def fields_for_query
|
131
|
+
self.class.fields_coercions.keys.each_with_object({}) do |field_name, results|
|
132
|
+
results[field_name] = @fields.each_with_object({}) do |(locale, fields), field_results|
|
133
|
+
field_results[locale] = field_name == :file ? (fields[field_name] ? fields[field_name].properties : nil) : fields[field_name]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# Returns the image url of an asset
|
139
|
+
# Allows you to pass in the following options for image resizing:
|
140
|
+
# :width
|
141
|
+
# :height
|
142
|
+
# :format
|
143
|
+
# :quality
|
144
|
+
# See https://www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing
|
145
|
+
def image_url(options = {})
|
146
|
+
query = {
|
147
|
+
w: options[:w] || options[:width],
|
148
|
+
h: options[:h] || options[:height],
|
149
|
+
fm: options[:fm] || options[:format],
|
150
|
+
q: options[:q] || options[:quality]
|
151
|
+
}.reject { |_k, v| v.nil? }
|
152
|
+
|
153
|
+
if query.empty?
|
154
|
+
file.url
|
155
|
+
else
|
156
|
+
"#{ file.url }?#{ URI.encode_www_form(query) }"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|