gifnoc 0.0.1

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.
@@ -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,35 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require "gifnoc/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gifnoc"
8
+ spec.version = Gifnoc::VERSION
9
+ spec.authors = ["Cloud 66"]
10
+ spec.email = ["hello@cloud66.com"]
11
+
12
+ spec.summary = "A Ruby Gifnoc client implementation"
13
+ spec.homepage = "https://github.com/cloud66-oss/gifnoc-ruby"
14
+ spec.license = "Apache-2.0"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
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 = "bin"
22
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ spec.add_development_dependency "pry-byebug", "~> 3.6"
29
+ spec.add_development_dependency "webmock", "~> 3.4", ">= 3.4.2"
30
+ spec.add_development_dependency "vcr", "~> 4.0"
31
+ spec.add_development_dependency "simplecov", "~> 0.16.1"
32
+
33
+ spec.add_dependency "httparty", "~> 0.16.2"
34
+ spec.add_dependency "ice_nine", "~> 0.11.2"
35
+ end
@@ -0,0 +1,11 @@
1
+ require 'ice_nine'
2
+ require 'ice_nine/core_ext/object'
3
+
4
+ require "gifnoc/apis"
5
+ require "gifnoc/clients"
6
+ require "gifnoc/exceptions"
7
+ require "gifnoc/models"
8
+ require "gifnoc/version"
9
+
10
+ module Gifnoc
11
+ end
@@ -0,0 +1,5 @@
1
+ require "gifnoc/apis/account_api_methods"
2
+ require "gifnoc/apis/namespace_api_methods"
3
+ require "gifnoc/apis/record_api_methods"
4
+ require "gifnoc/apis/token_api_methods"
5
+ require "gifnoc/apis/api"
@@ -0,0 +1,29 @@
1
+ module Gifnoc
2
+ module AccountAPIMethods
3
+
4
+ def list_accounts
5
+ return with_response_handling do
6
+ self.class.get("/accounts", @options)
7
+ end
8
+ end
9
+
10
+ def get_account(account_uuid)
11
+ return with_response_handling do
12
+ self.class.get("/accounts/#{account_uuid}", @options)
13
+ end
14
+ end
15
+
16
+ def create_account
17
+ return with_response_handling do
18
+ self.class.post("/accounts", @options)
19
+ end
20
+ end
21
+
22
+ def delete_account(account_uuid)
23
+ return with_response_handling do
24
+ self.class.delete("/accounts/#{account_uuid}", @options)
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,47 @@
1
+ require 'httparty'
2
+
3
+ module Gifnoc
4
+ class API
5
+
6
+ include HTTParty
7
+
8
+ include AccountAPIMethods
9
+ include NamespaceAPIMethods
10
+ include RecordAPIMethods
11
+ include TokenAPIMethods
12
+
13
+ default_timeout 30
14
+
15
+ def initialize(token:, base_url:)
16
+ @token = token
17
+ @options = {
18
+ base_uri: base_url,
19
+ headers: headers,
20
+ }
21
+ end
22
+
23
+ private
24
+
25
+ def headers
26
+ return {
27
+ "X-Token" => @token,
28
+ 'Content-Type' => 'application/json',
29
+ }
30
+ end
31
+
32
+ def with_response_handling
33
+ response = begin
34
+ yield
35
+ rescue ::Timeout::Error # includes ::Net::OpenTimeout and ::Net::ReadTimeout
36
+ raise Gifnoc::Timeout
37
+ end
38
+ handle_error(response) unless response.success?
39
+ return response.parsed_response
40
+ end
41
+
42
+ def handle_error(response)
43
+ raise Gifnoc::HTTPError.new(status_code: response.code, response: response.parsed_response)
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,39 @@
1
+ module Gifnoc
2
+ module NamespaceAPIMethods
3
+
4
+ def list_namespaces
5
+ return with_response_handling do
6
+ self.class.get("/namespaces", @options)
7
+ end
8
+ end
9
+
10
+ def get_namespace(namespace_uuid)
11
+ return with_response_handling do
12
+ self.class.get("/namespaces/#{namespace_uuid}", @options)
13
+ end
14
+ end
15
+
16
+ def create_namespace(namespace_hash)
17
+ options = {body: namespace_hash.to_json}
18
+ options = options.merge(@options)
19
+ return with_response_handling do
20
+ self.class.post("/namespaces", options)
21
+ end
22
+ end
23
+
24
+ def update_namespace(namespace_uuid, namespace_hash)
25
+ options = {body: namespace_hash.to_json}
26
+ options = options.merge(@options)
27
+ return with_response_handling do
28
+ self.class.put("/namespaces/#{namespace_uuid}", options)
29
+ end
30
+ end
31
+
32
+ def delete_namespace(namespace_uuid)
33
+ return with_response_handling do
34
+ self.class.delete("/namespaces/#{namespace_uuid}", @options)
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ module Gifnoc
2
+ module RecordAPIMethods
3
+
4
+ def list_records(namespace_uuid)
5
+ return with_response_handling do
6
+ self.class.get("/namespaces/#{namespace_uuid}/records", @options)
7
+ end
8
+ end
9
+
10
+ def get_record(namespace_uuid, record_key)
11
+ return with_response_handling do
12
+ self.class.get("/namespaces/#{namespace_uuid}/records/#{record_key}", @options)
13
+ end
14
+ end
15
+
16
+ def create_record(namespace_uuid, record_hash)
17
+ options = {body: record_hash.to_json}
18
+ options = options.merge(@options)
19
+ return with_response_handling do
20
+ self.class.post("/namespaces/#{namespace_uuid}/records", options)
21
+ end
22
+ end
23
+
24
+ def update_record(namespace_uuid, record_key, record_hash)
25
+ options = {body: record_hash.to_json}
26
+ options = options.merge(@options)
27
+ return with_response_handling do
28
+ self.class.put("/namespaces/#{namespace_uuid}/records/#{record_key}", options)
29
+ end
30
+ end
31
+
32
+ def delete_record(namespace_uuid, record_key)
33
+ return with_response_handling do
34
+ self.class.delete("/namespaces/#{namespace_uuid}/records/#{record_key}", @options)
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ module Gifnoc
2
+ module TokenAPIMethods
3
+
4
+ def list_tokens
5
+ return with_response_handling do
6
+ self.class.get("/tokens", @options)
7
+ end
8
+ end
9
+
10
+ def get_token(token_uuid)
11
+ return with_response_handling do
12
+ self.class.get("/tokens/#{token_uuid}", @options)
13
+ end
14
+ end
15
+
16
+ def create_token(token_hash)
17
+ options = {body: token_hash.to_json}
18
+ options = options.merge(@options)
19
+ return with_response_handling do
20
+ self.class.post("/tokens", options)
21
+ end
22
+ end
23
+
24
+ def update_token(token_uuid, token_hash)
25
+ options = {body: token_hash.to_json}
26
+ options = options.merge(@options)
27
+ return with_response_handling do
28
+ self.class.put("/tokens/#{token_uuid}", options)
29
+ end
30
+ end
31
+
32
+ def delete_token(token_uuid)
33
+ return with_response_handling do
34
+ self.class.delete("/tokens/#{token_uuid}", @options)
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ require "gifnoc/clients/account_client_methods"
2
+ require "gifnoc/clients/namespace_client_methods"
3
+ require "gifnoc/clients/record_client_methods"
4
+ require "gifnoc/clients/token_client_methods"
5
+ require "gifnoc/clients/client"
@@ -0,0 +1,24 @@
1
+ module Gifnoc
2
+ module AccountClientMethods
3
+
4
+ def list_accounts
5
+ return @api.list_accounts.map do |raw_account|
6
+ next(Gifnoc::Account.from_api_hash(raw_account))
7
+ end
8
+ end
9
+
10
+ def get_account(account_uuid)
11
+ return Gifnoc::Account.from_api_hash(@api.get_account(account_uuid))
12
+ end
13
+
14
+ def create_account
15
+ raw_result = @api.create_account
16
+ return Gifnoc::Account.from_api_hash(raw_result["account"]), Gifnoc::Token.from_api_hash(raw_result["token"])
17
+ end
18
+
19
+ def delete_account(account_uuid)
20
+ return @api.delete_account(account_uuid)
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ module Gifnoc
2
+ class Client
3
+
4
+ include AccountClientMethods
5
+ include NamespaceClientMethods
6
+ include RecordClientMethods
7
+ include TokenClientMethods
8
+
9
+ def initialize(token:, base_url:)
10
+ @api = Gifnoc::API.new(token: token, base_url: base_url)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ module Gifnoc
2
+ module NamespaceClientMethods
3
+
4
+ def list_namespaces
5
+ return @api.list_namespaces.map do |raw_namespace|
6
+ next(Gifnoc::Namespace.from_api_hash(raw_namespace))
7
+ end
8
+ end
9
+
10
+ def get_namespace(namespace_uuid)
11
+ return Gifnoc::Namespace.from_api_hash(@api.get_namespace(namespace_uuid))
12
+ end
13
+
14
+ def create_namespace(namespace)
15
+ return Gifnoc::Namespace.from_api_hash(@api.create_namespace(namespace.to_api_hash))
16
+ end
17
+
18
+ def update_namespace(namespace_uuid, namespace)
19
+ return Gifnoc::Namespace.from_api_hash(@api.update_namespace(namespace_uuid, namespace.to_api_hash))
20
+ end
21
+
22
+ def delete_namespace(namespace_uuid)
23
+ return @api.delete_namespace(namespace_uuid)
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module Gifnoc
2
+ module RecordClientMethods
3
+
4
+ def list_records(namespace_uuid)
5
+ return @api.list_records(namespace_uuid).map do |raw_record|
6
+ next(Gifnoc::Record.from_api_hash(raw_record))
7
+ end
8
+ end
9
+
10
+ def get_record(namespace_uuid, record_key)
11
+ return Gifnoc::Record.from_api_hash(@api.get_record(namespace_uuid, record_key))
12
+ end
13
+
14
+ def create_record(namespace_uuid, record)
15
+ return Gifnoc::Record.from_api_hash(@api.create_record(namespace_uuid, record.to_api_hash))
16
+ end
17
+
18
+ def update_record(namespace_uuid, record_key, record)
19
+ return Gifnoc::Record.from_api_hash(@api.update_record(namespace_uuid, record_key, record.to_api_hash))
20
+ end
21
+
22
+ def delete_record(namespace_uuid, record_key)
23
+ return @api.delete_record(namespace_uuid, record_key)
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module Gifnoc
2
+ module TokenClientMethods
3
+
4
+ def list_tokens
5
+ return @api.list_tokens.map do |raw_token|
6
+ next(Gifnoc::Token.from_api_hash(raw_token))
7
+ end
8
+ end
9
+
10
+ def get_token(token_uuid)
11
+ return Gifnoc::Token.from_api_hash(@api.get_token(token_uuid))
12
+ end
13
+
14
+ def create_token(token)
15
+ return Gifnoc::Token.from_api_hash(@api.create_token(token.to_api_hash))
16
+ end
17
+
18
+ def update_token(token_uuid, token)
19
+ return Gifnoc::Token.from_api_hash(@api.update_token(token_uuid, token.to_api_hash))
20
+ end
21
+
22
+ def delete_token(token_uuid)
23
+ return @api.delete_token(token_uuid)
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ module Gifnoc
2
+ class Error < StandardError; end
3
+ class Timeout < Error; end;
4
+ class HTTPError < Error
5
+ attr_reader :status_code, :response
6
+
7
+ def initialize(status_code:, response:, message: nil)
8
+ @status_code = status_code
9
+ @response = response
10
+ @message = message
11
+ @default_message = "Gifnoc API request failed with HTTP status code #{status_code}"
12
+ end
13
+
14
+ def to_s
15
+ return @message || @default_message
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ require "gifnoc/models/typed_attr_accessor"
2
+ require "gifnoc/models/model"
3
+ require "gifnoc/models/account"
4
+ require "gifnoc/models/namespace"
5
+ require "gifnoc/models/record"
6
+ require "gifnoc/models/token"
@@ -0,0 +1,18 @@
1
+ module Gifnoc
2
+ class Account < Model
3
+
4
+ ATTRIBUTES = {
5
+ uuid: TypedAttrAccessor::TYPE_STRING,
6
+ namespace_count: TypedAttrAccessor::TYPE_INTEGER,
7
+ created_at: TypedAttrAccessor::TYPE_DATE_TIME,
8
+ updated_at: TypedAttrAccessor::TYPE_DATE_TIME,
9
+ }.deep_freeze
10
+
11
+ API_ATTRIBUTE_LIST = [:uuid, :namespace_count, :created_at, :updated_at].deep_freeze
12
+
13
+ attributes_hash.each do |name, type_alias|
14
+ typed_attr_accessor(name, type_alias)
15
+ end
16
+
17
+ end
18
+ end