act_as_api_client 0.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 585bfe652e889c7eaebcd2dfefd0a0c295ae87a390f9522de24fc2126e9011b3
4
- data.tar.gz: 5d61e33b3f9702b1bbcc2bac97913278dcbcfc6e7db780e0ea4b1a1efdfb14cb
3
+ metadata.gz: a90b4aea75a3d4fffe79dc9d882cc6560c464409cfa07aa9227806573cef5055
4
+ data.tar.gz: 70429d7b213697175b2283bfa2aabca4b019a7c7272b097e34fa9786904ae6cd
5
5
  SHA512:
6
- metadata.gz: e2cc7b10a33b1d6955b6aad98709aa58ddba78e38e7804ab6f8b4b8be49ce4ac4b93077cdfa0a9f81e6ea5501784ce02d0ff278228affb4a33a23d71ed52adbe
7
- data.tar.gz: c3926de463c5fef33ce98c7158165cffe8fb19d5eef4a94b72fc14fbc51b9a0966134845d899a23310e26b786326a1fe9ee1f058390f7c19809efefa43f7b822
6
+ metadata.gz: b2f6b525f14bf39b4463d40cba93aa2cf84d98b92e5e5a77ddc9bc34cb8ba8f02a5e098d38788657317b6b5f0774a22f5345de6338e4b47e2548896b9216c794
7
+ data.tar.gz: 64a64300293bf60782357e32c7909dc8d01a95e47e22c6df36226f6278d4dc941449d38d6fc135e42620ecf7efa9a8a874dbad4d523e175cba22a90f7c762c99
@@ -0,0 +1,11 @@
1
+ {
2
+ "dockerComposeFile": "./docker-compose.yml",
3
+ "service": "app",
4
+ "customizations": {
5
+ "vscode": {
6
+ "extensions": [
7
+ "ms-azuretools.vscode-docker"
8
+ ]
9
+ }
10
+ }
11
+ }
@@ -9,9 +9,9 @@ name: Ruby
9
9
 
10
10
  on:
11
11
  push:
12
- branches: [ main ]
12
+ branches: [main]
13
13
  pull_request:
14
- branches: [ main ]
14
+ branches: [main]
15
15
 
16
16
  permissions:
17
17
  contents: read
@@ -21,7 +21,7 @@ jobs:
21
21
  runs-on: ubuntu-latest
22
22
  strategy:
23
23
  matrix:
24
- ruby-version: ['3.0']
24
+ ruby-version: ["3.0"]
25
25
  steps:
26
26
  - uses: actions/checkout@v3
27
27
  - name: Set up Ruby
@@ -35,15 +35,16 @@ jobs:
35
35
  runs-on: ubuntu-latest
36
36
  strategy:
37
37
  matrix:
38
- ruby-version: ['2.6', '2.7', '3.0']
38
+ os: [ubuntu-latest, macos-latest]
39
+ ruby-version: ["2.6", "2.7", "3.0", "3.1", "3.2", "3.3"]
39
40
  steps:
40
41
  - uses: actions/checkout@v3
41
42
  - name: Set up Ruby
42
- uses: ruby/setup-ruby@2b019609e2b0f1ea1a2bc8ca11cb82ab46ada124
43
+ uses: ruby/setup-ruby@v1
43
44
  with:
44
45
  ruby-version: ${{ matrix.ruby-version }}
45
- bundler-cache: true # runs 'bundle install' and caches installed gems automatically
46
+ bundler-cache: true
46
47
  - name: Copy credentials file for CI
47
48
  run: mv spec/credentials.example.yml spec/credentials.yml
48
49
  - name: Run tests
49
- run: bundle exec rspec
50
+ run: bundle exec rspec
data/.gitignore CHANGED
@@ -14,4 +14,6 @@
14
14
  .byebug_history
15
15
  spec/credentials.yml
16
16
 
17
- *.gem
17
+ *.gem
18
+
19
+ .DS_Store
data/.rubocop.yml CHANGED
@@ -16,7 +16,12 @@ Layout/LineLength:
16
16
 
