api_pattern 0.0.1

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: b824d5027294a5fbec1ae0b376da2e1031527c930aec4d70ba6924efbc1b3750
4
+ data.tar.gz: 8ec7712914d9d528de5fea6ff78bc618d0e6245da5ef0fb71475334510d168e8
5
+ SHA512:
6
+ metadata.gz: 06ede13830d1bd225cc84484c3a1aa5c6d7f723c6e32c010e27bc0c1cdefe09f1b892bdedd22d7e18fb507ac411afad7346a50103b6aeb607b426bd82f74e710
7
+ data.tar.gz: 9a414a4e91c7413d430fe3e059b9eb7ff5ccb407197c4b2a358a379b81151c181a364f1613b46e28b2fcc83f5148a4ba98372fd30f4304dcff66544a4748f703
@@ -0,0 +1,24 @@
1
+ name: Ruby Tests
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.2.2
14
+ - name: Cache dependencies
15
+ uses: actions/cache@v2
16
+ with:
17
+ path: vendor/bundle
18
+ key: v1-${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
19
+ restore-keys: |
20
+ v1-${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
21
+ - name: Install dependencies
22
+ run: bundle install
23
+ - name: Run tests
24
+ run: bundle exec rake test
data/.gitignore ADDED
@@ -0,0 +1,56 @@
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
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at contact@jasonchalom.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/TRex22/api-pattern" }
4
+
5
+ # Specify your gem's dependencies in api-pattern.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,119 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ api_pattern (0.0.1)
5
+ active_attr (~> 0.15.4)
6
+ httparty (~> 0.21.0)
7
+ nokogiri (~> 1.14.3)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actionpack (7.0.4.3)
13
+ actionview (= 7.0.4.3)
14
+ activesupport (= 7.0.4.3)
15
+ rack (~> 2.0, >= 2.2.0)
16
+ rack-test (>= 0.6.3)
17
+ rails-dom-testing (~> 2.0)
18
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
19
+ actionview (7.0.4.3)
20
+ activesupport (= 7.0.4.3)
21
+ builder (~> 3.1)
22
+ erubi (~> 1.4)
23
+ rails-dom-testing (~> 2.0)
24
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
25
+ active_attr (0.15.4)
26
+ actionpack (>= 3.0.2, < 7.1)
27
+ activemodel (>= 3.0.2, < 7.1)
28
+ activesupport (>= 3.0.2, < 7.1)
29
+ activemodel (7.0.4.3)
30
+ activesupport (= 7.0.4.3)
31
+ activesupport (7.0.4.3)
32
+ concurrent-ruby (~> 1.0, >= 1.0.2)
33
+ i18n (>= 1.6, < 2)
34
+ minitest (>= 5.1)
35
+ tzinfo (~> 2.0)
36
+ addressable (2.8.4)
37
+ public_suffix (>= 2.0.2, < 6.0)
38
+ ansi (1.5.0)
39
+ builder (3.2.4)
40
+ coderay (1.1.3)
41
+ concurrent-ruby (1.2.2)
42
+ crack (0.4.5)
43
+ rexml
44
+ crass (1.0.6)
45
+ erubi (1.12.0)
46
+ hashdiff (1.0.1)
47
+ httparty (0.21.0)
48
+ mini_mime (>= 1.0.0)
49
+ multi_xml (>= 0.5.2)
50
+ i18n (1.12.0)
51
+ concurrent-ruby (~> 1.0)
52
+ loofah (2.20.0)
53
+ crass (~> 1.0.2)
54
+ nokogiri (>= 1.5.9)
55
+ method_source (1.0.0)
56
+ mini_mime (1.1.2)
57
+ mini_portile2 (2.8.1)
58
+ minitest (5.18.0)
59
+ minitest-focus (1.3.1)
60
+ minitest (>= 4, < 6)
61
+ minitest-reporters (1.6.0)
62
+ ansi
63
+ builder
64
+ minitest (>= 5.0)
65
+ ruby-progressbar
66
+ mocha (2.0.2)
67
+ ruby2_keywords (>= 0.0.5)
68
+ multi_xml (0.6.0)
69
+ nokogiri (1.14.3)
70
+ mini_portile2 (~> 2.8.0)
71
+ racc (~> 1.4)
72
+ nokogiri (1.14.3-arm64-darwin)
73
+ racc (~> 1.4)
74
+ nokogiri (1.14.3-x86_64-linux)
75
+ racc (~> 1.4)
76
+ pry (0.14.2)
77
+ coderay (~> 1.1)
78
+ method_source (~> 1.0)
79
+ public_suffix (5.0.1)
80
+ racc (1.6.2)
81
+ rack (2.2.6.4)
82
+ rack-test (2.1.0)
83
+ rack (>= 1.3)
84
+ rails-dom-testing (2.0.3)
85
+ activesupport (>= 4.2.0)
86
+ nokogiri (>= 1.6)
87
+ rails-html-sanitizer (1.5.0)
88
+ loofah (~> 2.19, >= 2.19.1)
89
+ rake (13.0.6)
90
+ rexml (3.2.5)
91
+ ruby-progressbar (1.13.0)
92
+ ruby2_keywords (0.0.5)
93
+ timecop (0.9.6)
94
+ tzinfo (2.0.6)
95
+ concurrent-ruby (~> 1.0)
96
+ webmock (3.18.1)
97
+ addressable (>= 2.8.0)
98
+ crack (>= 0.3.2)
99
+ hashdiff (>= 0.4.0, < 2.0.0)
100
+
101
+ PLATFORMS
102
+ arm64-darwin-20
103
+ arm64-darwin-22
104
+ ruby
105
+ x86_64-linux
106
+
107
+ DEPENDENCIES
108
+ api_pattern!
109
+ minitest (~> 5.18.0)
110
+ minitest-focus (~> 1.3.1)
111
+ minitest-reporters (~> 1.6.0)
112
+ mocha (~> 2.0.2)
113
+ pry (~> 0.14.2)
114
+ rake (~> 13.0.6)
115
+ timecop (~> 0.9.6)
116
+ webmock (~> 3.18.1)
117
+
118
+ BUNDLED WITH
119
+ 2.3.16
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Jason Chalom
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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 trex22
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,114 @@
1
+ # ApiPattern
2
+ A base set of ops for all my API clients to make maintenance easier
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'api-pattern'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install api-pattern
19
+
20
+ ## Usage
21
+
22
+ ### Only unauthorised calls
23
+ ```ruby
24
+ require 'api-pattern'
25
+
26
+ class ExampleClient < ApiClient::Client
27
+ def self.compatible_api_version
28
+ 'v1'
29
+ end
30
+
31
+ def self.api_version
32
+ 'v1 2023-04-24'
33
+ end
34
+
35
+ def example_unauthorised_get
36
+ unauthorised_and_send(http_method: :get, path: "messages")
37
+ end
38
+
39
+ def example_unauthorised_post(payload)
40
+ unauthorised_and_send(http_method: :post, path: "users", payload: payload)
41
+ end
42
+ end
43
+
44
+ client = ExampleClient.new(
45
+ content_type: "application/json",
46
+ base_path: "https://example.com",
47
+ port: 443
48
+ )
49
+
50
+ client.example_unauthorised_get
51
+ ```
52
+
53
+ ### Using authorised calls
54
+ ```ruby
55
+ require 'api-pattern'
56
+
57
+ class ExampleClient < ApiClient::Client
58
+ def self.compatible_api_version
59
+ 'v1'
60
+ end
61
+
62
+ def self.api_version
63
+ 'v1 2023-04-24'
64
+ end
65
+
66
+ def example_authorised_get
67
+ authorised_and_send(http_method: :get, path: "messages")
68
+ end
69
+
70
+ def example_authorised_post(payload)
71
+ authorised_and_send(http_method: :post, path: "users", payload: payload)
72
+ end
73
+ end
74
+
75
+ client = ExampleClient.new(
76
+ token: "abc123",
77
+ content_type: "application/json",
78
+ base_path: "https://example.com",
79
+ port: 443
80
+ )
81
+
82
+ client.example_unauthorised_get
83
+ ```
84
+
85
+ ## Upgrades
86
+ Make sure to run:
87
+
88
+ ```
89
+ bundle lock --add-platform x86_64-linux
90
+ bundle lock --add-platform ruby
91
+ ```
92
+
93
+ ## Development
94
+
95
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
96
+
97
+ 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).
98
+
99
+ ### Tests
100
+ To run tests execute:
101
+
102
+ $ rake test
103
+
104
+ ## Contributing
105
+
106
+ Bug reports and pull requests are welcome on GitHub at https://github.com/trex22/api-pattern. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
107
+
108
+ ## License
109
+
110
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
111
+
112
+ ## Code of Conduct
113
+
114
+ Everyone interacting in the api-pattern: project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/trex22/api-pattern/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,38 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "api-pattern/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "api_pattern"
7
+ spec.version = ApiPattern::VERSION
8
+ spec.authors = ["trex22"]
9
+ spec.email = ["contact@jasonchalom.com"]
10
+
11
+ spec.summary = "A client for using the ApiPattern API in Ruby."
12
+ spec.description = "A client for using the ApiPattern API in Ruby. Built from their api documentation. This is an unofficial project."
13
+ spec.homepage = "https://github.com/TRex22/ApiPattern"
14
+ spec.license = "MIT"
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 = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "httparty", "~> 0.21.0"
26
+ spec.add_dependency "active_attr", "~> 0.15.4"
27
+ spec.add_dependency "nokogiri", "~> 1.14.3"
28
+
29
+ # Development dependancies
30
+ spec.add_development_dependency "rake", "~> 13.0.6"
31
+ spec.add_development_dependency "minitest", "~> 5.18.0"
32
+ spec.add_development_dependency "minitest-focus", "~> 1.3.1"
33
+ spec.add_development_dependency "minitest-reporters", "~> 1.6.0"
34
+ spec.add_development_dependency "timecop", "~> 0.9.6"
35
+ spec.add_development_dependency "mocha", "~> 2.0.2"
36
+ spec.add_development_dependency "pry", "~> 0.14.2"
37
+ spec.add_development_dependency "webmock", "~> 3.18.1"
38
+ end
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "api-pattern"
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
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,52 @@
1
+ # ClassHelpers
2
+ # Used to give some of the quality of life methods Rails would usually provide
3
+ # TODO: May need to evaluate using these
4
+ class Hash
5
+ def with_indifferent_access
6
+ dup.with_indifferent_access!
7
+ end
8
+
9
+ def with_indifferent_access!
10
+ keys.each { |key| resolve(key) }
11
+
12
+ self
13
+ end
14
+
15
+ def present?
16
+ !blank?
17
+ end
18
+
19
+ def blank?
20
+ nil? || self == {}
21
+ end
22
+
23
+ private
24
+
25
+ def resolve(key)
26
+ if self[key].is_a?(Hash)
27
+ self[key.to_s] = delete(key).with_indifferent_access!
28
+ else
29
+ self[key.to_s] = delete(key)
30
+ end
31
+ end
32
+ end
33
+
34
+ class String
35
+ def present?
36
+ !blank?
37
+ end
38
+
39
+ def blank?
40
+ nil? || self == "" || self == ""
41
+ end
42
+ end
43
+
44
+ class NilClass
45
+ def present?
46
+ !blank?
47
+ end
48
+
49
+ def blank?
50
+ nil?
51
+ end
52
+ end
@@ -0,0 +1,198 @@
1
+ module ApiPattern
2
+ class Client
3
+ include ::ApiPattern::Constants
4
+
5
+ attr_reader :base_path, :port, :content_type, :limit
6
+
7
+ # Other auth parameters may be removed in the future
8
+ # For now making sure this base is compatible with all my API clients
9
+ attr_reader :auth, :auth_type
10
+ attr_reader :token
11
+ attr_reader :username, :key
12
+ attr_reader :password, :secret
13
+
14
+ # Response instance variables
15
+ attr_reader :login_response, :raw_cookie, :expiry
16
+
17
+ def initialize(base_path:, content_type:, port: BASE_PORT, auth: EMPTY_AUTH, auth_type: DEFAULT_AUTH_TYPE, token: EMPTY_PARAMETER, api_token: EMPTY_PARAMETER, access_token: EMPTY_PARAMETER, username: EMPTY_PARAMETER, key: EMPTY_PARAMETER, password: EMPTY_PARAMETER, secret: EMPTY_PARAMETER, limit: EMPTY_PARAMETER)
18
+ process_auth(auth, auth_type, token, api_token, access_token, username, key, password, secret)
19
+
20
+ @content_type = content_type
21
+ @base_path = base_path
22
+ @port = port
23
+ @limit = limit
24
+ end
25
+
26
+ def self.compatible_api_version
27
+ raise NotImplementedError
28
+ end
29
+
30
+ # This is the version of the API docs this client was built off-of
31
+ def self.api_version
32
+ raise NotImplementedError
33
+ end
34
+
35
+ private
36
+
37
+ def unauthorised_and_send(http_method:, path:, payload: {}, params: {}, format: :json)
38
+ start_time = get_micro_second_time
39
+
40
+ response = ::HTTParty.send(
41
+ http_method.to_sym,
42
+ construct_base_path(path, params),
43
+ body: process_payload(payload),
44
+ headers: {
45
+ "Content-Type": @content_type,
46
+ },
47
+ port: port,
48
+ format: format,
49
+ )
50
+
51
+ end_time = get_micro_second_time
52
+ construct_response_object(response, path, start_time, end_time)
53
+ end
54
+
55
+ def authorise_and_send(http_method:, path:, payload: {}, params: {}, format: :json)
56
+ start_time = get_micro_second_time
57
+
58
+ send_params = {
59
+ body: process_payload(payload),
60
+ headers: {
61
+ "Content-Type": @content_type
62
+ },
63
+ port: port,
64
+ format: format,
65
+ }
66
+
67
+ response = ::HTTParty.send(
68
+ http_method.to_sym,
69
+ construct_base_path(path, params),
70
+ **configure_auth(send_params)
71
+ )
72
+
73
+ end_time = get_micro_second_time
74
+ construct_response_object(response, path, start_time, end_time)
75
+ end
76
+
77
+ def construct_response_object(response, path, start_time, end_time)
78
+ {
79
+ "body" => parse_body(response, path),
80
+ "headers" => response.headers,
81
+ "metadata" => construct_metadata(response, start_time, end_time)
82
+ }
83
+ end
84
+
85
+ def construct_metadata(response, start_time, end_time)
86
+ total_time = end_time - start_time
87
+
88
+ {
89
+ "start_time" => start_time,
90
+ "end_time" => end_time,
91
+ "total_time" => total_time
92
+ }
93
+ end
94
+
95
+ def body_is_present?(response)
96
+ !body_is_missing?(response)
97
+ end
98
+
99
+ def body_is_missing?(response)
100
+ response.body.nil? || response.body.empty?
101
+ end
102
+
103
+ def parse_body(response, path)
104
+ parsed_response = JSON.parse(response.body) # Purposely not using HTTParty
105
+
106
+ if parsed_response.dig(path.to_s)
107
+ parsed_response.dig(path.to_s)
108
+ else
109
+ parsed_response
110
+ end
111
+ rescue JSON::ParserError => _e
112
+ response.body
113
+ end
114
+
115
+ def get_micro_second_time
116
+ (Time.now.to_f * 1_000_000).to_i
117
+ end
118
+
119
+ def construct_base_path(path, params)
120
+ constructed_path = "#{base_path}/#{path}"
121
+
122
+ if params == {}
123
+ constructed_path
124
+ else
125
+ "#{constructed_path}?#{process_params(params)}"
126
+ end
127
+ end
128
+
129
+ def process_payload(payload)
130
+ if @content_type.to_s.downcase.include?("json")
131
+ payload.to_json
132
+ else
133
+ payload
134
+ end
135
+ end
136
+
137
+ def process_params(params)
138
+ params.keys.map { |key| "#{key}=#{params[key]}" }.join("&")
139
+ end
140
+
141
+ def process_auth(auth, auth_type, token, api_token, access_token, username, key, password, secret)
142
+ @auth = auth || {}
143
+ @auth_type = auth_type
144
+
145
+ if token.present? || api_token.present? || access_token.present?
146
+ @auth["token"] = token || api_token || access_token
147
+
148
+ @auth_type = "token"
149
+ @token = token || api_token || access_token
150
+ elsif username.present? && password.present?
151
+ @auth["username"] = username
152
+ @auth["password"] = password
153
+
154
+ basic_auth(username, password)
155
+ elsif key.present? && secret.present?
156
+ @key = key
157
+ @secret = secret
158
+
159
+ basic_auth(key, secret)
160
+ end
161
+ end
162
+
163
+ def basic_auth(username, password)
164
+ @auth["username"] = username
165
+ @auth["password"] = password
166
+
167
+ @auth_type = "basic"
168
+ @username = username
169
+ @password = password
170
+ end
171
+
172
+ # Need to use symbols here to HTTParty named parameters
173
+ def configure_auth(send_params)
174
+ if @auth_type == "token"
175
+ headers = send_params[:headers]
176
+ headers["Token"] = @auth["token"]
177
+ send_params[:headers] = headers
178
+ elsif @auth_type == "basic"
179
+ send_params[:basic_auth] = @auth # { username: "", password: "" }
180
+ end
181
+
182
+ # TODO: Using aliases for the sending
183
+ # TODO: Basic auth via body
184
+ # TODO: Separate login flow
185
+
186
+ send_params
187
+ end
188
+
189
+ # https://stackoverflow.com/questions/913349/what-is-the-best-way-to-create-alias-to-attributes-in-ruby
190
+ def alias_attr(new_attr, original)
191
+ alias_method(new_attr, original) if method_defined? original
192
+
193
+ new_writer = "#{new_attr}="
194
+ original_writer = "#{original}="
195
+ alias_method(new_writer, original_writer) if method_defined? original_writer
196
+ end
197
+ end
198
+ end
@@ -0,0 +1,16 @@
1
+ module ApiPattern
2
+ module Constants
3
+ BASE_PORT = 80
4
+
5
+ DEFAULT_AUTH_TYPE = "basic"
6
+ EMPTY_AUTH = {}
7
+ EMPTY_PARAMETER = nil
8
+
9
+ AUTH_TYPES = {
10
+ none: 0,
11
+ basic: 1,
12
+ token: 2,
13
+ oauth: 3,
14
+ }
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module ApiPattern
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require "httparty"
2
+ require "nokogiri"
3
+
4
+ require "api-pattern/class_helpers"
5
+
6
+ require "api-pattern/version"
7
+ require "api-pattern/constants"
8
+
9
+ require "api-pattern/client"
10
+
11
+ module ApiPattern
12
+ class Error < StandardError; end
13
+ end
metadata ADDED
@@ -0,0 +1,216 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_pattern
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - trex22
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.21.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.21.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_attr
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.15.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.15.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.14.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.14.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 13.0.6
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 13.0.6
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 5.18.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 5.18.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-focus
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-reporters
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.6.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.6.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: timecop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.9.6
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.9.6
125
+ - !ruby/object:Gem::Dependency
126
+ name: mocha
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 2.0.2
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 2.0.2
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.14.2
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.14.2
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.18.1
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 3.18.1
167
+ description: A client for using the ApiPattern API in Ruby. Built from their api documentation.
168
+ This is an unofficial project.
169
+ email:
170
+ - contact@jasonchalom.com
171
+ executables: []
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".github/workflows/ruby-ci.yml"
176
+ - ".gitignore"
177
+ - ".ruby-version"
178
+ - CODE_OF_CONDUCT.md
179
+ - Gemfile
180
+ - Gemfile.lock
181
+ - LICENSE
182
+ - LICENSE.txt
183
+ - README.md
184
+ - Rakefile
185
+ - api-pattern.gemspec
186
+ - bin/console
187
+ - bin/setup
188
+ - lib/api-pattern.rb
189
+ - lib/api-pattern/class_helpers.rb
190
+ - lib/api-pattern/client.rb
191
+ - lib/api-pattern/constants.rb
192
+ - lib/api-pattern/version.rb
193
+ homepage: https://github.com/TRex22/ApiPattern
194
+ licenses:
195
+ - MIT
196
+ metadata: {}
197
+ post_install_message:
198
+ rdoc_options: []
199
+ require_paths:
200
+ - lib
201
+ required_ruby_version: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ required_rubygems_version: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - ">="
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ requirements: []
212
+ rubygems_version: 3.4.10
213
+ signing_key:
214
+ specification_version: 4
215
+ summary: A client for using the ApiPattern API in Ruby.
216
+ test_files: []