jahuty 1.1.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.circleci/config.yml +33 -0
- data/.rubocop.yml +12 -0
- data/CHANGELOG.md +25 -0
- data/Gemfile +3 -1
- data/README.md +30 -50
- data/Rakefile +11 -3
- data/bin/console +4 -3
- data/bin/setup +2 -0
- data/jahuty.gemspec +32 -16
- data/lib/jahuty.rb +19 -15
- data/lib/jahuty/action/base.rb +15 -0
- data/lib/jahuty/action/show.rb +16 -0
- data/lib/jahuty/api/client.rb +37 -0
- data/lib/jahuty/client.rb +42 -0
- data/lib/jahuty/exception/{not_ok.rb → error.rb} +6 -1
- data/lib/jahuty/request/base.rb +16 -0
- data/lib/jahuty/request/factory.rb +17 -0
- data/lib/jahuty/resource/factory.rb +50 -0
- data/lib/jahuty/resource/problem.rb +17 -0
- data/lib/jahuty/resource/render.rb +20 -0
- data/lib/jahuty/service/base.rb +12 -0
- data/lib/jahuty/service/factory.rb +46 -0
- data/lib/jahuty/service/snippet.rb +20 -0
- data/lib/jahuty/version.rb +3 -1
- metadata +126 -15
- data/lib/jahuty/data/problem.rb +0 -21
- data/lib/jahuty/data/snippet.rb +0 -23
- data/lib/jahuty/service/connect.rb +0 -23
- data/lib/jahuty/service/get.rb +0 -23
- data/lib/jahuty/snippet.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f16f42cd41d86fdcdbd27591e637d82c324828f5cf60b3388afd7f1d439f9bee
|
4
|
+
data.tar.gz: 53ef3b027c56ac0a00a09ab8ba0a10a3794b0d47bab1975ceb64dfbce74a244c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24affb2a47535cd18148a52a51ed5f67120032ff859f54995433c10f1348cc72db3d17017b58e3e90a473678c7639acecc14832fe6b6cea302342baa46bc8821
|
7
|
+
data.tar.gz: f4429c45c905b4dd9336d1126d4256dc9bdbf0f0c548f52bc7c93a8dc7371882bafdb11b29e18644816b1378a1d4691c7c7c6796c5a1c9a7cd68a6b90ab445a6
|
@@ -0,0 +1,33 @@
|
|
1
|
+
version: 2.1
|
2
|
+
orbs:
|
3
|
+
ruby: circleci/ruby@0.1.2
|
4
|
+
codecov: codecov/codecov@1.1.3
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
docker:
|
8
|
+
- image: circleci/ruby:2.6.3-stretch-node
|
9
|
+
executor: ruby/default
|
10
|
+
steps:
|
11
|
+
- checkout
|
12
|
+
- run:
|
13
|
+
name: Update bundler
|
14
|
+
command: gem install bundler
|
15
|
+
- run:
|
16
|
+
name: Which bundler?
|
17
|
+
command: bundle -v
|
18
|
+
- ruby/bundle-install
|
19
|
+
- run:
|
20
|
+
name: Run rubocop
|
21
|
+
command: bundle exec rake rubocop
|
22
|
+
- run:
|
23
|
+
name: Run specs
|
24
|
+
command: |
|
25
|
+
bundle exec rspec --profile 10 \
|
26
|
+
--format RspecJunitFormatter \
|
27
|
+
--out test_results/rspec.xml \
|
28
|
+
--format progress \
|
29
|
+
$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
|
30
|
+
- store_test_results:
|
31
|
+
path: test_results
|
32
|
+
- codecov/upload:
|
33
|
+
file: ./coverage/coverage.xml
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,31 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## 3.0.0 - 2020-12-30
|
9
|
+
|
10
|
+
- Change from a static-based architecture (e.g., `Jahuty::Snippet.render(1)`) to an instance-based one (e.g., `jahuty.snippets.render(1)`) to make the library easier to develop, test, and use.
|
11
|
+
- Change `README` and fix broken links from `www.jahuty.com/docs` to `docs.jahuty.com`.
|
12
|
+
- Add [CircleCI](https://circleci.com/gh/jahuty/jahuty-ruby) continuous integration.
|
13
|
+
- Add [Rubocop](https://github.com/rubocop-hq/rubocop) code analyzer and formatter, based on the community style guide.
|
14
|
+
- Add code coverage analysis with [Simplecov](https://github.com/simplecov-ruby/simplecov) and [Codecov.io](https://codecov.io/gh/jahuty/jahuty-ruby).
|
15
|
+
|
16
|
+
## 2.0.0 - 2020-07-04
|
17
|
+
|
18
|
+
- Change `Service::Get` to `Service::Render`.
|
19
|
+
- Change `Snippet.get` to `Snippet.render`.
|
20
|
+
- Change optional second argument of `Snippet.render` from `params` hash to options hash with `params` key.
|
21
|
+
- Change `Data::Snippet` to `Data::Render`.
|
22
|
+
- Remove `id` attribute from `Data::Render`.
|
23
|
+
- Change API endpoint to `snippets/:id/render`.
|
24
|
+
|
25
|
+
## 1.1.2 - 2020-07-04
|
26
|
+
|
27
|
+
- Change the Faraday gem from `~> 0.1` to `~> 1.0`.
|
28
|
+
|
29
|
+
## 1.1.1 - 2020-03-15
|
30
|
+
|
31
|
+
- Change snippet parameters to JSON query string parameter (e.g., `params={"foo":"bar"}`) from serialized query string parameter (e.g.,`params[foo]=bar`).
|
32
|
+
|
8
33
|
## 1.1.0 - 2020-03-14
|
9
34
|
|
10
35
|
- Add snippet parameters.
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,59 +1,50 @@
|
|
1
|
+
[](https://circleci.com/gh/jahuty/jahuty-ruby) [](https://codecov.io/gh/jahuty/jahuty-ruby) [](https://github.com/rubocop-hq/rubocop)
|
2
|
+
|
1
3
|
# jahuty-ruby
|
2
|
-
|
4
|
+
|
5
|
+
Welcome to the [Ruby SDK](https://docs.jahuty.com/sdks/ruby) for [Jahuty's API](https://docs.jahuty.com/api)!
|
3
6
|
|
4
7
|
## Installation
|
5
8
|
|
6
|
-
This library requires [Ruby 2.
|
9
|
+
This library requires [Ruby 2.6+](https://www.ruby-lang.org/en/downloads/releases/).
|
7
10
|
|
8
11
|
It is multi-platform, and we strive to make it run equally well on Windows, Linux, and OSX.
|
9
12
|
|
10
|
-
Add this line to your application's `Gemfile
|
13
|
+
Add this line to your application's `Gemfile`:
|
11
14
|
|
12
15
|
```ruby
|
13
|
-
gem
|
14
|
-
```
|
15
|
-
|
16
|
-
And then execute:
|
17
|
-
|
18
|
-
```bash
|
19
|
-
$ bundle
|
16
|
+
gem 'jahuty', '~> 3.0'
|
20
17
|
```
|
21
18
|
|
22
19
|
## Usage
|
23
20
|
|
24
|
-
|
21
|
+
Instantiate the client with your [API key](https://docs.jahuty.com/api#authentication) and use `snippets.render()` to render your snippet:
|
25
22
|
|
26
23
|
```ruby
|
27
|
-
|
24
|
+
jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
|
28
25
|
|
29
|
-
|
26
|
+
puts jahuty.snippets.render(YOUR_SNIPPET_ID)
|
30
27
|
```
|
31
28
|
|
32
|
-
|
33
|
-
|
34
|
-
Then, use the `.get` method to fetch a snippet:
|
29
|
+
You can also access the render's content with `to_s` or `content`:
|
35
30
|
|
36
31
|
```ruby
|
37
|
-
|
32
|
+
jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
|
33
|
+
|
34
|
+
render = jahuty.snippets.render(YOUR_SNIPPET_ID)
|
38
35
|
|
39
|
-
|
40
|
-
snippet = Snippet.get YOUR_SNIPPET_ID
|
36
|
+
a = render.to_s
|
41
37
|
|
42
|
-
|
43
|
-
snippet.to_s
|
38
|
+
b = render.content
|
44
39
|
|
45
|
-
|
46
|
-
snippet.id
|
47
|
-
snippet.content
|
40
|
+
a == b # returns true
|
48
41
|
```
|
49
42
|
|
50
43
|
In an HTML view:
|
51
44
|
|
52
45
|
```html+erb
|
53
46
|
<%-
|
54
|
-
|
55
|
-
|
56
|
-
Jahuty.key = "YOUR_API_KEY"
|
47
|
+
jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
|
57
48
|
%>
|
58
49
|
<!doctype html>
|
59
50
|
<html>
|
@@ -61,49 +52,38 @@ Jahuty.key = "YOUR_API_KEY"
|
|
61
52
|
<title>Awesome example</title>
|
62
53
|
</head>
|
63
54
|
<body>
|
64
|
-
<%=
|
55
|
+
<%= jahuty.snippets.render YOUR_SNIPPET_ID %>
|
65
56
|
</body>
|
66
57
|
```
|
67
58
|
|
68
59
|
## Parameters
|
69
60
|
|
70
|
-
You can [pass parameters](https://
|
61
|
+
You can [pass parameters](https://docs.jahuty.com/liquid/parameters) into your snippet using the `params` option:
|
71
62
|
|
72
63
|
```ruby
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
foo: "bar",
|
77
|
-
baz: ["qux", "quux"],
|
78
|
-
corge: {
|
79
|
-
grault: {
|
80
|
-
garply: "waldo"
|
81
|
-
}
|
82
|
-
}
|
83
|
-
});
|
64
|
+
jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
|
65
|
+
|
66
|
+
jahuty.snippets.render(YOUR_SNIPPET_ID, params: { foo: 'bar' });
|
84
67
|
```
|
85
68
|
|
86
|
-
The parameters above would be equivalent to [assigning the
|
69
|
+
The parameters above would be equivalent to [assigning the variable](https://docs.jahuty.com/liquid/variables) below in your snippet:
|
87
70
|
|
88
71
|
```html
|
89
72
|
{% assign foo = "bar" %}
|
90
|
-
{% assign baz = ["qux", "quux"] %}
|
91
|
-
{% assign corge.grault.garply = "waldo" %}
|
92
73
|
```
|
93
74
|
|
94
75
|
## Errors
|
95
76
|
|
96
|
-
If
|
77
|
+
If an error occurs with [Jahuty's API](https://docs.jahuty.com/api#errors), a `Jahuty::Exception::Error` will be raised:
|
97
78
|
|
98
79
|
```ruby
|
99
|
-
require
|
80
|
+
require 'jahuty'
|
100
81
|
|
101
82
|
begin
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
# hmm, the API returned something besides 2xx status code
|
83
|
+
jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
|
84
|
+
jahuty.snippets.render YOUR_SNIPPET_ID
|
85
|
+
rescue Jahuty::Exception::Error => e
|
86
|
+
# The API returned an error. See the error's problem for details.
|
107
87
|
puts e.problem.type # a URL to more information
|
108
88
|
puts e.problem.status # the status code
|
109
89
|
puts e.problem.detail # a description of the error
|
data/Rakefile
CHANGED
@@ -1,6 +1,14 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'rubocop/rake_task'
|
3
6
|
|
4
7
|
RSpec::Core::RakeTask.new(:spec)
|
5
8
|
|
6
|
-
task :
|
9
|
+
task default: :spec
|
10
|
+
|
11
|
+
RuboCop::RakeTask.new do |task|
|
12
|
+
task.requires << 'rubocop-performance'
|
13
|
+
task.requires << 'rubocop-rspec'
|
14
|
+
end
|
data/bin/console
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'jahuty/snippets'
|
5
6
|
|
6
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +11,5 @@ require "jahuty/snippets"
|
|
10
11
|
# require "pry"
|
11
12
|
# Pry.start
|
12
13
|
|
13
|
-
require
|
14
|
+
require 'irb'
|
14
15
|
IRB.start(__FILE__)
|
data/bin/setup
CHANGED
data/jahuty.gemspec
CHANGED
@@ -1,30 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
|
-
lib = File.expand_path(
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
|
5
|
-
require
|
6
|
+
require 'jahuty/version'
|
6
7
|
|
7
8
|
Gem::Specification.new do |spec|
|
8
|
-
spec.name =
|
9
|
+
spec.name = 'jahuty'
|
9
10
|
spec.version = Jahuty::VERSION
|
10
|
-
spec.authors = [
|
11
|
-
spec.email = [
|
11
|
+
spec.authors = ['Jack Clayton']
|
12
|
+
spec.email = ['jack@jahuty.com']
|
12
13
|
|
13
|
-
spec.summary =
|
14
|
-
spec.description =
|
15
|
-
spec.homepage =
|
16
|
-
spec.license =
|
14
|
+
spec.summary = 'Jahuty\'s Ruby SDK.'
|
15
|
+
spec.description = 'Turn any page into a content-managed page.'
|
16
|
+
spec.homepage = 'https://www.jahuty.com'
|
17
|
+
spec.license = 'MIT'
|
17
18
|
|
18
|
-
spec.
|
19
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
20
|
+
|
21
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
22
|
+
spec.metadata['source_code_uri'] = 'https://github.com/jahuty/jahuty-ruby'
|
23
|
+
spec.metadata['changelog_uri'] = 'https://github.com/jahuty/jahuty-ruby/blob/master/CHANGELOG.md'
|
24
|
+
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
19
26
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
27
|
end
|
21
|
-
spec.bindir =
|
28
|
+
spec.bindir = 'exe'
|
22
29
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
-
spec.require_paths = [
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
|
32
|
+
spec.required_ruby_version = '~> 2.6'
|
24
33
|
|
25
|
-
spec.add_dependency 'faraday', '~> 0
|
34
|
+
spec.add_dependency 'faraday', '~> 1.0'
|
26
35
|
|
27
|
-
spec.add_development_dependency
|
28
|
-
spec.add_development_dependency
|
29
|
-
spec.add_development_dependency
|
36
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
37
|
+
spec.add_development_dependency 'rake', '~> 12.3'
|
38
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
39
|
+
spec.add_development_dependency 'rspec_junit_formatter', '~>0.4'
|
40
|
+
spec.add_development_dependency 'rubocop', '~> 1.7'
|
41
|
+
spec.add_development_dependency 'rubocop-performance', '~> 1.9'
|
42
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 2.1'
|
43
|
+
spec.add_development_dependency 'simplecov', '~>0.20'
|
44
|
+
spec.add_development_dependency 'simplecov-cobertura', '~> 1.4'
|
45
|
+
spec.add_development_dependency 'webmock', '~> 3.11'
|
30
46
|
end
|
data/lib/jahuty.rb
CHANGED
@@ -1,23 +1,27 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'jahuty/version'
|
4
4
|
|
5
|
-
require
|
6
|
-
require
|
5
|
+
require 'jahuty/action/base'
|
6
|
+
require 'jahuty/action/show'
|
7
7
|
|
8
|
-
require
|
8
|
+
require 'jahuty/api/client'
|
9
9
|
|
10
|
-
require
|
11
|
-
require "jahuty/service/get"
|
10
|
+
require 'jahuty/exception/error'
|
12
11
|
|
13
|
-
|
14
|
-
|
12
|
+
require 'jahuty/request/base'
|
13
|
+
require 'jahuty/request/factory'
|
14
|
+
|
15
|
+
require 'jahuty/resource/problem'
|
16
|
+
require 'jahuty/resource/render'
|
17
|
+
require 'jahuty/resource/factory'
|
15
18
|
|
16
|
-
|
17
|
-
|
19
|
+
require 'jahuty/service/base'
|
20
|
+
require 'jahuty/service/snippet'
|
21
|
+
require 'jahuty/service/factory'
|
18
22
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
+
require 'jahuty/client'
|
24
|
+
|
25
|
+
module Jahuty
|
26
|
+
BASE_URI = 'https://api.jahuty.com'
|
23
27
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jahuty
|
4
|
+
module Action
|
5
|
+
# Provides common logic for service actions.
|
6
|
+
class Base
|
7
|
+
attr_accessor :resource, :params
|
8
|
+
|
9
|
+
def initialize(resource:, params: {})
|
10
|
+
@resource = resource
|
11
|
+
@params = params
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jahuty
|
4
|
+
module Action
|
5
|
+
# Displays a specific resource.
|
6
|
+
class Show < Base
|
7
|
+
attr_accessor :id
|
8
|
+
|
9
|
+
def initialize(resource:, id:, params: {})
|
10
|
+
@id = id
|
11
|
+
|
12
|
+
super(resource: resource, params: params)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module Jahuty
|
6
|
+
module Api
|
7
|
+
# Handles HTTP requests and responses.
|
8
|
+
class Client
|
9
|
+
HEADERS = {
|
10
|
+
'Accept' => 'application/json;q=0.9,*/*;q=0.8',
|
11
|
+
'Accept-Encoding' => 'gzip, deflate',
|
12
|
+
'Content-Type' => 'application/json; charset=utf-8',
|
13
|
+
'User-Agent' => "Jahuty Ruby SDK v#{::Jahuty::VERSION}"
|
14
|
+
}.freeze
|
15
|
+
|
16
|
+
def initialize(api_key:)
|
17
|
+
@api_key = api_key
|
18
|
+
end
|
19
|
+
|
20
|
+
def send(request)
|
21
|
+
@client ||= Faraday.new(url: ::Jahuty::BASE_URI, headers: headers)
|
22
|
+
|
23
|
+
@client.send(
|
24
|
+
request.method.to_sym,
|
25
|
+
request.path,
|
26
|
+
{ params: request.params }
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def headers
|
33
|
+
{ 'Authorization' => "Bearer #{@api_key}" }.merge(HEADERS)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jahuty
|
4
|
+
# Executes requests against Jahuty's API and returns resources.
|
5
|
+
class Client
|
6
|
+
def initialize(api_key:)
|
7
|
+
@api_key = api_key
|
8
|
+
@services = Service::Factory.new(client: self)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Allows services to appear as properties (e.g., jahuty.snippets).
|
12
|
+
def method_missing(name, *args, &block)
|
13
|
+
if args.empty? && @services.respond_to?(name)
|
14
|
+
@services.send(name)
|
15
|
+
else
|
16
|
+
super
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def request(action)
|
21
|
+
@requests ||= Request::Factory.new
|
22
|
+
|
23
|
+
request = @requests.call(action)
|
24
|
+
|
25
|
+
@client ||= Api::Client.new(api_key: @api_key)
|
26
|
+
|
27
|
+
response = @client.send(request)
|
28
|
+
|
29
|
+
@resources ||= Resource::Factory.new
|
30
|
+
|
31
|
+
resource = @resources.call(action, response)
|
32
|
+
|
33
|
+
raise Exception::Error.new(resource), 'API responded with a problem' if resource.is_a?(Resource::Problem)
|
34
|
+
|
35
|
+
resource
|
36
|
+
end
|
37
|
+
|
38
|
+
def respond_to_missing?(name, include_private = false)
|
39
|
+
@services.respond_to?(name, include_private) || super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -1,10 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Jahuty
|
2
4
|
module Exception
|
3
|
-
|
5
|
+
# Thrown when a client- or server-error occurs.
|
6
|
+
class Error < ::StandardError
|
4
7
|
attr_reader :problem
|
5
8
|
|
6
9
|
def initialize(problem)
|
7
10
|
@problem = problem
|
11
|
+
|
12
|
+
super
|
8
13
|
end
|
9
14
|
|
10
15
|
def message
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jahuty
|
4
|
+
module Request
|
5
|
+
# Provides common logic for all requests.
|
6
|
+
class Base
|
7
|
+
attr_accessor :method, :path, :params
|
8
|
+
|
9
|
+
def initialize(method:, path:, params: {})
|
10
|
+
@method = method
|
11
|
+
@path = path
|
12
|
+
@params = params
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jahuty
|
4
|
+
module Request
|
5
|
+
# Instantiates a request from an action. Currently, this is elementary. As
|
6
|
+
# we add actions, it will become more complicated.
|
7
|
+
class Factory
|
8
|
+
def call(action)
|
9
|
+
Base.new(
|
10
|
+
method: 'get',
|
11
|
+
path: "snippets/#{action.id}/render",
|
12
|
+
params: action.params
|
13
|
+
)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Jahuty
|
6
|
+
module Resource
|
7
|
+
# Negotiates the resource to return given the requested action and the
|
8
|
+
# server's response.
|
9
|
+
class Factory
|
10
|
+
CLASSES = {
|
11
|
+
problem: Problem.name,
|
12
|
+
render: Render.name
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
def call(action, response)
|
16
|
+
if success? response
|
17
|
+
resource_name = action.resource
|
18
|
+
elsif problem? response
|
19
|
+
resource_name = 'problem'
|
20
|
+
else
|
21
|
+
raise ArgumentError, 'Unexpected response'
|
22
|
+
end
|
23
|
+
|
24
|
+
resource_class = class_name(resource_name.to_sym)
|
25
|
+
|
26
|
+
payload = parse(response)
|
27
|
+
|
28
|
+
Object.const_get(resource_class).send(:new, **payload)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def class_name(resource_name)
|
34
|
+
CLASSES[resource_name.to_sym]
|
35
|
+
end
|
36
|
+
|
37
|
+
def problem?(response)
|
38
|
+
response.headers['Content-Type'] == 'application/problem+json'
|
39
|
+
end
|
40
|
+
|
41
|
+
def parse(response)
|
42
|
+
JSON.parse(response.body, symbolize_names: true)
|
43
|
+
end
|
44
|
+
|
45
|
+
def success?(response)
|
46
|
+
response.status.between?(200, 299)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jahuty
|
4
|
+
module Resource
|
5
|
+
# An application/problem+json response. The API should respond with a
|
6
|
+
# problem whenever a client- or server-error occurs.
|
7
|
+
class Problem
|
8
|
+
attr_accessor :status, :type, :detail
|
9
|
+
|
10
|
+
def initialize(status:, type:, detail:)
|
11
|
+
@status = status
|
12
|
+
@type = type
|
13
|
+
@detail = detail
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jahuty
|
4
|
+
module Resource
|
5
|
+
# A snippet's rendered content. Remember, renders are unique by the
|
6
|
+
# combination of id and params (i.e., the same id can produce different
|
7
|
+
# renders with different params).
|
8
|
+
class Render
|
9
|
+
attr_accessor :content
|
10
|
+
|
11
|
+
def initialize(content:)
|
12
|
+
@content = content
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
@content
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jahuty
|
4
|
+
module Service
|
5
|
+
# Instantiates the requested service and memoizes it for subsequent
|
6
|
+
# requests.
|
7
|
+
class Factory
|
8
|
+
CLASSES = {
|
9
|
+
snippets: Snippet.name
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
def initialize(client:)
|
13
|
+
@client = client
|
14
|
+
@services = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def method_missing(name, *args, &block)
|
18
|
+
if args.empty? && class_name?(name)
|
19
|
+
unless @services.key?(name)
|
20
|
+
klass = class_name(name)
|
21
|
+
service = Object.const_get(klass).send(:new, client: @client)
|
22
|
+
@services[name] = service
|
23
|
+
end
|
24
|
+
|
25
|
+
@services[name]
|
26
|
+
else
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def respond_to_missing?(name, include_private = false)
|
32
|
+
class_name(name) || super
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def class_name(service_name)
|
38
|
+
CLASSES[service_name]
|
39
|
+
end
|
40
|
+
|
41
|
+
def class_name?(service_name)
|
42
|
+
CLASSES.key?(service_name)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jahuty
|
4
|
+
module Service
|
5
|
+
# A service for interacting with snippets.
|
6
|
+
class Snippet < Base
|
7
|
+
def render(id, options = {})
|
8
|
+
params = { params: options[:params].to_json } unless options[:params].nil?
|
9
|
+
|
10
|
+
action = ::Jahuty::Action::Show.new(
|
11
|
+
id: id,
|
12
|
+
resource: 'render',
|
13
|
+
params: params || {}
|
14
|
+
)
|
15
|
+
|
16
|
+
@client.request(action)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/jahuty/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jahuty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jack Clayton
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0
|
19
|
+
version: '1.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0
|
26
|
+
version: '1.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +66,104 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec_junit_formatter
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.4'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.7'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.7'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-performance
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.9'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.9'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '2.1'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '2.1'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0.20'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0.20'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: simplecov-cobertura
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '1.4'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '1.4'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: webmock
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '3.11'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '3.11'
|
69
167
|
description: Turn any page into a content-managed page.
|
70
168
|
email:
|
71
169
|
- jack@jahuty.com
|
@@ -73,8 +171,10 @@ executables: []
|
|
73
171
|
extensions: []
|
74
172
|
extra_rdoc_files: []
|
75
173
|
files:
|
174
|
+
- ".circleci/config.yml"
|
76
175
|
- ".gitignore"
|
77
176
|
- ".rspec"
|
177
|
+
- ".rubocop.yml"
|
78
178
|
- CHANGELOG.md
|
79
179
|
- Gemfile
|
80
180
|
- LICENSE
|
@@ -84,33 +184,44 @@ files:
|
|
84
184
|
- bin/setup
|
85
185
|
- jahuty.gemspec
|
86
186
|
- lib/jahuty.rb
|
87
|
-
- lib/jahuty/
|
88
|
-
- lib/jahuty/
|
89
|
-
- lib/jahuty/
|
90
|
-
- lib/jahuty/
|
91
|
-
- lib/jahuty/
|
92
|
-
- lib/jahuty/
|
187
|
+
- lib/jahuty/action/base.rb
|
188
|
+
- lib/jahuty/action/show.rb
|
189
|
+
- lib/jahuty/api/client.rb
|
190
|
+
- lib/jahuty/client.rb
|
191
|
+
- lib/jahuty/exception/error.rb
|
192
|
+
- lib/jahuty/request/base.rb
|
193
|
+
- lib/jahuty/request/factory.rb
|
194
|
+
- lib/jahuty/resource/factory.rb
|
195
|
+
- lib/jahuty/resource/problem.rb
|
196
|
+
- lib/jahuty/resource/render.rb
|
197
|
+
- lib/jahuty/service/base.rb
|
198
|
+
- lib/jahuty/service/factory.rb
|
199
|
+
- lib/jahuty/service/snippet.rb
|
93
200
|
- lib/jahuty/version.rb
|
94
|
-
homepage: https://
|
201
|
+
homepage: https://www.jahuty.com
|
95
202
|
licenses:
|
96
203
|
- MIT
|
97
|
-
metadata:
|
204
|
+
metadata:
|
205
|
+
allowed_push_host: https://rubygems.org
|
206
|
+
homepage_uri: https://www.jahuty.com
|
207
|
+
source_code_uri: https://github.com/jahuty/jahuty-ruby
|
208
|
+
changelog_uri: https://github.com/jahuty/jahuty-ruby/blob/master/CHANGELOG.md
|
98
209
|
post_install_message:
|
99
210
|
rdoc_options: []
|
100
211
|
require_paths:
|
101
212
|
- lib
|
102
213
|
required_ruby_version: !ruby/object:Gem::Requirement
|
103
214
|
requirements:
|
104
|
-
- - "
|
215
|
+
- - "~>"
|
105
216
|
- !ruby/object:Gem::Version
|
106
|
-
version: '
|
217
|
+
version: '2.6'
|
107
218
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
219
|
requirements:
|
109
220
|
- - ">="
|
110
221
|
- !ruby/object:Gem::Version
|
111
222
|
version: '0'
|
112
223
|
requirements: []
|
113
|
-
rubygems_version: 3.
|
224
|
+
rubygems_version: 3.1.4
|
114
225
|
signing_key:
|
115
226
|
specification_version: 4
|
116
227
|
summary: Jahuty's Ruby SDK.
|
data/lib/jahuty/data/problem.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
module Jahuty
|
2
|
-
module Data
|
3
|
-
class Problem
|
4
|
-
attr_accessor :status, :type, :detail
|
5
|
-
|
6
|
-
def initialize(status, type, detail)
|
7
|
-
@status = status
|
8
|
-
@type = type
|
9
|
-
@detail = detail
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.from(data)
|
13
|
-
raise ArgumentError.new "Key :status does not exist" if !data.key?(:status)
|
14
|
-
raise ArgumentError.new "Key :type does not exist" if !data.key?(:type)
|
15
|
-
raise ArgumentError.new "Key :detail does not exist" if !data.key?(:detail)
|
16
|
-
|
17
|
-
Problem.new(data[:status], data[:type], data[:detail])
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
data/lib/jahuty/data/snippet.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
module Jahuty
|
2
|
-
module Data
|
3
|
-
class Snippet
|
4
|
-
attr_accessor :id, :content
|
5
|
-
|
6
|
-
def initialize(id, content)
|
7
|
-
@id = id
|
8
|
-
@content = content
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.from(data)
|
12
|
-
raise ArgumentError.new "Key :id does not exist" if !data.key?(:id)
|
13
|
-
raise ArgumentError.new "Key :content does not exist" if !data.key?(:content)
|
14
|
-
|
15
|
-
Snippet.new(data[:id], data[:content])
|
16
|
-
end
|
17
|
-
|
18
|
-
def to_s
|
19
|
-
@content
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require "faraday"
|
2
|
-
|
3
|
-
module Jahuty
|
4
|
-
module Service
|
5
|
-
class Connect
|
6
|
-
URL = "https://www.jahuty.com/api"
|
7
|
-
|
8
|
-
HEADERS = {
|
9
|
-
"Accept": "application/json;q=0.9,*/*;q=0.8",
|
10
|
-
"Accept-Encoding": "gzip, deflate",
|
11
|
-
"Content-Type": "application/json; charset=utf-8",
|
12
|
-
"User-Agent": "Jahuty Ruby client #{::Jahuty::VERSION}"
|
13
|
-
}
|
14
|
-
|
15
|
-
def call(key)
|
16
|
-
Faraday.new(
|
17
|
-
url: URL,
|
18
|
-
headers: {"Authorization": "Bearer #{key}"}.merge(HEADERS)
|
19
|
-
)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/lib/jahuty/service/get.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require "json"
|
2
|
-
|
3
|
-
module Jahuty
|
4
|
-
class Service::Get
|
5
|
-
@connection
|
6
|
-
|
7
|
-
def initialize(connection)
|
8
|
-
@connection = connection
|
9
|
-
end
|
10
|
-
|
11
|
-
def call(id, params = {})
|
12
|
-
response = @connection.get("snippets/#{id}", { params: params })
|
13
|
-
|
14
|
-
payload = JSON.parse(response.body, symbolize_names: true)
|
15
|
-
|
16
|
-
if response.status != 200
|
17
|
-
raise Exception::NotOk.new(Data::Problem.from(payload))
|
18
|
-
end
|
19
|
-
|
20
|
-
return Data::Snippet.from(payload)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/lib/jahuty/snippet.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
module Jahuty
|
2
|
-
class Snippet
|
3
|
-
@get
|
4
|
-
|
5
|
-
class << self
|
6
|
-
def get(id, params = {})
|
7
|
-
raise "API key not set. Did you use Jahuty.key?" unless Jahuty.key?
|
8
|
-
|
9
|
-
@get ||= Service::Get.new(Service::Connect.new.call(Jahuty.key))
|
10
|
-
|
11
|
-
@get.call(id, params)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|