easyllama-client 0.1.3 → 0.2.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: 9148ba219056e97f195875a8aec77ee9f3aa3b11d21356af8da2b18987dfd126
4
- data.tar.gz: c7fe8a6bc289ae159f72509dd5ed2a745517b867a57ec62913082c18c3320415
3
+ metadata.gz: e1ffbbb8a0986c23d5f09b3d8550629f028d2d97589ff462f134c5a4368fd8d8
4
+ data.tar.gz: 7be45f553af3491b6b5c15b7da1caaac5893259d3a8972b392e9aa2a1f445de1
5
5
  SHA512:
6
- metadata.gz: 19b32f437da95f1a010f89ccc3bf89f8938ef5838d4722cdf4f1634b66dff8775efc71168cacd8c6d02515f8b1e3e107bad7a96b316b70997cdb372afd73e424
7
- data.tar.gz: 0e82fb94ff4773260861dfe11361bf5ff89530489cc23927c40be159afb67a040a0e55732b08a858a05c209e4decd939bf3f66bed7dac001c806dc2317af1e62
6
+ metadata.gz: 629a934fa5ccfb8a8dad78ac51501844d3283c8ef670be906964e5aef6816594b9c2f468c55c92bdd6d56a2d47c67a42280d5d829ad839008898e1c0fde2351f
7
+ data.tar.gz: 7e8e4ecdc91a01704c4636a6e52f9331d7e5a902480441d7c435658cd75efa6d0acde4c1fe08dbf93737804f035f4520d8f490cc3cb7956f0a3f71fe135fceec
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyLlama
4
+ class Client
5
+ # This class provides methods for interacting with the Easy Llama API.
6
+ class Api
7
+ DEFAULT_URI_BASE = 'https://api.easyllama.com/api/'
8
+ DEFAULT_API_VERSION = 'v1'
9
+
10
+ # Initializes the API client.
11
+ #
12
+ # @param token [String] The token.
13
+ # @return [String] The token.
14
+ def initialize(token)
15
+ @token = token
16
+
17
+ super()
18
+ end
19
+
20
+ # Sends an HTTP request to the specified path.
21
+ #
22
+ # @param path [String] The path of the API endpoint.
23
+ # @param method [Symbol] The HTTP method (:get, :post, :put, :patch, :delete).
24
+ # @param body [Hash] The request body (optional).
25
+ # @return [Net::HTTPResponse] The HTTP response.
26
+ def send_request(path: nil, method: :get, body: nil)
27
+ raise ArgumentError, 'Please provide EasyLlama::Client.api_token' if @token.nil?
28
+
29
+ uri = build_uri(path)
30
+ request = build_request(uri, method, body)
31
+ execute_request(uri, request)
32
+ end
33
+
34
+ # Parses the response body.
35
+ # If the response is successful, returns the value for the key.
36
+ # If the response is unsuccessful, returns an error code and message.
37
+ #
38
+ # @param response [Net::HTTPResponse] The HTTP response.
39
+ # @param key [String] The key to retrieve from the response body.
40
+ # @return [Object] The empty hash, parsed response body, value corresponding to the key or error code and message.
41
+ def parse_response!(response, key = nil)
42
+ if response.is_a?(Net::HTTPSuccess)
43
+ return {} if response.body.nil?
44
+ return JSON.parse(response.body) if key.nil?
45
+
46
+ JSON.parse(response.body)[key]
47
+ else
48
+ { 'code' => response.code, 'error' => response.message }
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def build_uri(path)
55
+ URI("#{DEFAULT_URI_BASE}#{DEFAULT_API_VERSION}#{path}")
56
+ end
57
+
58
+ def build_request(uri, method, body)
59
+ request = http_method_class(method).new(uri)
60
+ request['content-type'] = 'application/json'
61
+ request['accept'] = 'application/json'
62
+ request['authorization'] = "Bearer #{@token}"
63
+ request.body = body.to_json if body.present?
64
+ request
65
+ end
66
+
67
+ def execute_request(uri, request)
68
+ http = Net::HTTP.new(uri.host, uri.port)
69
+ http.use_ssl = true
70
+ http.request(request)
71
+ end
72
+
73
+ def http_method_class(method)
74
+ case method
75
+ when :get then Net::HTTP::Get
76
+ when :post then Net::HTTP::Post
77
+ when :put then Net::HTTP::Put
78
+ when :patch then Net::HTTP::Patch
79
+ when :delete then Net::HTTP::Delete
80
+ else raise ArgumentError, "Http method #{method} not recognized in EasyLlama::Client"
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module EasyLlama
4
4
  module Client