17
17
  AllCops:
18
18
  NewCops: enable
19
- TargetRubyVersion: 2.4
19
+ TargetRubyVersion: 2.6
20
20
 
21
21
  Style/Documentation:
22
- Enabled: false
22
+ Enabled: false
23
+
24
+ Metrics/BlockLength:
25
+ Exclude:
26
+ - 'spec/**/*'
27
+ - 'act_as_api_client.gemspec'
data/.rubocop_todo.yml CHANGED
@@ -10,61 +10,47 @@
10
10
  # Configuration parameters: IgnoredMethods, Max.
11
11
  Metrics/AbcSize:
12
12
  Exclude:
13
- - 'lib/act_as_api_client/clients/github_client.rb'
14
-
15
- # Offense count: 5
16
- # Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods.
17
- # ExcludedMethods: refine
18
- Metrics/BlockLength:
19
- Exclude:
20
- - '**/*.gemspec'
21
- - 'spec/act_as_api_client/clients/authorize_net/authorize_net_notifications_client_spec.rb'
22
- - 'spec/act_as_api_client/clients/github_client/find_by_spec.rb'
23
- - 'spec/act_as_api_client/clients/github_client/find_spec.rb'
24
- - 'spec/act_as_api_client/clients/github_client/where_spec.rb'
13
+ - "lib/act_as_api_client/clients/github_repositories_client.rb"
25
14
 
26
15
  # Offense count: 1
27
16
  # Configuration parameters: IgnoredMethods, Max.
28
17
  Metrics/CyclomaticComplexity:
29
18
  Exclude:
30
- - 'lib/act_as_api_client/clients/github_client.rb'
19
+ - "lib/act_as_api_client/clients/github_repositories_client.rb"
31
20
 
32
21
  # Offense count: 2
33
22
  # Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods.
34
23
  Metrics/MethodLength:
35
24
  Exclude:
36
- - 'lib/act_as_api_client/clients/github_client.rb'
37
- - 'lib/act_as_api_client/clients/http_client.rb'
25
+ - "lib/act_as_api_client/clients/github_repositories_client.rb"
26
+ - "lib/act_as_api_client/clients/http_client.rb"
38
27
 
39
28
  # Offense count: 2
40
29
  Naming/AccessorMethodName:
41
30
  Exclude:
42
- - 'lib/act_as_api_client.rb'
31
+ - "lib/act_as_api_client.rb"
43
32
 
44
33
  # Offense count: 1
45
34
  # Configuration parameters: Max.
46
35
  RSpec/ExampleLength:
47
36
  Exclude:
48
- - 'spec/act_as_api_client/clients/http_client_spec.rb'
37
+ - "spec/act_as_api_client/clients/http_client_spec.rb"
49
38
 
50
39
  # Offense count: 2
51
40
  # Configuration parameters: CustomTransform, IgnoreMethods, SpecSuffixOnly.
52
41
  RSpec/FilePath:
53
42
  Exclude:
54
- - 'spec/act_as_api_client/clients/authorize_net/authorize_net_notifications_client_spec.rb'
55
- - 'spec/act_as_api_client/clients/authorize_net/authorize_net_webhooks_client_spec.rb'
43
+ - "spec/act_as_api_client/clients/authorize_net/authorize_net_notifications_client_spec.rb"
44
+ - "spec/act_as_api_client/clients/authorize_net/authorize_net_webhooks_client_spec.rb"
45
+ - "spec/act_as_api_client/clients/github_client/find_by_spec.rb"
46
+ - "spec/act_as_api_client/clients/github_client/find_spec.rb"
47
+ - "spec/act_as_api_client/clients/github_client/where_spec.rb"
56
48
 
57
49
  # Offense count: 3
58
50
  # Configuration parameters: Max.
59
51
  RSpec/NestedGroups:
60
52
  Exclude:
