shadow_cabinet 0.1.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
+ SHA1:
3
+ metadata.gz: 7c7d851f2e66541dedd6f0ee304a55c2cd6951a3
4
+ data.tar.gz: f2cd55dca943163cfd9073a1a3a332cc7560854d
5
+ SHA512:
6
+ metadata.gz: 2bc35e6ff56f09b8134d3f22b5290de521d3c2823d6ca751d114138aadd7532f1b1e67938b710e3cbbd37999b4dc32014bc20942ed225a3dd8da5026c680f777
7
+ data.tar.gz: b223b04db5624e6081477f26010dedf4b9e2d2afb5baf56e0145e0d3a331f9bc6072495f0bcad1a54cbd9c84e9b2c9942073140bedccca5ac3d9209df33c08e9
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ *~
15
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ stormwatch-client
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Dennis Walters
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Stormwatch
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/stormwatch`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'stormwatch'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install stormwatch
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/stormwatch.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://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 "shadow_cabinet"
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
@@ -0,0 +1,25 @@
1
+ require 'shadow_cabinet/client'
2
+ require 'shadow_cabinet/version'
3
+
4
+ module ShadowCabinet
5
+ def self.client(url:, key:, secret:)
6
+ Client::Real.new(url: url, key: key, secret: secret)
7
+ end
8
+
9
+ def self.update_box(api, id, content)
10
+ begin
11
+ data = api.put(
12
+ path: "/boxes/#{id}",
13
+ data: {
14
+ "box" => {
15
+ "content" => content
16
+ }
17
+ }.to_json
18
+ )
19
+ rescue
20
+ return false
21
+ end
22
+
23
+ true
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ module ShadowCabinet
2
+ module Client
3
+ Unimplemented = Class.new(StandardError)
4
+ TransportError = Class.new(StandardError)
5
+ end
6
+ end
7
+
8
+ #require 'shadow_cabinet/client/mock'
9
+ require 'shadow_cabinet/client/real'
@@ -0,0 +1,87 @@
1
+ require 'addressable/uri'
2
+ require 'shadow_cabinet/client/result'
3
+
4
+ module ShadowCabinet
5
+ module Client
6
+ class Base
7
+ attr_reader :url, :key, :secret
8
+
9
+ def initialize(url:, key:, secret:)
10
+ @url = url
11
+ @key = key
12
+ @secret = secret
13
+ end
14
+
15
+ def get(path:, params: {})
16
+ result = make_request(
17
+ verb: :get,
18
+ request_url: construct_url(path: path, params: params)
19
+ )
20
+
21
+ raise result.error if result.error_present?
22
+
23
+ result.body
24
+ end
25
+
26
+ def post(path:, params: {}, data: nil)
27
+ result = make_request(
28
+ verb: :post,
29
+ request_url: construct_url(path: path, params: params),
30
+ data: data
31
+ )
32
+
33
+ raise result.error if result.error_present?
34
+
35
+ result.body
36
+ end
37
+
38
+ def put(path:, params: {}, data: nil)
39
+ result = make_request(
40
+ verb: :put,
41
+ request_url: construct_url(path: path, params: params),
42
+ data: data
43
+ )
44
+
45
+ raise result.error if result.error_present?
46
+
47
+ result.body
48
+ end
49
+
50
+ def delete(path:, params: {})
51
+ result = make_request(
52
+ verb: :delete,
53
+ request_url: construct_url(path, params)
54
+ )
55
+
56
+ raise result.error if result.error_present?
57
+
58
+ result.body
59
+ end
60
+
61
+ private
62
+
63
+ def make_request(verb:, request_url:, data: nil)
64
+ Result.new(
65
+ error: Unimplmented.new(
66
+ "#make_request is not defined for #{self.class}"
67
+ )
68
+ )
69
+ end
70
+
71
+ def base_url
72
+ @base_url ||= Addressable::URI.parse(url)
73
+ end
74
+
75
+ def construct_url(path:, params: {})
76
+ base_url.tap do |url|
77
+ url.path = File.join('/', 'api', 'v1', path)
78
+
79
+ url.query_values = params
80
+
81
+ return url
82
+ end
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,35 @@
1
+ require 'faraday'
2
+ require 'shadow_cabinet/client/base'
3
+ require 'shadow_cabinet/client/real/request'
4
+
5
+ module ShadowCabinet
6
+ module Client
7
+ class Real < Base
8
+ private
9
+ def connector
10
+ @connector ||= Faraday.new(url: base_url) do |faraday|
11
+ faraday.headers.merge!(
12
+ {
13
+ 'Content-Type' => 'application/json',
14
+ 'Accept' => 'application/json',
15
+ 'User-Agent' => 'shadow_cabinet-client/0.1.0 (https://github.com/ess/shadow_cabinet-client)',
16
+ 'Authorization' => "#{key}:#{secret}"
17
+ }
18
+ )
19
+
20
+ faraday.request :url_encoded
21
+ faraday.adapter Faraday.default_adapter
22
+ end
23
+ end
24
+
25
+ def make_request(verb:, request_url:, data: nil)
26
+ Request.perform(
27
+ verb: verb,
28
+ connector: connector,
29
+ request_url: request_url,
30
+ data: data
31
+ )
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ require 'shadow_cabinet/client/base'
2
+ require 'shadow_cabinet/client/real/request/put'
3
+
4
+ module ShadowCabinet
5
+ module Client
6
+ class Real < Base
7
+ module Request
8
+ KNOWN = {
9
+ put: Put,
10
+ }
11
+
12
+ def self.perform(verb:, connector:, request_url:, data: nil)
13
+ (KNOWN[verb] || Base).
14
+ new(connector: connector, request_url: request_url, data: data).
15
+ perform
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,74 @@
1
+ require 'json'
2
+
3
+ require 'shadow_cabinet/client/base'
4
+ require 'shadow_cabinet/client/result'
5
+
6
+ module ShadowCabinet
7
+ module Client
8
+ class Real < Base
9
+ module Request
10
+ # A request is an implementation of an HTTP verb in terms of the Engine
11
+ # Yard Core API. This is the base class for all Requests.
12
+ class Base
13
+
14
+ attr_reader :connector, :request_url, :data
15
+
16
+ def initialize(connector:, request_url:, data: nil)
17
+ @connector = connector
18
+ @request_url = request_url
19
+ @data = data
20
+ end
21
+
22
+ def perform
23
+ Result.new(
24
+ error: Unimplemented.new(
25
+ "that operation is not implemented"
26
+ )
27
+ )
28
+ end
29
+
30
+ private
31
+ def path
32
+ request_url.path
33
+ end
34
+
35
+ def params
36
+ @params = request_url.query_values || {}
37
+ end
38
+
39
+ def process_response(response)
40
+ body = response.body
41
+
42
+ status = response.status.to_i
43
+
44
+ if status < 200 || status > 299
45
+ api_errors =
46
+ begin
47
+ JSON.parse(body, symbolize_names: true)[:errors] || []
48
+ rescue
49
+ []
50
+ end
51
+
52
+ if api_errors.empty?
53
+ return Result.new(
54
+ error: TransportError.new(
55
+ "API returned a #{status} status"
56
+ )
57
+ )
58
+ else
59
+ return Result.new(
60
+ error: CoreError.new(
61
+ "Could not process request. The following issues were identified:\n#{api_errors.map(&:to_s).join("\n")}"
62
+ )
63
+ )
64
+ end
65
+ end
66
+
67
+ Result.new(body: body)
68
+ end
69
+
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,25 @@
1
+ require 'shadow_cabinet/client/base'
2
+ require 'shadow_cabinet/client/real/request/base'
3
+
4
+ module ShadowCabinet
5
+ module Client
6
+ class Real < Base
7
+ module Request
8
+ # Implementation for the HTTP PUT verb
9
+ class Put < Base
10
+
11
+ def perform
12
+ response = connector.put(path, data) do |req|
13
+ params.keys.each do |key|
14
+ req.params[key] = params[key]
15
+ end
16
+ end
17
+
18
+ process_response(response)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ module ShadowCabinet
2
+ module Client
3
+ # A structure used within client implementations to encapsulate the result
4
+ # of a given operation. The result contains both a body and an error. If
5
+ # an error is present, the body should be ignored and the error should be
6
+ # handled (or raised). Otherwise, the body should be considered the de-facto
7
+ # result of the operation. This pattern is stolen from Rust, Go, Haskell and
8
+ # the like, but done so in a VERY naive manner.
9
+ Result = Struct.new(:body, :error) do
10
+ def initialize(body: nil, error: nil)
11
+ self.body = body
12
+ self.error = error
13
+ end
14
+
15
+ def error_present?
16
+ !self.error.nil?
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module ShadowCabinet
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "shadow_cabinet/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "shadow_cabinet"
8
+ spec.version = ShadowCabinet::VERSION
9
+ spec.authors = ["Dennis Walters"]
10
+ spec.email = ["dwalters@engineyard.com"]
11
+
12
+ spec.summary = %q{Stormwatch API client}
13
+ spec.homepage = "https://github.com/ess/shadow_cabinet"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'addressable', '~> 2.3.8'
24
+ spec.add_dependency 'faraday', '~> 0.12.1'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.15"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shadow_cabinet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dennis Walters
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.12.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - dwalters@engineyard.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".ruby-gemset"
93
+ - ".ruby-version"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/shadow_cabinet.rb
102
+ - lib/shadow_cabinet/client.rb
103
+ - lib/shadow_cabinet/client/base.rb
104
+ - lib/shadow_cabinet/client/real.rb
105
+ - lib/shadow_cabinet/client/real/request.rb
106
+ - lib/shadow_cabinet/client/real/request/base.rb
107
+ - lib/shadow_cabinet/client/real/request/put.rb
108
+ - lib/shadow_cabinet/client/result.rb
109
+ - lib/shadow_cabinet/version.rb
110
+ - shadow_cabinet.gemspec
111
+ homepage: https://github.com/ess/shadow_cabinet
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.6.13
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Stormwatch API client
135
+ test_files: []