5
- VERSION = '0.1.3'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
@@ -3,76 +3,28 @@
3
3
  require 'net/http'
4
4
  require 'json'
5
5
 
6
+ require_relative 'api'
7
+ require_relative 'oauth'
6
8
  require_relative 'locations'
7
9
  require_relative 'learners'
8
10
  require_relative 'trainings'
9
11
 
10
12
  module EasyLlama
11
- # This class provides methods for interacting with the Easy Llama API.
13
+ # This class provides methods for interacting with the Easy Llama API interface.
12
14
  class Client
13
15
  class << self
14
- API_ROOT = 'https://api.easyllama.com/api/v1'
16
+ attr_accessor :api_token
15
17
 
16
- # Sends an HTTP request to the specified path.
17
- #
18
- # @param path [String] The path of the API endpoint.
19
- # @param method [Symbol] The HTTP method (:get, :post, :put, :patch, :delete).
20
- # @param body [Hash] The request body (optional).
21
- # @return [Net::HTTPResponse] The HTTP response.
22
- def send_request(path: nil, method: :get, body: nil)
23
- uri = build_uri(path)
24
- request = build_request(uri, method, body)
25
- execute_request(uri, request)
18
+ def locations
19
+ Locations.new(api_token)
26
20
  end
27
21
 
28
- # Parses the response body.
29
- # If the response is successful, returns the value for the key.
30
- # If the response is unsuccessful, returns an error code and message.
31
- #
32
- # @param response [Net::HTTPResponse] The HTTP response.
33
- # @param key [String] The key to retrieve from the response body.
34
- # @return [Object] The empty hash, parsed response body, value corresponding to the key or error code and message.
35
- def parse_response(response, key = nil)
36
- if response.is_a?(Net::HTTPSuccess)
37
- return {} if response.body.nil?
38
- return JSON.parse(response.body) if key.nil?
39
-
40
- JSON.parse(response.body)[key]
41
- else
42
- { 'code' => response.code, 'error' => response.message }
43
- end
44
- end
45
-
46
- private
47
-
48
- def build_uri(path)
49
- URI("#{API_ROOT}#{path}")
50
- end
51
-
52
- def build_request(uri, method, body)
53
- request = http_method_class(method).new(uri)
54
- request['content-type'] = 'application/json'
55
- request['accept'] = 'application/json'
56
- request['authorization'] = "Bearer #{ENV['EASY_LLAMA_API_TOKEN']}"
57
- request.body = body.to_json if body.present?
58
- request
59
- end
60
-
61
- def execute_request(uri, request)
62
- http = Net::HTTP.new(uri.host, uri.port)
63
- http.use_ssl = true
64
- http.request(request)
22
+ def learners
23
+ Learners.new(api_token)
65
24
  end
66
25
 
67
- def http_method_class(method)
68
- case method
69
- when :get then Net::HTTP::Get
70
- when :post then Net::HTTP::Post
71
- when :put then Net::HTTP::Put
72
- when :patch then Net::HTTP::Patch
73
- when :delete then Net::HTTP::Delete
74
- else raise ArgumentError, "Http method #{method} not recognized in EasyLlama::Client"
75
- end
26
+ def trainings
27
+ Trainings.new(api_token)
76
28
  end
77
29
  end
78
30
  end
@@ -3,72 +3,70 @@
3
3
  module EasyLlama
4
4
  class Client
5
5
  # This class provides methods for interacting with the Easy Llama API for learners.
6
- class Learners
7
- class << self
8
- # Sends a GET request to retrieve all learners.
9
- #
10
- # @return [Object] The learners or an error message.
11
- def all
12
- response = EasyLlama::Client.send_request(path: '/learners')
6
+ class Learners < Api
7
+ # Sends a GET request to retrieve all learners.
8
+ #
9
+ # @return [Object] The learners or an error message.
10
+ def all
11
+ response = send_request(path: '/learners')
13
12
 
