locomotivecms_coal 1.0.0.pre.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +23 -0
- data/Gemfile.lock +104 -0
- data/LICENSE +20 -0
- data/README.md +92 -0
- data/Rakefile +24 -0
- data/lib/locomotive/coal/client.rb +53 -0
- data/lib/locomotive/coal/error.rb +27 -0
- data/lib/locomotive/coal/paginated_resources.rb +20 -0
- data/lib/locomotive/coal/request.rb +63 -0
- data/lib/locomotive/coal/resource.rb +21 -0
- data/lib/locomotive/coal/resources/content_entries.rb +35 -0
- data/lib/locomotive/coal/resources/contents.rb +25 -0
- data/lib/locomotive/coal/resources/my_account.rb +15 -0
- data/lib/locomotive/coal/resources/sites.rb +31 -0
- data/lib/locomotive/coal/resources/token.rb +15 -0
- data/lib/locomotive/coal/version.rb +8 -0
- data/lib/locomotive/coal.rb +16 -0
- data/locomotivecms_coal.gemspec +26 -0
- data/spec/fixtures/coal_cassettes/client.yml +144 -0
- data/spec/fixtures/coal_cassettes/content_entries.yml +386 -0
- data/spec/fixtures/coal_cassettes/contents.yml +120 -0
- data/spec/fixtures/coal_cassettes/my_account.yml +97 -0
- data/spec/fixtures/coal_cassettes/sites.yml +204 -0
- data/spec/fixtures/coal_cassettes/token.yml +99 -0
- data/spec/integration/client_spec.rb +33 -0
- data/spec/integration/resources/content_entries_spec.rb +52 -0
- data/spec/integration/resources/contents_spec.rb +22 -0
- data/spec/integration/resources/my_account_spec.rb +17 -0
- data/spec/integration/resources/sites_spec.rb +43 -0
- data/spec/integration/resources/token_spec.rb +38 -0
- data/spec/spec_helper.rb +46 -0
- data/spec/support/api_settings.rb +6 -0
- data/spec/support/default_resources.rb +7 -0
- data/spec/support/pry.rb +4 -0
- data/spec/unit/client_spec.rb +25 -0
- data/spec/unit/paginated_resources_spec.rb +50 -0
- data/spec/unit/resource_spec.rb +18 -0
- metadata +159 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 259ae27071883220aca274191042f544e80df7c2
|
4
|
+
data.tar.gz: f29a3c015fc1ce549bf8b33564456db6a5cf881f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f95b87e05a3311b40609f859cd2c3e3afbe60f0ad7939f2db0299f789bf3584bdc9f7bf4d1c7da00cc4959fd332850fa132682bf501c0ce6135b62102f1e6813
|
7
|
+
data.tar.gz: 33e434030b8dbdd97f9ad3fce0c07a7a6631b0af891bed7847dafdd6d27c45a96c35abfac51f8ad4e0b0f6cfbb7c0b8a190b93d3d57eeb4ce73ee32d85a059c2
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
coverage/
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :development do
|
6
|
+
end
|
7
|
+
|
8
|
+
group :test do
|
9
|
+
gem 'rspec', '~> 3.2.0'
|
10
|
+
gem 'json_spec', '~> 1.1.4'
|
11
|
+
|
12
|
+
gem 'pry-byebug', '~> 3.1.0'
|
13
|
+
|
14
|
+
gem 'webmock'
|
15
|
+
gem 'vcr'
|
16
|
+
|
17
|
+
gem 'codeclimate-test-reporter', '~> 0.4.7', require: false
|
18
|
+
gem 'coveralls', '~> 0.7.11', require: false
|
19
|
+
end
|
20
|
+
|
21
|
+
platform :ruby do
|
22
|
+
ruby '2.2.0'
|
23
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
locomotivecms_coal (1.0.0.pre.alpha)
|
5
|
+
activesupport (~> 4.2.1)
|
6
|
+
unirest (~> 1.1.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (4.2.1)
|
12
|
+
i18n (~> 0.7)
|
13
|
+
json (~> 1.7, >= 1.7.7)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
16
|
+
tzinfo (~> 1.1)
|
17
|
+
addressable (2.3.7)
|
18
|
+
byebug (4.0.3)
|
19
|
+
columnize (= 0.9.0)
|
20
|
+
codeclimate-test-reporter (0.4.7)
|
21
|
+
simplecov (>= 0.7.1, < 1.0.0)
|
22
|
+
coderay (1.1.0)
|
23
|
+
columnize (0.9.0)
|
24
|
+
coveralls (0.7.12)
|
25
|
+
multi_json (~> 1.10)
|
26
|
+
rest-client (>= 1.6.8, < 2)
|
27
|
+
simplecov (~> 0.9.1)
|
28
|
+
term-ansicolor (~> 1.3)
|
29
|
+
thor (~> 0.19.1)
|
30
|
+
crack (0.4.2)
|
31
|
+
safe_yaml (~> 1.0.0)
|
32
|
+
diff-lcs (1.2.5)
|
33
|
+
docile (1.1.5)
|
34
|
+
i18n (0.7.0)
|
35
|
+
json (1.8.2)
|
36
|
+
json_spec (1.1.4)
|
37
|
+
multi_json (~> 1.0)
|
38
|
+
rspec (>= 2.0, < 4.0)
|
39
|
+
method_source (0.8.2)
|
40
|
+
mime-types (1.25.1)
|
41
|
+
minitest (5.5.1)
|
42
|
+
multi_json (1.11.0)
|
43
|
+
pry (0.10.1)
|
44
|
+
coderay (~> 1.1.0)
|
45
|
+
method_source (~> 0.8.1)
|
46
|
+
slop (~> 3.4)
|
47
|
+
pry-byebug (3.1.0)
|
48
|
+
byebug (~> 4.0)
|
49
|
+
pry (~> 0.10)
|
50
|
+
rake (10.4.2)
|
51
|
+
rdoc (4.2.0)
|
52
|
+
rest-client (1.6.8)
|
53
|
+
mime-types (~> 1.16)
|
54
|
+
rdoc (>= 2.4.2)
|
55
|
+
rspec (3.2.0)
|
56
|
+
rspec-core (~> 3.2.0)
|
57
|
+
rspec-expectations (~> 3.2.0)
|
58
|
+
rspec-mocks (~> 3.2.0)
|
59
|
+
rspec-core (3.2.2)
|
60
|
+
rspec-support (~> 3.2.0)
|
61
|
+
rspec-expectations (3.2.0)
|
62
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
63
|
+
rspec-support (~> 3.2.0)
|
64
|
+
rspec-mocks (3.2.1)
|
65
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
66
|
+
rspec-support (~> 3.2.0)
|
67
|
+
rspec-support (3.2.2)
|
68
|
+
safe_yaml (1.0.4)
|
69
|
+
simplecov (0.9.2)
|
70
|
+
docile (~> 1.1.0)
|
71
|
+
multi_json (~> 1.0)
|
72
|
+
simplecov-html (~> 0.9.0)
|
73
|
+
simplecov-html (0.9.0)
|
74
|
+
slop (3.6.0)
|
75
|
+
term-ansicolor (1.3.0)
|
76
|
+
tins (~> 1.0)
|
77
|
+
thor (0.19.1)
|
78
|
+
thread_safe (0.3.5)
|
79
|
+
tins (1.3.5)
|
80
|
+
tzinfo (1.2.2)
|
81
|
+
thread_safe (~> 0.1)
|
82
|
+
unirest (1.1.2)
|
83
|
+
addressable (~> 2.3.5)
|
84
|
+
json (~> 1.8.1)
|
85
|
+
rest-client (~> 1.6.7)
|
86
|
+
vcr (2.9.3)
|
87
|
+
webmock (1.9.3)
|
88
|
+
addressable (>= 2.2.7)
|
89
|
+
crack (>= 0.3.2)
|
90
|
+
|
91
|
+
PLATFORMS
|
92
|
+
ruby
|
93
|
+
|
94
|
+
DEPENDENCIES
|
95
|
+
bundler (~> 1.9.1)
|
96
|
+
codeclimate-test-reporter (~> 0.4.7)
|
97
|
+
coveralls (~> 0.7.11)
|
98
|
+
json_spec (~> 1.1.4)
|
99
|
+
locomotivecms_coal!
|
100
|
+
pry-byebug (~> 3.1.0)
|
101
|
+
rake (~> 10.4.2)
|
102
|
+
rspec (~> 3.2.0)
|
103
|
+
vcr
|
104
|
+
webmock
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 NoCoffee
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Coal
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/locomotivecms/coal/badges/gpa.svg)](https://codeclimate.com/github/locomotivecms/coal) [![Dependency Status](https://gemnasium.com/locomotivecms/coal.svg)](https://gemnasium.com/locomotivecms/coal) [![Build Status](https://travis-ci.org/locomotivecms/coal.svg?branch=master)](https://travis-ci.org/locomotivecms/coal) [![Coverage Status](https://coveralls.io/repos/locomotivecms/coal/badge.svg?branch=master)](https://coveralls.io/r/locomotivecms/coal?branch=master)
|
4
|
+
|
5
|
+
The Ruby API Client for LocomotiveCMS.
|
6
|
+
|
7
|
+
## Installation [WIP]
|
8
|
+
|
9
|
+
gem install locomotivecms_coal --pre
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
### Authentication
|
14
|
+
|
15
|
+
client = Locomotive::Coal::Client.new('http://www.myengine.dev/locomotive/api', { email: <EMAIL>, api_key: <API KEY> })
|
16
|
+
|
17
|
+
### Resources
|
18
|
+
|
19
|
+
#### MyAccount
|
20
|
+
|
21
|
+
**Get the name of the logged in account**
|
22
|
+
|
23
|
+
my_account = client.my_account.get
|
24
|
+
puts my_account.name + " / " + my_account.email
|
25
|
+
|
26
|
+
#### Sites
|
27
|
+
|
28
|
+
**Get all my sites**
|
29
|
+
|
30
|
+
my_sites = client.sites.all
|
31
|
+
puts "I've got #{my_sites.size}"
|
32
|
+
|
33
|
+
**Create a new site**
|
34
|
+
|
35
|
+
my_site = client.sites.create(name: 'Acme', subdomain: 'acme', locales: ['en'], timezone: 'UTC')
|
36
|
+
|
37
|
+
**Destroy a site**
|
38
|
+
|
39
|
+
my_site = client.sites.destroy(my_site._id)
|
40
|
+
|
41
|
+
#### Content Types
|
42
|
+
|
43
|
+
**Get a content type by its slug**
|
44
|
+
|
45
|
+
*Note:* We first need to log in to the site the content type belongs to. It can be done by calling the scope_by method of the client instance.
|
46
|
+
|
47
|
+
site = client.sites.by_subdomain('acme')
|
48
|
+
site_client = client.scope_by(site)
|
49
|
+
|
50
|
+
content_type = site_client.contents.projects
|
51
|
+
|
52
|
+
#### Content Entries
|
53
|
+
|
54
|
+
**Get all the first 10 entries filtered by a property (published)**
|
55
|
+
|
56
|
+
articles = site_client.contents.articles.all({ published: true })
|
57
|
+
|
58
|
+
**Loop over all the content entries**
|
59
|
+
|
60
|
+
page = 1
|
61
|
+
while page do
|
62
|
+
articles = site_client.contents.articles.all({ published: true }, page: page)
|
63
|
+
articles.each { |article| puts article.title }
|
64
|
+
page = articles._next_page
|
65
|
+
end
|
66
|
+
|
67
|
+
**Update a content entry**
|
68
|
+
|
69
|
+
article = site_client.contents.articles.all.first
|
70
|
+
site_client.contents.articles.update(article._id, { title: 'Hello world'})
|
71
|
+
|
72
|
+
## TODO
|
73
|
+
|
74
|
+
We only implemented a few resources (my_account, sites, content types and content entries) and for some of them, not all the actions have been implemented.
|
75
|
+
|
76
|
+
Check the issues section of the repository to see what is missing.
|
77
|
+
|
78
|
+
## Credits ##
|
79
|
+
|
80
|
+
[Christian](https://github.com/cblavier), [Greg](https://github.com/gregKawet) and [Ben](https://github.com/stiiig) from the Cogip/Insert International LTD who brainstormed with me (a very long time) to find this awesome name.
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
1. Fork it ( http://github.com/<my-github-username>/locomotivecms/coal )
|
85
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
86
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
87
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
88
|
+
5. Create new Pull Request
|
89
|
+
|
90
|
+
## License
|
91
|
+
|
92
|
+
Copyright (c) 2015 NoCoffee. MIT Licensed, see LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
|
5
|
+
require 'rake'
|
6
|
+
require 'rspec'
|
7
|
+
|
8
|
+
# === Gems install tasks ===
|
9
|
+
Bundler::GemHelper.install_tasks
|
10
|
+
|
11
|
+
require_relative 'lib/locomotive/coal'
|
12
|
+
|
13
|
+
require 'rspec/core/rake_task'
|
14
|
+
RSpec::Core::RakeTask.new('spec')
|
15
|
+
|
16
|
+
RSpec::Core::RakeTask.new('spec:integration') do |spec|
|
17
|
+
spec.pattern = 'spec/integration/**/*_spec.rb'
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec::Core::RakeTask.new('spec:unit') do |spec|
|
21
|
+
spec.pattern = 'spec/unit/**/*_spec.rb'
|
22
|
+
end
|
23
|
+
|
24
|
+
task default: [:spec]
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Locomotive::Coal
|
2
|
+
|
3
|
+
class Client
|
4
|
+
|
5
|
+
attr_reader :uri, :credentials
|
6
|
+
|
7
|
+
attr_accessor :scoped_by_site
|
8
|
+
|
9
|
+
def initialize(uri, credentials)
|
10
|
+
if uri.blank? || credentials.blank?
|
11
|
+
raise MissingURIOrCredentialsError.new('URI and/or credentials are missing')
|
12
|
+
else
|
13
|
+
@uri, @credentials = URI(uri), credentials
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def token
|
18
|
+
@token ||= Resources::Token.new(uri, credentials).get
|
19
|
+
end
|
20
|
+
|
21
|
+
def my_account
|
22
|
+
@my_account ||= Resources::MyAccount.new(uri, token)
|
23
|
+
end
|
24
|
+
|
25
|
+
def sites
|
26
|
+
@sites ||= Resources::Sites.new(uri, token)
|
27
|
+
end
|
28
|
+
|
29
|
+
def contents
|
30
|
+
@contents ||= Resources::Contents.new(uri, token)
|
31
|
+
end
|
32
|
+
|
33
|
+
def scope_by(site)
|
34
|
+
if site.domains.include?(domain)
|
35
|
+
self
|
36
|
+
else
|
37
|
+
new_uri = site.domains.first
|
38
|
+
self.class.new(new_uri, self.credentials).tap do |_client|
|
39
|
+
_client.scoped_by_site = true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def domain
|
47
|
+
self.uri.to_s =~ /^https?:\/\/([^\/:]+)*/
|
48
|
+
$1
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Locomotive::Coal
|
2
|
+
|
3
|
+
# Take inspiration from this repository:
|
4
|
+
# https://github.com/vigeland/hooty
|
5
|
+
#
|
6
|
+
class Error < StandardError
|
7
|
+
|
8
|
+
def self.from_response(response)
|
9
|
+
status = response.code
|
10
|
+
if klass = case status
|
11
|
+
when 401 then Locomotive::Coal::UnauthorizedError
|
12
|
+
when 404 then Locomotive::Coal::UnknownResourceError
|
13
|
+
when 429 then Locomotive::Coal::TooManyRequestsError
|
14
|
+
when 400..499 then Error
|
15
|
+
end
|
16
|
+
klass.new(response)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class MissingURIOrCredentialsError < Error; end
|
22
|
+
class UnknownResourceError < Error; end
|
23
|
+
class TooManyRequestsError < Error; end
|
24
|
+
class UnauthorizedError < Error; end
|
25
|
+
class BadRequestError < Error; end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Locomotive::Coal
|
2
|
+
|
3
|
+
class PaginatedResources < SimpleDelegator
|
4
|
+
|
5
|
+
attr_accessor :_page, :_total_pages, :_total_entries
|
6
|
+
|
7
|
+
def initialize(list, page, total_pages, total_entries)
|
8
|
+
@_page, @_total_pages, @_total_entries = page, total_pages, total_entries
|
9
|
+
super(list)
|
10
|
+
end
|
11
|
+
|
12
|
+
def _next_page
|
13
|
+
if @_page < @_total_pages
|
14
|
+
@_page + 1
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Locomotive::Coal
|
2
|
+
|
3
|
+
module Request
|
4
|
+
|
5
|
+
def get(endpoint, parameters = {}, raw = false)
|
6
|
+
safe_request_call(raw) do
|
7
|
+
Unirest.get "#{uri.to_s}/#{endpoint}.json",
|
8
|
+
headers: { 'Accept' => 'application/json' },
|
9
|
+
auth: uri.userinfo,
|
10
|
+
parameters: parameters.merge(auth_token: token)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def post(endpoint, parameters = {})
|
15
|
+
parameters = parameters.merge(auth_token: token) if respond_to?(:token)
|
16
|
+
|
17
|
+
safe_request_call do
|
18
|
+
Unirest.post "#{uri.to_s}/#{endpoint}.json",
|
19
|
+
auth: uri.userinfo,
|
20
|
+
headers: { 'Accept' => 'application/json' },
|
21
|
+
parameters: parameters
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def put(endpoint, parameters = {})
|
26
|
+
parameters = parameters.merge(auth_token: token) if respond_to?(:token)
|
27
|
+
|
28
|
+
safe_request_call do
|
29
|
+
Unirest.put "#{uri.to_s}/#{endpoint}.json",
|
30
|
+
auth: uri.userinfo,
|
31
|
+
headers: { 'Accept' => 'application/json' },
|
32
|
+
parameters: parameters
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def delete(endpoint, id)
|
37
|
+
safe_request_call do
|
38
|
+
Unirest.delete "#{uri.to_s}/#{endpoint}/#{id}.json",
|
39
|
+
auth: uri.userinfo,
|
40
|
+
headers: { 'Accept' => 'application/json' },
|
41
|
+
parameters: { auth_token: token }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def safe_request_call(raw = false, &block)
|
48
|
+
response = begin
|
49
|
+
yield
|
50
|
+
rescue Exception => e
|
51
|
+
raise Locomotive::Coal::BadRequestError.new(e)
|
52
|
+
end
|
53
|
+
|
54
|
+
if response.code == 200
|
55
|
+
raw ? response : response.body
|
56
|
+
else
|
57
|
+
raise Locomotive::Coal::Error.from_response(response)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Locomotive::Coal
|
2
|
+
|
3
|
+
class Resource
|
4
|
+
|
5
|
+
attr_accessor :attributes
|
6
|
+
|
7
|
+
def initialize(attributes)
|
8
|
+
@attributes = attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_missing(name, *args)
|
12
|
+
if self.attributes.key?(name.to_s)
|
13
|
+
self.attributes[name.to_s]
|
14
|
+
else
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Locomotive::Coal
|
2
|
+
|
3
|
+
module Resources
|
4
|
+
|
5
|
+
class ContentEntries < Struct.new(:uri, :token, :content_type)
|
6
|
+
|
7
|
+
include Locomotive::Coal::Request
|
8
|
+
|
9
|
+
def all(query = {}, options = {})
|
10
|
+
parameters = { where: query.to_json }.merge(options)
|
11
|
+
response = get(endpoint, parameters, true)
|
12
|
+
|
13
|
+
list = response.body.map { |attributes| Resource.new(attributes) }
|
14
|
+
|
15
|
+
PaginatedResources.new(list,
|
16
|
+
options[:page] || 1,
|
17
|
+
response.headers[:x_total_pages].to_i,
|
18
|
+
response.headers[:x_total_entries].to_i)
|
19
|
+
end
|
20
|
+
|
21
|
+
def update(id, attributes)
|
22
|
+
data = put(endpoint + "/#{id}", { content_entry: attributes })
|
23
|
+
Resource.new(data)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def endpoint(name = 'entries')
|
29
|
+
"content_types/#{content_type.slug}/#{name}"
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Locomotive::Coal
|
2
|
+
module Resources
|
3
|
+
|
4
|
+
class Contents < Struct.new(:uri, :token)
|
5
|
+
|
6
|
+
include Locomotive::Coal::Request
|
7
|
+
|
8
|
+
def by_slug(slug)
|
9
|
+
get('content_types').each do |attributes|
|
10
|
+
return Resource.new(attributes) if attributes['slug'] == slug.to_s
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(meth, *args)
|
15
|
+
if content_type = by_slug(meth)
|
16
|
+
Locomotive::Coal::Resources::ContentEntries.new(uri, token, content_type)
|
17
|
+
else
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Locomotive::Coal
|
2
|
+
module Resources
|
3
|
+
|
4
|
+
class Sites < Struct.new(:uri, :token)
|
5
|
+
|
6
|
+
include Locomotive::Coal::Request
|
7
|
+
|
8
|
+
def all
|
9
|
+
get('sites').map do |attributes|
|
10
|
+
Resource.new(attributes)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def create(attributes = {})
|
15
|
+
data = post('sites', { site: attributes })
|
16
|
+
Resource.new(data)
|
17
|
+
end
|
18
|
+
|
19
|
+
def by_subdomain(subdomain)
|
20
|
+
all.find { |site| site.subdomain == subdomain.to_s }
|
21
|
+
end
|
22
|
+
|
23
|
+
def destroy(id)
|
24
|
+
data = delete('sites', id)
|
25
|
+
Resource.new(data)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'active_support/deprecation'
|
4
|
+
require 'active_support/core_ext'
|
5
|
+
require 'unirest'
|
6
|
+
|
7
|
+
require_relative 'coal/error'
|
8
|
+
require_relative 'coal/request'
|
9
|
+
require_relative 'coal/paginated_resources'
|
10
|
+
require_relative 'coal/resource'
|
11
|
+
require_relative 'coal/resources/token'
|
12
|
+
require_relative 'coal/resources/my_account'
|
13
|
+
require_relative 'coal/resources/sites'
|
14
|
+
require_relative 'coal/resources/contents'
|
15
|
+
require_relative 'coal/resources/content_entries'
|
16
|
+
require_relative 'coal/client'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative 'lib/locomotive/coal/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'locomotivecms_coal'
|
5
|
+
spec.version = Locomotive::Coal::VERSION
|
6
|
+
spec.authors = ['Didier Lafforgue']
|
7
|
+
spec.email = ['did@locomotivecms.com']
|
8
|
+
spec.description = %q{The LocomotiveCMS coal is the API ruby client for LocomotiveCMS}
|
9
|
+
spec.summary = %q{The LocomotiveCMS coal is the API ruby client for LocomotiveCMS}
|
10
|
+
spec.homepage = 'http://www.locomotivecms.com'
|
11
|
+
spec.homepage = 'https://github.com/locomotivecms/coal'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_development_dependency 'bundler', '~> 1.9.1'
|
20
|
+
spec.add_development_dependency 'rake', '~> 10.4.2'
|
21
|
+
|
22
|
+
spec.add_dependency 'unirest', '~> 1.1.2'
|
23
|
+
spec.add_dependency 'activesupport', '~> 4.2.1'
|
24
|
+
|
25
|
+
spec.required_ruby_version = '>= 2.0'
|
26
|
+
end
|