locomotivecms_coal 1.0.0.pre.alpha.1 → 1.0.0.pre.alpha.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.DS_Store +0 -0
- data/.gitignore +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +23 -12
- data/README.md +167 -10
- data/lib/locomotive/coal/client.rb +70 -20
- data/lib/locomotive/coal/client_v2.rb +31 -0
- data/lib/locomotive/coal/error.rb +46 -5
- data/lib/locomotive/coal/resources/base.rb +57 -0
- data/lib/locomotive/coal/resources/concerns/request.rb +101 -0
- data/lib/locomotive/coal/resources/content_assets.rb +9 -0
- data/lib/locomotive/coal/resources/content_entries.rb +17 -11
- data/lib/locomotive/coal/resources/{contents.rb → content_types.rb} +4 -5
- data/lib/locomotive/coal/resources/engine_version.rb +15 -0
- data/lib/locomotive/coal/resources/my_account.rb +14 -2
- data/lib/locomotive/coal/resources/pages.rb +25 -0
- data/lib/locomotive/coal/resources/sites.rb +5 -17
- data/lib/locomotive/coal/resources/snippets.rb +9 -0
- data/lib/locomotive/coal/resources/theme_assets.rb +13 -0
- data/lib/locomotive/coal/resources/token.rb +2 -2
- data/lib/locomotive/coal/resources/translations.rb +9 -0
- data/lib/locomotive/coal/upload_io.rb +11 -0
- data/lib/locomotive/coal/version.rb +1 -1
- data/lib/locomotive/coal.rb +11 -3
- data/locomotivecms_coal.gemspec +4 -4
- data/spec/fixtures/assets/locomotive.png +0 -0
- data/spec/fixtures/assets/rails.png +0 -0
- data/spec/fixtures/coal_cassettes/client.yml +333 -69
- data/spec/fixtures/coal_cassettes/content_assets.yml +595 -0
- data/spec/fixtures/coal_cassettes/content_entries.yml +1134 -223
- data/spec/fixtures/coal_cassettes/content_types.yml +616 -0
- data/spec/fixtures/coal_cassettes/my_account.yml +167 -47
- data/spec/fixtures/coal_cassettes/pages.yml +281 -0
- data/spec/fixtures/coal_cassettes/sites.yml +93 -117
- data/spec/fixtures/coal_cassettes/snippets.yml +227 -0
- data/spec/fixtures/coal_cassettes/theme_assets.yml +406 -0
- data/spec/fixtures/coal_cassettes/token.yml +37 -55
- data/spec/fixtures/coal_cassettes/translations.yml +227 -0
- data/spec/fixtures/coal_cassettes/version.yml +89 -0
- data/spec/integration/client_spec.rb +37 -3
- data/spec/integration/resources/content_assets_spec.rb +42 -0
- data/spec/integration/resources/content_entries_spec.rb +45 -18
- data/spec/integration/resources/content_types_spec.rb +44 -0
- data/spec/integration/resources/engine_version_spec.rb +17 -0
- data/spec/integration/resources/my_account_spec.rb +22 -5
- data/spec/integration/resources/pages_spec.rb +43 -0
- data/spec/integration/resources/sites_spec.rb +9 -9
- data/spec/integration/resources/snippets_spec.rb +38 -0
- data/spec/integration/resources/theme_assets_spec.rb +43 -0
- data/spec/integration/resources/token_spec.rb +7 -7
- data/spec/integration/resources/translations_spec.rb +38 -0
- data/spec/spec_helper.rb +2 -13
- data/spec/support/api_settings.rb +7 -3
- data/spec/support/assets.rb +4 -0
- data/spec/support/vcr.rb +10 -0
- data/spec/unit/error_spec.rb +28 -0
- metadata +71 -13
- data/lib/locomotive/coal/request.rb +0 -63
- data/spec/fixtures/coal_cassettes/contents.yml +0 -120
- data/spec/integration/resources/contents_spec.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a8f142dbb0800706987fe62347716b7d5928b54
|
4
|
+
data.tar.gz: 83488a8289d95c6b88f45ca9f936d16703257de0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94c6a362f32059241e7980251a914d9973fde88123678ee4a716ac4b63b7473d13c1c2cc389e40808068f29475cd15e1eb0d7bae4a18bcf3a7b31376ff9159f5
|
7
|
+
data.tar.gz: 102af54aebaa85d419c03cbad4fc2416f8236fb362afb0f4ffda122b12d46afafc2e7f7d41ec5bc33b267ae299abcb1312ebb9496cc31a50fae85cff2cc30774
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
locomotivecms_coal (1.0.0.pre.alpha.
|
4
|
+
locomotivecms_coal (1.0.0.pre.alpha.2)
|
5
5
|
activesupport (~> 4.2.1)
|
6
|
-
|
6
|
+
faraday (~> 0.9.1)
|
7
|
+
faraday_middleware (~> 0.9.1)
|
7
8
|
|
8
9
|
GEM
|
9
10
|
remote: https://rubygems.org/
|
@@ -31,15 +32,25 @@ GEM
|
|
31
32
|
safe_yaml (~> 1.0.0)
|
32
33
|
diff-lcs (1.2.5)
|
33
34
|
docile (1.1.5)
|
35
|
+
domain_name (0.5.23)
|
36
|
+
unf (>= 0.0.5, < 1.0.0)
|
37
|
+
faraday (0.9.1)
|
38
|
+
multipart-post (>= 1.2, < 3)
|
39
|
+
faraday_middleware (0.9.1)
|
40
|
+
faraday (>= 0.7.4, < 0.10)
|
41
|
+
http-cookie (1.0.2)
|
42
|
+
domain_name (~> 0.5)
|
34
43
|
i18n (0.7.0)
|
35
44
|
json (1.8.2)
|
36
45
|
json_spec (1.1.4)
|
37
46
|
multi_json (~> 1.0)
|
38
47
|
rspec (>= 2.0, < 4.0)
|
39
48
|
method_source (0.8.2)
|
40
|
-
mime-types (
|
41
|
-
minitest (5.
|
49
|
+
mime-types (2.4.3)
|
50
|
+
minitest (5.6.1)
|
42
51
|
multi_json (1.11.0)
|
52
|
+
multipart-post (2.0.0)
|
53
|
+
netrc (0.10.3)
|
43
54
|
pry (0.10.1)
|
44
55
|
coderay (~> 1.1.0)
|
45
56
|
method_source (~> 0.8.1)
|
@@ -48,10 +59,10 @@ GEM
|
|
48
59
|
byebug (~> 4.0)
|
49
60
|
pry (~> 0.10)
|
50
61
|
rake (10.4.2)
|
51
|
-
|
52
|
-
|
53
|
-
mime-types (
|
54
|
-
|
62
|
+
rest-client (1.8.0)
|
63
|
+
http-cookie (>= 1.0.2, < 2.0)
|
64
|
+
mime-types (>= 1.16, < 3.0)
|
65
|
+
netrc (~> 0.7)
|
55
66
|
rspec (3.2.0)
|
56
67
|
rspec-core (~> 3.2.0)
|
57
68
|
rspec-expectations (~> 3.2.0)
|
@@ -79,10 +90,9 @@ GEM
|
|
79
90
|
tins (1.3.5)
|
80
91
|
tzinfo (1.2.2)
|
81
92
|
thread_safe (~> 0.1)
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
rest-client (~> 1.6.7)
|
93
|
+
unf (0.1.4)
|
94
|
+
unf_ext
|
95
|
+
unf_ext (0.0.6)
|
86
96
|
vcr (2.9.3)
|
87
97
|
webmock (1.9.3)
|
88
98
|
addressable (>= 2.2.7)
|
@@ -99,6 +109,7 @@ DEPENDENCIES
|
|
99
109
|
locomotivecms_coal!
|
100
110
|
pry-byebug (~> 3.1.0)
|
101
111
|
rake (~> 10.4.2)
|
112
|
+
rest-client (~> 1.8.0)
|
102
113
|
rspec (~> 3.2.0)
|
103
114
|
vcr
|
104
115
|
webmock
|
data/README.md
CHANGED
@@ -2,9 +2,29 @@
|
|
2
2
|
|
3
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
4
|
|
5
|
-
The Ruby API Client for LocomotiveCMS.
|
6
|
-
|
7
|
-
##
|
5
|
+
The Ruby API Client for LocomotiveCMS V3 (WIP).
|
6
|
+
|
7
|
+
## Table of Contents
|
8
|
+
|
9
|
+
- [Installation](#installation)
|
10
|
+
- [Usage](#usage)
|
11
|
+
- [Authentication](#authentication)
|
12
|
+
- [Resources](#resources)
|
13
|
+
- [MyAccount](#my-account)
|
14
|
+
- [Sites](#sites)
|
15
|
+
- [Pages](#pages)
|
16
|
+
- [Content Types](#content-types)
|
17
|
+
- [Content Entries](#content-entries)
|
18
|
+
- [Snippets](#snippets)
|
19
|
+
- [Theme Assets](#theme-assets)
|
20
|
+
- [Content Assets](#content-assets)
|
21
|
+
- [What is missing](#todo)
|
22
|
+
- [Contributing](#contributing)
|
23
|
+
- [How to write specs](#how-to-write-specs)
|
24
|
+
- [Credits](#credits)
|
25
|
+
- [License](#license)
|
26
|
+
|
27
|
+
## Installation
|
8
28
|
|
9
29
|
gem install locomotivecms_coal --pre
|
10
30
|
|
@@ -12,7 +32,13 @@ The Ruby API Client for LocomotiveCMS.
|
|
12
32
|
|
13
33
|
### Authentication
|
14
34
|
|
15
|
-
client = Locomotive::Coal::Client.new('http://www.myengine.dev
|
35
|
+
client = Locomotive::Coal::Client.new('http://www.myengine.dev', { email: <EMAIL>, api_key: <API KEY> })
|
36
|
+
|
37
|
+
You can get the version of the remote running Engine by calling the following method:
|
38
|
+
|
39
|
+
client.engine_version
|
40
|
+
|
41
|
+
*Note:* Coal only supports Engine version **~> 3.0.0**.
|
16
42
|
|
17
43
|
### Resources
|
18
44
|
|
@@ -23,6 +49,15 @@ The Ruby API Client for LocomotiveCMS.
|
|
23
49
|
my_account = client.my_account.get
|
24
50
|
puts my_account.name + " / " + my_account.email
|
25
51
|
|
52
|
+
**If not logged in, then it's possible to create your account**
|
53
|
+
|
54
|
+
client = Locomotive::Coal::Client.new('http://www.myengine.dev')
|
55
|
+
client.my_account.create(name: 'John Doe', email: 'john@doe.net', password: 'easyone', password_confirmation: 'easyone')
|
56
|
+
|
57
|
+
**Update my account**
|
58
|
+
|
59
|
+
client.my_account.update(name: 'Jane Doe')
|
60
|
+
|
26
61
|
#### Sites
|
27
62
|
|
28
63
|
**Get all my sites**
|
@@ -38,6 +73,35 @@ The Ruby API Client for LocomotiveCMS.
|
|
38
73
|
|
39
74
|
my_site = client.sites.destroy(my_site._id)
|
40
75
|
|
76
|
+
#### Pages
|
77
|
+
|
78
|
+
*Note:* We first need to log in to the site. It can be done by calling the scope_by method of the client instance.
|
79
|
+
|
80
|
+
site = client.sites.by_subdomain('acme')
|
81
|
+
site_client = client.scope_by(site)
|
82
|
+
|
83
|
+
**Get the list of pages in English**
|
84
|
+
|
85
|
+
pages = site_client.pages.all(:en)
|
86
|
+
|
87
|
+
**Get only the id/fullpath attributes**
|
88
|
+
|
89
|
+
pages = site_client.pages.fullpaths(:en)
|
90
|
+
|
91
|
+
It is required when we need the id of an existing page according to its fullpath.
|
92
|
+
|
93
|
+
**Create a page**
|
94
|
+
|
95
|
+
page = site_client.pages.create(title: 'About us', slug: 'about-us', parent: 'index', template: 'Locomotive rocks!')
|
96
|
+
|
97
|
+
**Update a page in French**
|
98
|
+
|
99
|
+
site_client.pages.update(page._id, { template: 'Locomotive est trop fort!!!' }, :fr)
|
100
|
+
|
101
|
+
**Destroy a page**
|
102
|
+
|
103
|
+
site_client.pages.destroy(page._id)
|
104
|
+
|
41
105
|
#### Content Types
|
42
106
|
|
43
107
|
**Get a content type by its slug**
|
@@ -51,7 +115,7 @@ The Ruby API Client for LocomotiveCMS.
|
|
51
115
|
|
52
116
|
#### Content Entries
|
53
117
|
|
54
|
-
**Get
|
118
|
+
**Get the first 10 entries filtered by a property (published)**
|
55
119
|
|
56
120
|
articles = site_client.contents.articles.all({ published: true })
|
57
121
|
|
@@ -64,20 +128,95 @@ The Ruby API Client for LocomotiveCMS.
|
|
64
128
|
page = articles._next_page
|
65
129
|
end
|
66
130
|
|
131
|
+
*Note:* You can also use the following syntax
|
132
|
+
|
133
|
+
content_type = site_client.contents.projects
|
134
|
+
articles = site_client.content_entries(content_type).all
|
135
|
+
|
136
|
+
**Create a content entry**
|
137
|
+
|
138
|
+
first_article = site_client.contents.articles.create(title: 'Hello world')
|
139
|
+
|
67
140
|
**Update a content entry**
|
68
141
|
|
69
142
|
article = site_client.contents.articles.all.first
|
70
143
|
site_client.contents.articles.update(article._id, { title: 'Hello world'})
|
71
144
|
|
72
|
-
|
145
|
+
**Update a content entry in a different locale**
|
73
146
|
|
74
|
-
|
147
|
+
# create the article in the default locale
|
148
|
+
article = site_client.contents.articles.create(title: 'Hello world')
|
75
149
|
|
76
|
-
|
150
|
+
# then update the title in another locale
|
151
|
+
site_client.contents.articles.update(article._id, { title: 'Bonjour le monde'}, :fr)
|
77
152
|
|
78
|
-
|
153
|
+
**Destroy a content entry**
|
79
154
|
|
80
|
-
|
155
|
+
site_client.contents.articles.destroy(article._id)
|
156
|
+
|
157
|
+
#### Snippets
|
158
|
+
|
159
|
+
**Get the list of snippets**
|
160
|
+
|
161
|
+
snippets = site_client.snippets.all
|
162
|
+
|
163
|
+
**Create a snippet**
|
164
|
+
|
165
|
+
snippet = site_client.snippets.create(name: 'Header', slug: 'header', template: 'Locomotive rocks!')
|
166
|
+
|
167
|
+
# create a snippet in different locales
|
168
|
+
|
169
|
+
snippet = site_client.snippets.create(name: 'Header', slug: 'header', template: { en: 'Locomotive rocks!', fr: 'Locomotive déchire !' })
|
170
|
+
|
171
|
+
**Update a snippet**
|
172
|
+
|
173
|
+
site_client.snippets.update('header', template: 'Locomotive rocks!!!')
|
174
|
+
|
175
|
+
**Destroy a snippet**
|
176
|
+
|
177
|
+
site_client.snippets.destroy('header')
|
178
|
+
|
179
|
+
#### Theme assets
|
180
|
+
|
181
|
+
**Get the list of theme assets**
|
182
|
+
|
183
|
+
assets = site_client.theme_assets.all
|
184
|
+
|
185
|
+
**Create a theme asset**
|
186
|
+
|
187
|
+
asset = site_client.theme_assets.create(source: Locomotive::Coal::UploadIO.new('<local path of my image>'), folder: 'images/header')
|
188
|
+
|
189
|
+
**Update a theme asset**
|
190
|
+
|
191
|
+
site_client.theme_assets.update(asset._id, source: Locomotive::Coal::UploadIO.new('<local path of my new image>'))
|
192
|
+
|
193
|
+
**Destroy a theme asset**
|
194
|
+
|
195
|
+
site_client.theme_assets.destroy(asset._id)
|
196
|
+
|
197
|
+
#### Content assets
|
198
|
+
|
199
|
+
**Get the list of content assets**
|
200
|
+
|
201
|
+
assets = site_client.content_assets.all
|
202
|
+
|
203
|
+
**Create a content asset**
|
204
|
+
|
205
|
+
asset = site_client.content_assets.create(source: Locomotive::Coal::UploadIO.new('<local path of my image>'))
|
206
|
+
|
207
|
+
**Update a content asset**
|
208
|
+
|
209
|
+
site_client.content_assets.update(asset._id, source: Locomotive::Coal::UploadIO.new('<local path of my new image>'))
|
210
|
+
|
211
|
+
**Destroy a content asset**
|
212
|
+
|
213
|
+
site_client.content_assets.destroy(asset._id)
|
214
|
+
|
215
|
+
## What is missing?
|
216
|
+
|
217
|
+
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.
|
218
|
+
|
219
|
+
Check the issues section of the repository to see what is missing.
|
81
220
|
|
82
221
|
## Contributing
|
83
222
|
|
@@ -87,6 +226,24 @@ Check the issues section of the repository to see what is missing.
|
|
87
226
|
4. Push to the branch (`git push origin my-new-feature`)
|
88
227
|
5. Create new Pull Request
|
89
228
|
|
229
|
+
## How to write specs
|
230
|
+
|
231
|
+
**Engine configuration:**
|
232
|
+
|
233
|
+
1. You need to run the last version (master branch) of the [Locomotive engine](https://github.com/locomotivecms/engine).
|
234
|
+
2. Pick a different database name to run your Coal specs against.
|
235
|
+
3. Run ````bundle exec rake development:bootstrap````
|
236
|
+
4. Run the Locomotive server ````bundle exec rails server````
|
237
|
+
|
238
|
+
**System configuration:**
|
239
|
+
|
240
|
+
- edit your /etc/hosts file (*NIX systems) and add 2 lines
|
241
|
+
127.0.0.0 www.example.com acme.example.com
|
242
|
+
|
243
|
+
## Credits
|
244
|
+
|
245
|
+
[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.
|
246
|
+
|
90
247
|
## License
|
91
248
|
|
92
249
|
Copyright (c) 2015 NoCoffee. MIT Licensed, see LICENSE for details.
|
@@ -2,15 +2,14 @@ module Locomotive::Coal
|
|
2
2
|
|
3
3
|
class Client
|
4
4
|
|
5
|
-
attr_reader :uri, :credentials
|
5
|
+
attr_reader :uri, :credentials, :options
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
def initialize(uri, credentials)
|
7
|
+
def initialize(uri, credentials, options = {})
|
10
8
|
if uri.blank? || credentials.blank?
|
11
9
|
raise MissingURIOrCredentialsError.new('URI and/or credentials are missing')
|
12
10
|
else
|
13
|
-
@
|
11
|
+
@options = { path_prefix: 'locomotive' }.merge(options).with_indifferent_access
|
12
|
+
@uri, @credentials = prepare_uri(uri), credentials.with_indifferent_access
|
14
13
|
end
|
15
14
|
end
|
16
15
|
|
@@ -19,33 +18,84 @@ module Locomotive::Coal
|
|
19
18
|
end
|
20
19
|
|
21
20
|
def my_account
|
22
|
-
@my_account ||= Resources::MyAccount.new(uri,
|
21
|
+
@my_account ||= Resources::MyAccount.new(uri, connection)
|
23
22
|
end
|
24
23
|
|
25
24
|
def sites
|
26
|
-
@sites ||= Resources::Sites.new(uri,
|
25
|
+
@sites ||= Resources::Sites.new(uri, connection)
|
26
|
+
end
|
27
|
+
|
28
|
+
def pages
|
29
|
+
@pages ||= Resources::Pages.new(uri, connection)
|
30
|
+
end
|
31
|
+
|
32
|
+
def content_types
|
33
|
+
@content_types ||= Resources::ContentTypes.new(uri, connection)
|
34
|
+
end
|
35
|
+
|
36
|
+
alias :contents :content_types
|
37
|
+
|
38
|
+
def content_entries(content_type)
|
39
|
+
@content_entries ||= {}
|
40
|
+
@content_entries[content_type.slug] ||= Resources::ContentEntries.new(uri, connection, content_type)
|
41
|
+
end
|
42
|
+
|
43
|
+
def content_assets
|
44
|
+
@content_assets ||= Resources::ContentAssets.new(uri, connection)
|
45
|
+
end
|
46
|
+
|
47
|
+
def theme_assets
|
48
|
+
@theme_assets ||= Resources::ThemeAssets.new(uri, connection)
|
49
|
+
end
|
50
|
+
|
51
|
+
def snippets
|
52
|
+
@snippets ||= Resources::Snippets.new(uri, connection)
|
53
|
+
end
|
54
|
+
|
55
|
+
def translations
|
56
|
+
@translations ||= Resources::Translations.new(uri, connection)
|
27
57
|
end
|
28
58
|
|
29
|
-
def
|
30
|
-
@
|
59
|
+
def engine_version
|
60
|
+
@engine_version ||= Resources::EngineVersion.new(uri, connection).version
|
31
61
|
end
|
32
62
|
|
63
|
+
alias version engine_version
|
64
|
+
|
33
65
|
def scope_by(site)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
66
|
+
options[:handle] = site.handle
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
def reset
|
71
|
+
@token = @my_account = @sites = @pages = @content_types = @theme_assets = @content_assets = @translations = nil
|
72
|
+
end
|
73
|
+
|
74
|
+
def ssl?
|
75
|
+
!!self.options[:ssl]
|
42
76
|
end
|
43
77
|
|
44
78
|
private
|
45
79
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
80
|
+
def connection
|
81
|
+
_token = credentials[:token] || -> { token }
|
82
|
+
credentials.merge(token: _token, handle: options[:handle])
|
83
|
+
end
|
84
|
+
|
85
|
+
def uri_path
|
86
|
+
[self.options[:path_prefix], 'api', 'v3'].join('/')
|
87
|
+
end
|
88
|
+
|
89
|
+
def prepare_uri(uri)
|
90
|
+
uri = "http://#{uri.to_s}" unless uri.to_s =~ /^https?:\/\//
|
91
|
+
|
92
|
+
URI(uri).tap do |uri|
|
93
|
+
uri.scheme = 'https' if ssl?
|
94
|
+
|
95
|
+
if uri.path == '/' || uri.path.blank?
|
96
|
+
uri.merge!(uri_path)
|
97
|
+
end
|
98
|
+
end
|
49
99
|
end
|
50
100
|
|
51
101
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Locomotive::Coal
|
2
|
+
|
3
|
+
class ClientV2 < Client
|
4
|
+
|
5
|
+
attr_accessor :scoped_by_site
|
6
|
+
|
7
|
+
def scope_by(site)
|
8
|
+
if site.domains.include?(domain)
|
9
|
+
self
|
10
|
+
else
|
11
|
+
new_uri = site.domains.first
|
12
|
+
|
13
|
+
self.class.new(new_uri, credentials, options).tap do |_client|
|
14
|
+
_client.scoped_by_site = true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def uri_path(handle = nil)
|
22
|
+
"#{self.options[:path_prefix]}/api"
|
23
|
+
end
|
24
|
+
|
25
|
+
def domain
|
26
|
+
self.uri.host
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -5,23 +5,64 @@ module Locomotive::Coal
|
|
5
5
|
#
|
6
6
|
class Error < StandardError
|
7
7
|
|
8
|
+
attr_reader :response
|
9
|
+
|
10
|
+
def initialize(response = nil)
|
11
|
+
@response = response
|
12
|
+
super(build_message)
|
13
|
+
end
|
14
|
+
|
15
|
+
def body
|
16
|
+
@response.body || {}
|
17
|
+
end
|
18
|
+
|
8
19
|
def self.from_response(response)
|
9
|
-
status = response.
|
10
|
-
if klass = case status
|
20
|
+
status = response.status
|
21
|
+
if klass = case status.to_i
|
11
22
|
when 401 then Locomotive::Coal::UnauthorizedError
|
12
23
|
when 404 then Locomotive::Coal::UnknownResourceError
|
24
|
+
when 422 then Locomotive::Coal::InvalidResourceError
|
13
25
|
when 429 then Locomotive::Coal::TooManyRequestsError
|
14
|
-
when
|
26
|
+
when 500 then Locomotive::Coal::ServerSideError
|
27
|
+
else Error
|
15
28
|
end
|
16
29
|
klass.new(response)
|
17
30
|
end
|
18
31
|
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def build_message
|
36
|
+
@response ? body['error'] : nil
|
37
|
+
end
|
38
|
+
|
19
39
|
end
|
20
40
|
|
21
|
-
class MissingURIOrCredentialsError <
|
41
|
+
class MissingURIOrCredentialsError < StandardError; end
|
42
|
+
class BadRequestError < StandardError; end
|
22
43
|
class UnknownResourceError < Error; end
|
23
44
|
class TooManyRequestsError < Error; end
|
24
45
|
class UnauthorizedError < Error; end
|
25
|
-
class
|
46
|
+
class ServerSideError < Error; end
|
47
|
+
class TimeoutError < Error; end
|
48
|
+
|
49
|
+
class InvalidResourceError < Error
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def build_message
|
54
|
+
attributes = body['attributes'].map do |name, errors|
|
55
|
+
if errors.is_a?(Hash)
|
56
|
+
errors.map do |k, _errors|
|
57
|
+
"#{name}.#{k}: #{_errors.join(' + ')}"
|
58
|
+
end
|
59
|
+
else
|
60
|
+
"#{name} #{errors.join(' ')}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
"#{body['error']}: #{attributes.join(', ')}"
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
26
67
|
|
27
68
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative 'concerns/request'
|
2
|
+
|
3
|
+
module Locomotive::Coal
|
4
|
+
module Resources
|
5
|
+
|
6
|
+
class Base
|
7
|
+
|
8
|
+
include Concerns::Request
|
9
|
+
|
10
|
+
attr_accessor :uri, :credentials
|
11
|
+
|
12
|
+
def initialize(uri, credentials)
|
13
|
+
@uri, @credentials = uri, credentials
|
14
|
+
end
|
15
|
+
|
16
|
+
def index
|
17
|
+
get(resources_name).map do |attributes|
|
18
|
+
Resource.new(attributes)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
alias :all :index
|
23
|
+
|
24
|
+
def create(attributes = {})
|
25
|
+
data = post(resources_name, { resource_name => attributes })
|
26
|
+
Resource.new(data)
|
27
|
+
end
|
28
|
+
|
29
|
+
def update(id, attributes = {})
|
30
|
+
data = put("#{resources_name}/#{id}", { resource_name => attributes })
|
31
|
+
Resource.new(data)
|
32
|
+
end
|
33
|
+
|
34
|
+
def update_with_locale(id, attributes = {}, locale = nil)
|
35
|
+
data = put("#{resources_name}/#{id}", { _locale: locale, resource_name => attributes })
|
36
|
+
Resource.new(data)
|
37
|
+
end
|
38
|
+
|
39
|
+
def destroy(id)
|
40
|
+
data = delete("#{resources_name}/#{id}")
|
41
|
+
Resource.new(data)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def resources_name
|
47
|
+
resource_name.pluralize
|
48
|
+
end
|
49
|
+
|
50
|
+
def resource_name
|
51
|
+
self.class.name.demodulize.underscore.singularize
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|