14
- EasyLlama::Client.parse_response(response, 'learners')
15
- end
13
+ parse_response!(response, 'learners')
14
+ end
16
15
 
17
- # Sends a GET request to retrieve a learner by ID.
18
- #
19
- # @param id [Integer] The ID of the learner.
20
- # @return [Object] The learner or an error message.
21
- def find(id)
22
- response = EasyLlama::Client.send_request(path: "/learners/#{id}")
16
+ # Sends a GET request to retrieve a learner by ID.
17
+ #
18
+ # @param id [Integer] The ID of the learner.
19
+ # @return [Object] The learner or an error message.
20
+ def find(id)
21
+ response = send_request(path: "/learners/#{id}")
23
22
 
24
- EasyLlama::Client.parse_response(response, 'learner')
25
- end
23
+ parse_response!(response, 'learner')
24
+ end
26
25
 
27
- # Sends a POST request to create a learner.
28
- #
29
- # @param learner_attributes [Hash] The attributes of the learner.
30
- # @return [Object] The created learner or an error message.
31
- def create(learner_attributes = {})
32
- response = EasyLlama::Client.send_request(path: '/learners', method: :post, body: learner_attributes)
26
+ # Sends a POST request to create a learner.
27
+ #
28
+ # @param learner_attributes [Hash] The attributes of the learner.
29
+ # @return [Object] The created learner or an error message.
30
+ def create(learner_attributes = {})
31
+ response = send_request(path: '/learners', method: :post, body: learner_attributes)
33
32
 
34
- EasyLlama::Client.parse_response(response, 'learner')
35
- end
33
+ parse_response!(response, 'learner')
34
+ end
36
35
 
37
- # Sends a PATCH request to update a learner by ID.
38
- #
39
- # @param id [Integer] The ID of the learner.
40
- # @param learner_attributes [Hash] The attributes to update.
41
- # @return [Object] The updated learner or an error message.
42
- def update(id, learner_attributes = {})
43
- response = EasyLlama::Client.send_request(path: "/learners/#{id}", method: :patch, body: learner_attributes)
36
+ # Sends a PATCH request to update a learner by ID.
37
+ #
38
+ # @param id [Integer] The ID of the learner.
39
+ # @param learner_attributes [Hash] The attributes to update.
40
+ # @return [Object] The updated learner or an error message.
41
+ def update(id, learner_attributes = {})
42
+ response = send_request(path: "/learners/#{id}", method: :patch, body: learner_attributes)
44
43
 
45
- EasyLlama::Client.parse_response(response, 'learner')
46
- end
44
+ parse_response!(response, 'learner')
45
+ end
47
46
 
48
- # Sends a POST request to assign a training to a learner.
49
- #
50
- # @param training_id [Integer] The ID of the training.
51
- # @param learner_id [Integer] The ID of the learner.
52
- # @return [Object] The training assignment details or an error message.
53
- def assign_training_to_learner(training_id:, learner_id:)
54
- response = EasyLlama::Client.send_request(
55
- path: "/learners/#{learner_id}/learner_trainings",
56
- method: :post,
57
- body: { learner_id:, training_id: }
58
- )
47
+ # Sends a POST request to assign a training to a learner.
48
+ #
49
+ # @param training_id [Integer] The ID of the training.
50
+ # @param learner_id [Integer] The ID of the learner.
51
+ # @return [Object] The training assignment details or an error message.
52
+ def assign_training_to_learner(training_id:, learner_id:)
53
+ response = send_request(
54
+ path: "/learners/#{learner_id}/learner_trainings",
55
+ method: :post,
56
+ body: { learner_id:, training_id: }
57
+ )
59
58
 
60
- EasyLlama::Client.parse_response(response)
61
- end
59
+ parse_response!(response)
60
+ end
62
61
 
63
- # Sends a DELETE request to archive a learner by ID.
64
- #
65
- # @param id [Integer] The ID of the learner.
66
- # @return [Object] The updated learner or an error message.
67
- def archive(id)
68
- response = EasyLlama::Client.send_request(path: "/learners/#{id}", method: :delete)
62
+ # Sends a DELETE request to archive a learner by ID.
63
+ #
64
+ # @param id [Integer] The ID of the learner.
65
+ # @return [Object] The updated learner or an error message.
66
+ def archive(id)
67
+ response = send_request(path: "/learners/#{id}", method: :delete)
69
68
 
