beze 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
+ SHA256:
3
+ metadata.gz: b6d584889f587c985651c1a0cbed7e78fcd5749c9d3c8efa87cc8edc56b74ac4
4
+ data.tar.gz: c49296c79872869e4f581c916859077d63e8ba2198aeb3057d78645d68391b3e
5
+ SHA512:
6
+ metadata.gz: 244d4bb0dacd16f9dabd8453ecc766f999b7bef7bda22a10998f3e721ad15ece97da97e4527f801403786e5fc7c9d9925a2fcb217482a74ff350baa318576e8c
7
+ data.tar.gz: ab3704f86d0210ec2a1546380cf5d6a774dcf42c03d3d20827c3368b250ebea78bf9266a8f2719b91099b94ca10e5afacc25130639143cf5e9f8b36498cde94a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Released]
2
+
3
+ ## [0.1.0] - 2022-04-20
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in beze.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Sylvance
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,40 @@
1
+ # Beze
2
+
3
+ Spicy beze gem.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add beze
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install beze
14
+
15
+ ## Usage
16
+
17
+ To use this;
18
+
19
+ ```ruby
20
+ Beze.create_resource(:[resource_name], payload, options)
21
+ Beze.delete_resource(:[resource_name], payload, options)
22
+ Beze.get_resource(:[resource_name], payload, options)
23
+ Beze.list_resource(:[resource_name], payload, options)
24
+ Beze.update_resource(:[resource_name], payload, options)
25
+ Beze.patch_resource(;[resource_name], payload, options)
26
+ ```
27
+
28
+ ## Development
29
+
30
+ 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.
31
+
32
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bezefy/beze.
37
+
38
+ ## License
39
+
40
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/beze.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/beze/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "beze"
7
+ spec.version = Beze::VERSION
8
+ spec.authors = ["Sylvance"]
9
+ spec.email = ["103753399+ninjawork@users.noreply.github.com"]
10
+
11
+ spec.summary = "Spicy beze gem."
12
+ spec.description = "Spicy beze gem"
13
+ spec.homepage = "https://github.com/bezefy/beze"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/bezefy/beze"
21
+ spec.metadata["changelog_uri"] = "https://github.com/bezefy/beze/blob/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "http_client"
4
+
5
+ # Ruby gem that helps you work with Beze API.
6
+ module Beze
7
+ # Client that acts as a middleman to the API.
8
+ class Client
9
+ # Class to display Beze result
10
+ class BezeResult
11
+ attr_accessor :result, :message
12
+
13
+ def initialize(result:, message:)
14
+ @result = result
15
+ @message = message
16
+ end
17
+ end
18
+
19
+ # Class to display error result
20
+ class BezeErrorResult
21
+ attr_accessor :result, :message
22
+
23
+ def initialize(result:, message:)
24
+ @result = result
25
+ @message = message
26
+ end
27
+ end
28
+
29
+ attr_reader :resource, :payload, :options
30
+
31
+ def self.call(resource:, payload:, options:)
32
+ new(resource, payload, options).call
33
+ end
34
+
35
+ def initialize(resource, payload, options)
36
+ @resource = resource
37
+ @payload = payload
38
+ @options = options
39
+ end
40
+
41
+ def call
42
+ body = payload
43
+ headers = options[:headers]
44
+ params = options[:params]
45
+ uri = "#{Beze::BASE_URL}/#{Beze.configuration.api_version}/#{resource}/#{options[:uri]}"
46
+
47
+ http_call = Beze::HttpClient.call(body: body, headers: headers, method: method, params: params, uri: uri)
48
+
49
+ if http_call.successful?
50
+ BezesResult.new(result: http_call, message: "Successful call to endpoint. #{http_call.result.message}")
51
+ else
52
+ BezeErrorResult.new(result: http_call, message: "Failure while calling endpoint. #{http_call.result.message}")
53
+ end
54
+ end
55
+ end
56
+ end
57
+
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "net/http"
5
+ require "openssl"
6
+ require "ostruct"
7
+ require "json"
8
+
9
+ # Ruby gem that helps you work with Bezefy API.
10
+ module Beze
11
+ # Http client that acts as a middleman to the API.
12
+ class HttpClient
13
+ NET_HTTP_ERRORS = [
14
+ Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError,
15
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,
16
+ JSON::ParserError, SocketError
17
+ ].freeze
18
+
19
+ def self.call(method:, uri:, body: nil, params: nil, headers: nil, expect: nil)
20
+ new(method, uri, body, params, headers, expect).call
21
+ end
22
+
23
+ def initialize(method, uri, body, params, headers, expect)
24
+ @method = method
25
+ @body = body
26
+ @headers = headers
27
+ @url = prepare_uri(uri, params)
28
+ @expect = expect.nil? ? :json : expect
29
+ @http = prepare_http
30
+ @request = prepare_request
31
+ end
32
+
33
+ def call
34
+ response
35
+ end
36
+
37
+ private
38
+
39
+ attr_accessor :body, :expect, :headers, :http, :method, :request, :url
40
+
41
+ def response
42
+ http_response = http.request(request)
43
+
44
+ if http_response.is_a?(Net::HTTPSuccess)
45
+ result = Beze.to_ostruct(JSON.parse(http_response.read_body)) if expect == :json
46
+ result = http_response.read_body if expect == :html
47
+ result_struct(result, http_response, true)
48
+ else
49
+ result_struct(nil, http_response, false)
50
+ end
51
+ rescue *NET_HTTP_ERRORS => e
52
+ result_struct(e, http_response, false)
53
+ end
54
+
55
+ def result_struct(result, response, success)
56
+ OpenStruct.new(
57
+ result: result,
58
+ headers: Beze.to_ostruct(response.to_hash),
59
+ code: response.code,
60
+ message: response.message,
61
+ class: response.class.name,
62
+ successful?: success
63
+ )
64
+ end
65
+
66
+ def prepare_uri(uri, params)
67
+ build_url = URI(uri)
68
+ build_url.query = URI.encode_www_form(params) unless params.nil?
69
+ build_url
70
+ end
71
+
72
+ def prepare_http
73
+ protocol = Net::HTTP.new(url.host, url.port)
74
+ protocol.use_ssl = true
75
+ protocol.verify_mode = OpenSSL::SSL::VERIFY_NONE
76
+ protocol
77
+ end
78
+
79
+ def prepare_request
80
+ req = build_request
81
+ req.basic_auth(Beze.configuration.app_email, Beze.configuration.app_password)
82
+ req.body = JSON.dump(body) unless body.nil?
83
+ req
84
+ end
85
+
86
+ def build_request
87
+ return Net::HTTP::Get.new(url, headers) if method == :get
88
+ return Net::HTTP::Post.new(url, headers) if method == :post
89
+ return Net::HTTP::Put.new(url, headers) if method == :put
90
+ return Net::HTTP::Patch.new(url, headers) if method == :patch
91
+ return Net::HTTP::Delete.new(url, headers) if method == :delete
92
+ return Net::HTTP::Options.new(url, headers) if method == :options
93
+ end
94
+ end
95
+ end
96
+
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Beze
4
+ VERSION = "0.1.0"
5
+ end
data/lib/beze.rb ADDED
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ostruct"
4
+
5
+ require_relative "beze/client"
6
+ require_relative "beze/http_client"
7
+ require_relative "beze/version"
8
+
9
+ module Beze
10
+ BASE_URL = ENV.fetch('API_BASE_URL') || "http://localhost:3000"
11
+
12
+ def self.configure
13
+ yield configuration
14
+ end
15
+
16
+ def self.configuration
17
+ @configuration ||= OpenStruct.new(api_version: nil, app_email: nil, app_password: nil)
18
+ end
19
+
20
+ def self.create_resource(resource, payload, options)
21
+ Beze::Client.call(resource: resource, payload: payload, options: options, method: :post)
22
+ end
23
+
24
+ def self.get_resource(resource, payload, options)
25
+ Beze::Client.call(resource: resource, payload: payload, options: options, method: :get)
26
+ end
27
+
28
+ def self.list_resource(resource, payload, options)
29
+ Beze::Client.call(resource: resource, payload: payload, options: options, method: :get)
30
+ end
31
+
32
+ def self.update_resource(resource, payload, options)
33
+ Beze::Client.call(resource: resource, payload: payload, options: options, method: :put)
34
+ end
35
+
36
+ def self.patch_resource(resource, payload, options)
37
+ Beze::Client.call(resource: resource, payload: payload, options: options, method: :patch)
38
+ end
39
+
40
+ def self.delete_resource(resource, payload, options)
41
+ Beze::Client.call(resource: resource, payload: payload, options: options, method: :delete)
42
+ end
43
+
44
+ def self.to_ostruct(input)
45
+ result = {}
46
+
47
+ if input.is_a?(Hash)
48
+ result["type"] = input.class
49
+ result["keys"] = input.keys
50
+
51
+ input.each do |key, value|
52
+ result[key] = hash_or_array(value) ? to_ostruct(value) : OpenStruct.new(value: value, type: value.class)
53
+ end
54
+ end
55
+
56
+ if input.is_a?(Array)
57
+ result["type"] = input.class
58
+ result["size"] = input.size
59
+
60
+ input.each.with_index do |value, index|
61
+ result["value_at_#{index}"] = hash_or_array(value) ? to_ostruct(value) : OpenStruct.new(value: value, type: value.class)
62
+ end
63
+ end
64
+
65
+ OpenStruct.new(result)
66
+ end
67
+
68
+ def self.hash_or_array(val)
69
+ val.is_a?(Hash) || val.is_a?(Array)
70
+ end
71
+ end
72
+
data/sig/beze.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Beze
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beze
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sylvance
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Spicy beze gem
14
+ email:
15
+ - 103753399+ninjawork@users.noreply.github.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - beze.gemspec
28
+ - lib/beze.rb
29
+ - lib/beze/client.rb
30
+ - lib/beze/http_client.rb
31
+ - lib/beze/version.rb
32
+ - sig/beze.rbs
33
+ homepage: https://github.com/bezefy/beze
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ allowed_push_host: https://rubygems.org
38
+ homepage_uri: https://github.com/bezefy/beze
39
+ source_code_uri: https://github.com/bezefy/beze
40
+ changelog_uri: https://github.com/bezefy/beze/blob/main/CHANGELOG.md
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.6.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.2.22
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Spicy beze gem.
60
+ test_files: []