61
- - 'spec/act_as_api_client/clients/github_client/find_by_spec.rb'
62
-
63
- # Offense count: 1
64
- # Configuration parameters: MinBodyLength.
65
- Style/GuardClause:
66
- Exclude:
67
- - 'lib/act_as_api_client/clients/authorize_net_webhooks_client.rb'
53
+ - "spec/act_as_api_client/clients/github_client/find_by_spec.rb"
68
54
 
69
55
  # Offense count: 4
70
56
  # Cop supports --auto-correct.
@@ -72,5 +58,5 @@ Style/GuardClause:
72
58
  # URISchemes: http, https
73
59
  Layout/LineLength:
74
60
  Exclude:
75
- - 'lib/act_as_api_client/clients/github_client.rb'
76
- - 'spec/support/vcr_setup.rb'
61
+ - "lib/act_as_api_client/clients/github_repositories_client.rb"
62
+ - "spec/support/vcr_setup.rb"
data/Dockerfile ADDED
@@ -0,0 +1,3 @@
1
+ FROM ruby:2.6-bullseye
2
+ COPY Gemfile Gemfile.lock act_as_api_client.gemspec .
3
+ RUN bundle install
data/Gemfile CHANGED
@@ -4,11 +4,3 @@ source "https://rubygems.org"
4
4
 
5
5
  # Specify your gem's dependencies in act_as_api_client.gemspec
6
6
  gemspec
7
-
8
- gem "byebug", "~> 11.1"
9
- gem "rake", "~> 13.0"
10
- gem "rspec", "~> 3.0"
11
- gem "rubocop", "~> 0.80"
12
- gem "rubocop-rspec", require: false
13
- gem "vcr", "~> 6.1"
14
- gem "webmock", "~> 3.14"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- act_as_api_client (0.2.0)
4
+ act_as_api_client (1.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -68,7 +68,4 @@ DEPENDENCIES
68
68
  rubocop (~> 0.80)
69
69
  rubocop-rspec
70
70
  vcr (~> 6.1)
71
- webmock (~> 3.14)
72
-
73
- BUNDLED WITH
74
- 2.2.3
71
+ webmock (~> 3.14)
data/README.md CHANGED
@@ -1,11 +1,9 @@
1
1
  # act_as_api_client
2
2
 
3
- How to create api clients for your application? What is a better way to encapsualte interactions with third APIs? My answer is `act_as_api_client`.
3
+ How to create api clients for your application? What is a better way to encapsualte interactions with third party APIs? My answer is `act_as_api_client`.
4
4
 
5
5
  Let's assume you have a typical Rails or any ruby application and want to play around with an API, Github for example. See the [Usage](https://github.com/Rukomoynikov/act_as_api_client#usage) section to find how to use existing preconfigured API's and encapsulate all logic inside `APIClient` classes.
6
6
 
7
- _At the moment i experiment in order to make api clients behavior very similiar to `ActiveRecord` models, so out of the box most of the clients support these methods: find, where, delete, update, find_by, create, update_
8
-
9
7
  ## Installation
10
8
 
11
9
  Add this line to your application's Gemfile:
@@ -25,14 +23,14 @@ Or install it yourself as:
25
23
  ## Usage
26
24
 
27
25
  ### Folder for clients:
28
- Create a folder in your app for examples `api_clients` inside your `lib` directory. This will be the place where you will keep all... api clients.
26
+ Create a folder in your app for examples `api_clients`. This will be the place where you will keep all servies responsible for communication with external HTTP APIs.
29
27
 
30
28
  ### Create API client class:
31
- For example you want to fetch and update Github repositoties, then you class may have a form like this:
29
+ For example, you need to fetch and update Github repositories, then you class may have a form like this:
32
30
 
33
31
  ```ruby
34
32
  class GithubClient < ApiClient
35
- act_as_api_client for: :github
33
+ act_as_api_client for: :github_repositories
36
34
  end
37
35
  ```
38
36
 
@@ -40,9 +38,9 @@ In case you want to provide and use authorization token for Github:
40
38
 
41
39
  ```ruby
42
40
  class GithubClient < ApiClient
43
- act_as_api_client for: :github,
41
+ act_as_api_client for: :github_repositories,
44
42
  with: {
45
- token: <your_token>
43
+ token: <your_token> # ENV variable of secret
46
44
  }
47
45
  end
48
46
  ```
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/act_as_api_client/version"
4
-
5
3
  Gem::Specification.new do |spec|
6
4
  spec.name = "act_as_api_client"
7
- spec.version = ActAsApiClient::VERSION
5
+ spec.version = "1.3.0"
8
6
  spec.authors = ["Max Rukomoynikov"]
9
7
  spec.email = ["rukomoynikov@gmail.com"]
10
8
 
@@ -12,7 +10,7 @@ Gem::Specification.new do |spec|
12
10
  spec.description = "Helps you to build reliable API clients in a minute. Just add act_as_api_client to your classes"
13
11
  spec.homepage = "https://rubygems.org/gems/act_as_api_client"
14
12
  spec.license = "MIT"
15
- spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
16
14
 
17
15
  spec.metadata = {
18
16
  "source_code_uri" => "https://github.com/Rukomoynikov/act_as_api_client",
@@ -29,6 +27,13 @@ Gem::Specification.new do |spec|
29
27
  spec.require_paths = ["lib"]
30
28
 
31
29
  # Uncomment to register a new dependency of your gem
30
+ spec.add_development_dependency "byebug", "~> 11.1"
31
+ spec.add_development_dependency "rake", "~> 13.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "rubocop", "~> 0.80"
34
+ spec.add_development_dependency "rubocop-rspec"
35
+ spec.add_development_dependency "vcr", "~> 6.1"
36
+ spec.add_development_dependency "webmock", "~> 3.14"
32
37
 
33
38
  # For more information and examples about making a new gem, checkout our
34
39
  # guide at: https://bundler.io/guides/creating_gem.html
data/demo.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require_relative "lib/act_as_api_client"
4
4
 
5
5
  class GithubClient < ApiClient
6
- act_as_api_client for: :github
6
+ act_as_api_client for: :github_repositories
7
7
  end
8
8
 
9
9
  p GithubClient.ancestors
@@ -0,0 +1,9 @@
1
+ services:
2
+ app:
3
+ build:
4
+ dockerfile: Dockerfile
5
+ working_dir: /app
6
+ volumes:
7
+ - ./:/app
8
+ tty: true
9
+ stdin_open: true
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Run it with `ruby anthropic_playground.rb`
4
+ # See Anthropic development API documentation on https://docs.anthropic.com/en/api/messages
5
+
6
+ require "bundler/inline"
7
+
8
+ gemfile(true) do
9
+ source "https://rubygems.org"
10
+
11
+ gem "act_as_api_client", path: "../"
12
+ end
13
+
14
+ require "act_as_api_client"
15
+
16
+ class AnthropicClient < ApiClient
17
+ act_as_api_client for: %i[anthropic messages],
18
+ with: { x_api_key: ENV["ANTHROPIC_API_KEY"] }
19
+ end
20
+
21
+ anthropic_client = AnthropicClient.new
22
+
23
+ pp anthropic_client.create(
24
+ model: "claude-3-5-sonnet-20241022",
25
+ messages: [
26
+ { "role": "user", "content": "Hello there." },
27
+ { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" },
28
+ { "role": "user", "content": "Can you explain LLMs in plain English?" }
29
+ ],
30
+ max_tokens: 1024
31
+ )
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Run it with `ruby github_repositories.rb`
4
+
5
+ require "bundler/inline"
6
+
7
+ gemfile(true) do
8
+ source "https://rubygems.org"
9
+
10
+ gem "act_as_api_client", path: "./"
11
+ gem "tabled"
12
+ end
13
+
14
+ require "act_as_api_client"
15
+
16
+ class GithubApiClient < ApiClient
17
+ act_as_api_client for: :github_repositories
18
+ end
19
+
20
+ github_api_client = GithubApiClient.new
21
+ pp github_api_client.find("Rukomoynikov/tabled")
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActAsApiClient
4
+ class ClientLoader
5
+ attr_reader :client_for
6
+
7
+ def initialize(client_for:)
8
+ @client_for = client_for
9
+ end
10
+
11
+ def class_name
12
+ case client_for.class.to_s
13
+ when "String", "Symbol"
14
+ "#{convert_underscore_to_camelcase(client_for)}Client"
15
+ when "Array"
16
+ client_for
17
+ .map { |item| convert_underscore_to_camelcase(item) }
18
+ .join("::")
19
+ .concat("Client")
20
+ end
21
+ end
22
+
23
+ def path
24
+ case client_for.class.to_s
25
+ when "String", "Symbol"
26
+ File.expand_path("clients/#{client_for}_client", File.dirname(__FILE__))
27
+ when "Array"
28
+ client_path = client_for.join("/")
29
+
30
+ File.expand_path("clients/#{client_path}_client", File.dirname(__FILE__))
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ # Converting from authorize_net_webhooks_client to AuthorizeNetWebhooksClient
37
+ #
38
+ # @param value [String] string with underscores
39
+ # @return [String] transformed in camel case format string
40
+ def convert_underscore_to_camelcase(value)
41
+ value.to_s.capitalize.gsub(/_(.)/) do |s|
42
+ s.upcase.gsub("_", "")
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../http_client"
4
+
5
+ # See the documentation on https://docs.anthropic.com/en/api/messages
6
+
7
+ module ActAsApiClient
8
+ module Clients
9
+ module Anthropic
10
+ module MessagesClient
11
+ ANTHROPIC_VERSION = "2023-06-01"
12
+ BASE_URL = "https://api.anthropic.com/v1/messages"
13
+
14
+ include HttpClient
15
+
16
+ # Sends request to Anthropic API https://docs.anthropic.com/en/api/messages
17
+ #
18
+ # @param model [String] The model that will complete your prompt
19
+ # @param messages [Array] Input messages
20
+ # @param max_tokens [Integer] The maximum number of tokens to generate before stopping
21
+ # @param metadata [Hash] An object describing metadata about the request
22
+ # @param stop_sequences [Array<String>] Custom text sequences that will cause the model to stop generating
23
+ # @param stream [Boolean] Whether to incrementally stream the response using server-sent events
24
+ # @param system [String] System prompt
25
+ # @param temperature [Float] Amount of randomness injected into the response
26
+ # @param tool_choice [Hash] How the model should use the provided tools
27
+ # @param tools [Array<Hash>] Definitions of tools that the model may use
28
+ # @param top_k [Integer] Only sample from the top K options for each subsequent token
29
+ # @param top_p [Float] Use nucleus sampling
30
+ #
31
+ # @return [Hash] Response from Anthropic API
32
+ def create(**params)
33
+ post(BASE_URL, headers: headers, body: body(**params))
34
+ end
35
+
36
+ private
37
+
38
+ def headers
39
+ default = {
40
+ "Content-Type": "application/json",
41
+ "x-api-key" => options[:x_api_key],
42
+ "anthropic-version" => options[:anthropic_version] || ANTHROPIC_VERSION
43
+ }
44
+
45
+ default["anthropic-beta"] = options[:anthropic_beta] if options[:anthropic_beta]
46
+
47
+ default
48
+ end
49
+
50
+ # rubocop:disable Metrics/ParameterLists, Metrics/CyclomaticComplexity, Metrics/MethodLength
51
+ def body(model:,
52
+ messages:,
53
+ max_tokens:,
54
+ metadata: nil,
55
+ stop_sequences: nil,
56
+ stream: false,
57
+ system: nil,
58
+ temperature: 1.0,
59
+ tool_choice: nil,
60
+ tools: nil,
61
+ top_k: nil,
62
+ top_p: nil)
63
+ default = {
64
+ model: model,
65
+ messages: messages,
66
+ max_tokens: max_tokens,
67
+ stream: stream,
68
+ temperature: temperature
69
+ }
70
+
71
+ default[:metadata] = metadata if metadata
72
+ default[:stop_sequences] = stop_sequences if stop_sequences
73
+ default[:system] = system if system
74
+ default[:tool_choice] = tool_choice if tool_choice
75
+ default[:tools] = tools if tools
76
+ default[:top_k] = top_k if top_k
77
+ default[:top_p] = top_p if top_p
78
+
79
+ default
80
+ end
81
+ # rubocop:enable Metrics/ParameterLists, Metrics/CyclomaticComplexity, Metrics/MethodLength
82
+ end
83
+ end
84
+ end
85
+ end
@@ -4,7 +4,7 @@ require_relative "http_client"
4
4
 
5
5
  module ActAsApiClient
6
6
  module Clients
7
- module GithubClient
7
+ module GithubRepositoriesClient
8
8
  include HttpClient
9
9
 
10
10
  # Searches Github for one repository by it's owner and repository names.
@@ -30,7 +30,7 @@ module ActAsApiClient
30
30
  #
31
31
  # @example Reverse a string
32
32
  # class GithubClient < ApiClient
33
- # act_as_api_client for: :github
33
+ # act_as_api_client for: :github_repositories
34
34
  # end
35
35
  #
36
36
  # GithubClient.new.where('rails')
@@ -50,7 +50,7 @@ module ActAsApiClient
50
50
  #
51
51
  # @example Reverse a string
52
52
  # class GithubClient < ApiClient
53
- # act_as_api_client for: :github
53
+ # act_as_api_client for: :github_repositories
54
54
  # end
55
55
  #
56
56
  # GithubClient.new.find_by(organization: 'rails')
@@ -3,19 +3,22 @@
3
3
  require "net/http"
4
4
  require "json"
5
5
 
6
+ require "act_as_api_client/errors/invalid_response_error"
7
+ require "act_as_api_client/errors/network_error"
8
+
6
9
  module ActAsApiClient
7
10
  module Clients
8
11
  module HttpClient
9
12
  private
10
13
 
11
- def get(url, options = {})
14
+ def get(url, **options)
12
15
  # Request part
13
16
  uri = URI(url)
14
17
  uri.query = URI.encode_www_form(options.fetch(:params, {}))
15
18
 
16
19
  request = Net::HTTP::Get.new(uri)
17
- request_headers(headers: options.fetch(:headers, {}),
18
- request: request)
20
+ set_request_headers(headers: options.fetch(:headers, {}),
21
+ request: request)
19
22
 
20
23
  # Response part
21
24
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
@@ -28,8 +31,31 @@ module ActAsApiClient
28
31
  end
29
32
  end
30
33
 
31
- def post
32
- # HTTParty.post
34
+ def post(url, headers: {}, **options) # rubocop:disable Metrics/AbcSize
35
+ # Request part
36
+ uri = URI(url)
37
+ uri.query = URI.encode_www_form(options.fetch(:params, {}))
38
+
39
+ request = Net::HTTP::Post.new(uri)
40
+
41
+ request.body = options.fetch(:body, {}).to_json
42
+ set_request_headers(headers: headers, request: request)
43
+
44
+ # Response part
45
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
46
+ http.request(request)
47
+ end
48
+
49
+ case response
50
+ when Net::HTTPSuccess
51
+ ::JSON.parse(response.body)
52
+ when Net::HTTPNotFound, Net::HTTPUnprocessableEntity, Net::HTTPUnauthorized
53
+ ::JSON.parse(response.body)
54
+ end
55
+ rescue Net::OpenTimeout, Net::ReadTimeout, SocketError => e
56
+ raise ActAsApiClient::NetworkError, "Network error occurred: #{e.message}"
57
+ rescue JSON::ParserError => e
58
+ raise ActAsApiClient::InvalidResponseError, "Invalid JSON response: #{e.message}"
33
59
  end
34
60
 
35
61
  def put
@@ -44,7 +70,7 @@ module ActAsApiClient
44
70
  # HTTParty.delete
45
71
  end
46
72
 
47
- def request_headers(headers:, request:)
73
+ def set_request_headers(headers:, request:)
48
74
  headers.each do |key, value|
49
75
  request[key] = value
50
76
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActAsApiClient
4
+ module Errors
5
+ class InvalidResponseError < StandardError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActAsApiClient
4
+ module Errors
5
+ class NetworkError < StandardError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActAsApiClient
4
+ module Errors
5
+ class NonExistingClient < StandardError
6
+ def initialize(requested_client:)
7
+ @requested_client = requested_client
8
+ super
9
+ end
10
+
11
+ def message
12
+ "The requested client (#{@requested_client}) doesn't exist"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "act_as_api_client/version"
4
3
  require_relative "act_as_api_client/base_api_methods"
4
+ require "act_as_api_client/errors/non_existing_client_error"
5
+ require "act_as_api_client/client_loader"
5
6
 
6
7
  class ApiClient
7
8
  include ActAsApiClient::BaseApiMethods
@@ -17,26 +18,16 @@ class ApiClient
17
18
  def set_general_client(client_for:)
18
19
  return if client_for.nil?
19
20
 
20
- class_name = convert_underscore_to_camelcase(client_for)
21
+ client_loader = ActAsApiClient::ClientLoader.new(client_for: client_for)
21
22
 
22
- require(File.expand_path("act_as_api_client/clients/#{client_for}_client",
23
- File.dirname(__FILE__)))
24
- include const_get("ActAsApiClient::Clients::#{class_name}Client")
23
+ require(client_loader.path)
24
+ include const_get("ActAsApiClient::Clients::#{client_loader.class_name}")
25
+ rescue LoadError
26
+ raise ActAsApiClient::Errors::NonExistingClient.new(requested_client: client_for)
25
27
  end
26
28
 
27
29
  def set_options(options:)
28
30
  define_method("options") { options }
29
31
  end
30
-
31
- # Converting from authorize_net_webhooks_client to AuthorizeNetWebhooksClient
32
- #
33
- # @param value [String] string with underscores
34
- #
35
- # @return [String] transformed in camel case format string
36
- def convert_underscore_to_camelcase(value)
37
- value.to_s.capitalize.gsub(/_(.)/) do |s|
38
- s.upcase.gsub("_", "")
39
- end
40
- end
41
32
  end
42
33
  end
metadata CHANGED
@@ -1,15 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: act_as_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Rukomoynikov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-29 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '11.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '11.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.80'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.80'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '6.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '6.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.14'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.14'
13
111
  description: Helps you to build reliable API clients in a minute. Just add act_as_api_client
14
112
  to your classes
15
113
  email:
@@ -18,12 +116,14 @@ executables: []
18
116
  extensions: []
19
117
  extra_rdoc_files: []
20
118
  files:
119
+ - ".devcontainer.json"
21
120
  - ".github/workflows/main.yml"
22
121
  - ".gitignore"
23
122
  - ".rspec"
24
123
  - ".rubocop.yml"
25
124
  - ".rubocop_todo.yml"
26
125
  - CODE_OF_CONDUCT.md
126
+ - Dockerfile
27
127
  - Gemfile
28
128
  - Gemfile.lock
29
129
  - LICENSE.txt
@@ -33,13 +133,18 @@ files:
33
133
  - bin/console
34
134
  - bin/setup
35
135
  - demo.rb
136
+ - docker-compose.yml
137
+ - examples/anthropic_playground.rb
138
+ - examples/github_repositories.rb
36
139
  - lib/act_as_api_client.rb
37
140
  - lib/act_as_api_client/base_api_methods.rb
38
- - lib/act_as_api_client/clients/authorize_net_notifications_client.rb
39
- - lib/act_as_api_client/clients/authorize_net_webhooks_client.rb
40
- - lib/act_as_api_client/clients/github_client.rb
141
+ - lib/act_as_api_client/client_loader.rb
142
+ - lib/act_as_api_client/clients/anthropic/messages_client.rb
143
+ - lib/act_as_api_client/clients/github_repositories_client.rb
41
144
  - lib/act_as_api_client/clients/http_client.rb
42
- - lib/act_as_api_client/version.rb
145
+ - lib/act_as_api_client/errors/invalid_response_error.rb
146
+ - lib/act_as_api_client/errors/network_error.rb
147
+ - lib/act_as_api_client/errors/non_existing_client_error.rb
43
148
  homepage: https://rubygems.org/gems/act_as_api_client
44
149
  licenses:
45
150
  - MIT
@@ -48,7 +153,7 @@ metadata:
48
153
  homepage_uri: https://rubygems.org/gems/act_as_api_client
49
154
  documentation_uri: https://rubydoc.info/github/Rukomoynikov/act_as_api_client/main
50
155
  bug_tracker_uri: https://github.com/Rukomoynikov/act_as_api_client/issues
51
- post_install_message:
156
+ post_install_message:
52
157
  rdoc_options: []
53
158
  require_paths:
54
159
  - lib
@@ -56,15 +161,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
161
  requirements:
57
162
  - - ">="
58
163
  - !ruby/object:Gem::Version
59
- version: 2.4.0
164
+ version: 2.6.0
60
165
  required_rubygems_version: !ruby/object:Gem::Requirement
61
166
  requirements:
62
167
  - - ">="
63
168
  - !ruby/object:Gem::Version
64
169
  version: '0'
65
170
  requirements: []
66
- rubygems_version: 3.2.3
67
- signing_key:
171
+ rubygems_version: 3.0.3.1
172
+ signing_key:
68
173
  specification_version: 4
69
174
  summary: Collection of predefined API clients
70
175
  test_files: []
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "http_client"
4
- require_relative "./authorize_net_webhooks_client"
5
- require "base64"
6
-
7
- module ActAsApiClient
8
- module Clients
9
- module AuthorizeNetNotificationsClient
10
- include AuthorizeNetWebhooksClient
11
-
12
- def where(parameters = {})
13
- # GET https://apitest.authorize.net/rest/v1/notifications?from_date=2017-03-01&to_date=2017-03-13&offset=0&limit=100
14
- # GET https://apitest.authorize.net/rest/v1/notifications?deliveryStatus=Failed
15
- # [:from_date, :to_date, :offset, :limit, :deliveryStatus]
16
-
17
- get("https://#{base_uri}/rest/v1/notifications/",
18
- headers: { "Authorization" => auth },
19
- params: parameters)
20
- end
21
-
22
- def find(uuid)
23
- raise StandardError, "uuid is not provided" if uuid.empty?
24
-
25
- get("https://#{base_uri}/rest/v1/notifications/#{uuid}",
26
- headers: { "Authorization" => auth })
27
- end
28
- end
29
- end
30
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "http_client"
4
- require "base64"
5
-
6
- module ActAsApiClient
7
- module Clients
8
- module AuthorizeNetWebhooksClient
9
- include HttpClient
10
- DEFAULT_OPTIONS = {
11
- mode: :production
12
- }.freeze
13
-
14
- def initialize
15
- @options = DEFAULT_OPTIONS.merge(options)
16
- end
17
-
18
- def find(uuid)
19
- raise StandardError, "uuid is not provided" if uuid.empty?
20
-
21
- get("https://#{base_uri}/rest/v1/webhooks/#{uuid}",
22
- headers: { "Authorization" => auth })
23
- end
24
-
25
- private
26
-
27
- def base_uri
28
- if @options[:mode] == :test
29
- "apitest.authorize.net"
30
- else
31
- "authorize.net"
32
- end
33
- end
34
-
35
- def auth
36
- if options[:payment_user]
37
- "Basic #{Base64.strict_encode64("#{options[:payment_user]}:#{options[:payment_pass]}")}"
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActAsApiClient
4
- VERSION = "0.2.0"
5
- end