70
- EasyLlama::Client.parse_response(response)
71
- end
69
+ parse_response!(response)
72
70
  end
73
71
  end
74
72
  end
@@ -3,26 +3,24 @@
3
3
  module EasyLlama
4
4
  class Client
5
5
  # This class provides methods for interacting with the Easy Llama API for locations.
6
- class Locations
7
- class << self
8
- # Sends a GET request to retrieve all locations.
9
- #
10
- # @return [Object] The locations or an error message.
11
- def all
12
- response = EasyLlama::Client.send_request(path: '/locations')
6
+ class Locations < Api
7
+ # Sends a GET request to retrieve all locations.
8
+ #
9
+ # @return [Object] The locations or an error message.
10
+ def all
11
+ response = send_request(path: '/locations')
13
12
 
14
- EasyLlama::Client.parse_response(response, 'locations')
15
- end
13
+ parse_response!(response, 'locations')
14
+ end
16
15
 
17
- # Sends a POST request to create a location.
18
- #
19
- # @param location_attributes [Hash] The attributes of the location.
20
- # @return [Object] The created location or an error message.
21
- def create(location_attributes = {})
22
- response = EasyLlama::Client.send_request(path: '/locations', method: :post, body: location_attributes)
16
+ # Sends a POST request to create a location.
17
+ #
18
+ # @param location_attributes [Hash] The attributes of the location.
19
+ # @return [Object] The created location or an error message.
20
+ def create(location_attributes = {})
21
+ response = send_request(path: '/locations', method: :post, body: location_attributes)
23
22
 
24
- EasyLlama::Client.parse_response(response, 'location')
25
- end
23
+ parse_response!(response, 'location')
26
24
  end
27
25
  end
28
26
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyLlama
4
+ class Client
5
+ # This class provides methods for interacting with the Easy Llama API for OAuth.
6
+ class OAuth < Api
7
+ # Initializes the OAuth client.
8
+ #
9
+ # @param client_id [String] The client ID.
10
+ # @param client_secret [String] The client secret.
11
+ # @return [Object] The Easy Llama API client.
12
+ def initialize(client_id, client_secret)
13
+ access_token = get_access_token(client_id, client_secret)
14
+
15
+ EasyLlama::Client.api_token = access_token
16
+
17
+ super access_token
18
+ end
19
+
20
+ private
21
+
22
+ def get_access_token(client_id, client_secret)
23
+ response = send_request(
24
+ path: '/oauth/token',
25
+ method: :post,
26
+ body: {
27
+ client_id:,
28
+ client_secret:,
29
+ grant_type: 'client_credentials'
30
+ }
31
+ )
32
+
33
+ parse_response!(response, 'access_token')
34
+ end
35
+ end
36
+ end
37
+ end
@@ -3,16 +3,14 @@
3
3
  module EasyLlama
4
4
  class Client
5
5
  # This class provides methods for interacting with the Easy Llama API for trainings.
6
- class Trainings
7
- class << self
8
- # Sends a GET request to retrieve all trainings.
9
- #
10
- # @return [Object] The trainings or an error message.
11
- def all
12
- response = EasyLlama::Client.send_request(path: '/trainings')
6
+ class Trainings < Api
7
+ # Sends a GET request to retrieve all trainings.
8
+ #
9
+ # @return [Object] The trainings or an error message.
10
+ def all
11
+ response = send_request(path: '/trainings')
13
12
 
14
- EasyLlama::Client.parse_response(response, 'trainings')
15
- end
13
+ parse_response!(response, 'trainings')
16
14
  end
17
15
  end
18
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easyllama-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitalii Kashoid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-16 00:00:00.000000000 Z
11
+ date: 2024-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -45,10 +45,12 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - lib/easyllama/api.rb
48
49
  - lib/easyllama/client.rb
49
50
  - lib/easyllama/client/version.rb
50
51
  - lib/easyllama/learners.rb
51
52
  - lib/easyllama/locations.rb
53
+ - lib/easyllama/oauth.rb
52
54
  - lib/easyllama/trainings.rb
53
55
  homepage: https://rubygems.org/gems/easyllama-client
54
56
  licenses: