jahuty 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 948fab3c8b8bda77a94f6a69efaf79d4529a2bd26d5183eea758e143bac819d6
4
+ data.tar.gz: 0f8dfaf771f9d50926b1c8042da5e1a121c6f42ad0af5b2ba4879fd8d7663b67
5
+ SHA512:
6
+ metadata.gz: b8a73aa645e540e01355ebc3d75445cafe611b33e3e610bc3b327472f5ed608ea0839f21977348dd8d9a6b69b2c1369f450ae15a9ae7917103c1835e5da0bf1f
7
+ data.tar.gz: dfe0afb290abd658c8a2fef352e36aa0141b63e17282bfb476b1829dd32229ffa157eb7b1ba40d77e084a2d3897a3b6cd304545f1058b95a44464f594a3f8a2f
data/.gitignore ADDED
@@ -0,0 +1,45 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # RSpec artifacts
14
+ .rspec_status
15
+
16
+ # Used by dotenv library to load environment variables.
17
+ .env
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Documentation cache and generated files:
28
+ /.yardoc/
29
+ /_yardoc/
30
+ /doc/
31
+ /rdoc/
32
+
33
+ ## Environment normalization:
34
+ /.bundle/
35
+ /vendor/bundle
36
+ /lib/bundler/man/
37
+
38
+ # for a library or gem, you might want to ignore these files since the code is
39
+ # intended to run in multiple environments; otherwise, check them in:
40
+ Gemfile.lock
41
+ .ruby-version
42
+ .ruby-gemset
43
+
44
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
45
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## 1.0.0 - 2020-03-08
9
+
10
+ - Update `rake` version.
11
+ - Move API key to `Jahuty` object from `Snippet` object.
12
+ - Rename gem to `jahuty` from `jahuty-snippet`.
13
+ - Rename repository to `jahuty-ruby` from `snippets-ruby`.
14
+ - Add `CHANGELOG.md`.
15
+
16
+ ## 0.1.0 - 2019-09-07
17
+
18
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jahuty.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 jahuty
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # jahuty-ruby
2
+ Welcome [Jahuty's](https://www.jahuty.com) Ruby SDK!
3
+
4
+ ## Installation
5
+
6
+ This library requires [Ruby 2.3+](https://www.ruby-lang.org/en/downloads/releases/).
7
+
8
+ It is multi-platform, and we strive to make it run equally well on Windows, Linux, and OSX.
9
+
10
+ Add this line to your application's `Gemfile`, where `x` is the latest major version number:
11
+
12
+ ```ruby
13
+ gem "jahuty", "~> x"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```bash
19
+ $ bundle
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Before use, the library needs to be configured with your [API key](https://www.jahuty.com/docs/api#authentication) (ideally, once during startup):
25
+
26
+ ```ruby
27
+ require "jahuty"
28
+
29
+ Jahuty.key = "YOUR_API_KEY"
30
+ ```
31
+
32
+ With the API key set, you can use the `get()` method to retrieve a snippet:
33
+
34
+ Then, use the `.get` method to fetch a snippet:
35
+
36
+ ```ruby
37
+ require "jahuty"
38
+
39
+ # retrieve the snippet
40
+ snippet = Snippet.get YOUR_SNIPPET_ID
41
+
42
+ # convert it to a string
43
+ snippet.to_s
44
+
45
+ # or, access its attributes
46
+ snippet.id
47
+ snippet.content
48
+ ```
49
+
50
+ In an HTML view:
51
+
52
+ ```html+erb
53
+ <%-
54
+ require "jahuty"
55
+
56
+ Jahuty.key = "YOUR_API_KEY"
57
+ %>
58
+ <!doctype html>
59
+ <html>
60
+ <head>
61
+ <title>Awesome example</title>
62
+ </head>
63
+ <body>
64
+ <%= Snippet.get YOUR_SNIPPET_ID %>
65
+ </body>
66
+ ```
67
+
68
+ If you don't set your API key before calling `Snippet.get`, a `StandardError` will be raised. If an error occurs with [Jahuty's API](https://www.jahuty.com/docs/api), a `NotOk` exception will be raised:
69
+
70
+ ```ruby
71
+ require "jahuty"
72
+
73
+ begin
74
+ Snippet.get YOUR_SNIPPET_ID
75
+ rescue StandardError => e
76
+ # hmm, did you set the API key first?
77
+ rescue Jahuty::Exception::NotOk => e
78
+ # hmm, the API returned something besides 2xx status code
79
+ puts e.problem.type # a URL to more information
80
+ puts e.problem.status # the status code
81
+ puts e.problem.detail # a description of the error
82
+ end
83
+ ```
84
+
85
+ That's it!
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/jahuty/snippets-ruby).
90
+
91
+ ## License
92
+
93
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jahuty/snippets"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/jahuty.gemspec ADDED
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require "jahuty"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "jahuty"
9
+ spec.version = Jahuty::VERSION
10
+ spec.authors = ["Jack Clayton"]
11
+ spec.email = ["jack@jahuty.com"]
12
+
13
+ spec.summary = %q{Jahuty's Ruby SDK.}
14
+ spec.description = %q{Turn any page into a content-managed page.}
15
+ spec.homepage = "https://github.com/jahuty/jahuty-ruby"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency 'faraday', '~> 0.1'
26
+
27
+ spec.add_development_dependency "bundler", "~> 2.0"
28
+ spec.add_development_dependency "rake", "~> 12.3"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
data/lib/jahuty.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "jahuty/version"
2
+
3
+ require "jahuty/snippet"
4
+
5
+ require "jahuty/data/problem"
6
+ require "jahuty/data/snippet"
7
+
8
+ require "jahuty/exception/not_ok"
9
+
10
+ require "jahuty/service/connect"
11
+ require "jahuty/service/get"
12
+
13
+ module Jahuty
14
+ @key
15
+
16
+ class << self
17
+ attr_accessor :key
18
+
19
+ def key?
20
+ !(@key.nil? || @key.empty?)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
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
@@ -0,0 +1,23 @@
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
@@ -0,0 +1,15 @@
1
+ module Jahuty
2
+ module Exception
3
+ class NotOk < ::StandardError
4
+ attr_reader :problem
5
+
6
+ def initialize(problem)
7
+ @problem = problem
8
+ end
9
+
10
+ def message
11
+ "The API responded with #{@problem.status}, #{@problem.type}: #{@problem.detail}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ require "faraday"
2
+
3
+ module Jahuty
4
+ module Service
5
+ class Connect
6
+ URL = "https://www.jahuty.com/api/snippets"
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
@@ -0,0 +1,24 @@
1
+ require "json"
2
+
3
+ module Jahuty
4
+ class Service::Get
5
+ @connection
6
+
7
+ def initialize(connection)
8
+ @connection = connection
9
+
10
+ end
11
+
12
+ def call(id)
13
+ response = @connection.get(id.to_s)
14
+
15
+ payload = JSON.parse(response.body, symbolize_names: true)
16
+
17
+ if response.status != 200
18
+ raise Exception::NotOk.new(Data::Problem.from(payload))
19
+ end
20
+
21
+ return Data::Snippet.from(payload)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module Jahuty
2
+ class Snippet
3
+ @@get
4
+
5
+ def self.get(id)
6
+ raise "API key not set. Did you use Jahuty.key?" unless Jahuty.key?
7
+
8
+ @@get ||= Service::Get.new(Service::Connect.new.call(Jahuty.key))
9
+
10
+ @@get.call(id)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Jahuty
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jahuty
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jack Clayton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Turn any page into a content-managed page.
70
+ email:
71
+ - jack@jahuty.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - CHANGELOG.md
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - jahuty.gemspec
86
+ - lib/jahuty.rb
87
+ - lib/jahuty/data/problem.rb
88
+ - lib/jahuty/data/snippet.rb
89
+ - lib/jahuty/exception/not_ok.rb
90
+ - lib/jahuty/service/connect.rb
91
+ - lib/jahuty/service/get.rb
92
+ - lib/jahuty/snippet.rb
93
+ - lib/jahuty/version.rb
94
+ homepage: https://github.com/jahuty/jahuty-ruby
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.0.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Jahuty's Ruby SDK.
117
+ test_files: []