easyllama-client 0.1.0
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.
- checksums.yaml +7 -0
- data/lib/easyllama/client/version.rb +7 -0
- data/lib/easyllama/client.rb +80 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2272c4bd3d6e675e1c5c6a768f156c7624c1bfb6a0c283ce02dc76af4b980c7f
|
4
|
+
data.tar.gz: 419953966eeb58db9b83be9e4de547a581aef9c18740d93b54c2db8184bdd947
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 981cfe3b3436caa78ae65e62ab40635a8de7f2f896ee25cb40e25dc3b4baac684700ce4f3fb00372bc3756e16a6aeb8dab48f7c73bdfd3543a0f6d9195cd4a6c
|
7
|
+
data.tar.gz: 488d7e16f922ea4e8b34fde90124b09bcf67e4f2b828a6181166393b8845351d0750bc693ee732d00a2bde82f192ecc9bfd1b0c128ffdf02150e85a0560fc0d8
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module EasyLlama
|
7
|
+
# This class provides methods for interacting with the Easy Llama API.
|
8
|
+
class Client
|
9
|
+
class << self
|
10
|
+
API_ROOT = 'https://api.easyllama.com/api/v1'
|
11
|
+
|
12
|
+
# Sends an HTTP request to the specified path.
|
13
|
+
#
|
14
|
+
# @param path [String] The path of the API endpoint.
|
15
|
+
# @param method [Symbol] The HTTP method (:get, :post, :put, :patch, :delete).
|
16
|
+
# @param body [Hash] The request body (optional).
|
17
|
+
# @return [Net::HTTPResponse] The HTTP response.
|
18
|
+
def send_request(path: nil, method: :get, body: nil)
|
19
|
+
uri = build_uri(path)
|
20
|
+
request = build_request(uri, method, body)
|
21
|
+
execute_request(uri, request)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Parses the response body and returns the value corresponding to the provided key.
|
25
|
+
# If the response is successful, returns the value for the key.
|
26
|
+
# If the response is unsuccessful, returns an error message.
|
27
|
+
#
|
28
|
+
# @param response [Net::HTTPResponse] The HTTP response.
|
29
|
+
# @param key [String] The key to retrieve from the response body.
|
30
|
+
# @return [Object] The value corresponding to the key or an error message.
|
31
|
+
def response_body(response, key)
|
32
|
+
if response.is_a?(Net::HTTPSuccess)
|
33
|
+
JSON.parse(response.body)[key]
|
34
|
+
else
|
35
|
+
{ 'error' => response.message }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Sends a GET request to retrieve locations.
|
40
|
+
#
|
41
|
+
# @return [Object] The locations or an error message.
|
42
|
+
def locations
|
43
|
+
response = send_request(path: '/locations')
|
44
|
+
response_body(response, 'locations')
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def build_uri(path)
|
50
|
+
URI("#{API_ROOT}#{path}")
|
51
|
+
end
|
52
|
+
|
53
|
+
def build_request(uri, method, body)
|
54
|
+
request = http_method_class(method).new(uri)
|
55
|
+
request['content-type'] = 'application/json'
|
56
|
+
request['accept'] = 'application/json'
|
57
|
+
request['authorization'] = "Bearer #{ENV['EASY_LLAMA_API_TOKEN']}"
|
58
|
+
request.body = body.to_json if body.present?
|
59
|
+
request
|
60
|
+
end
|
61
|
+
|
62
|
+
def execute_request(uri, request)
|
63
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
64
|
+
http.use_ssl = true
|
65
|
+
http.request(request)
|
66
|
+
end
|
67
|
+
|
68
|
+
def http_method_class(method)
|
69
|
+
case method
|
70
|
+
when :get then Net::HTTP::Get
|
71
|
+
when :post then Net::HTTP::Post
|
72
|
+
when :put then Net::HTTP::Put
|
73
|
+
when :patch then Net::HTTP::Patch
|
74
|
+
when :delete then Net::HTTP::Delete
|
75
|
+
else raise ArgumentError, "Http method #{method} not recognized in EasyLlama::Client"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easyllama-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vitalii Kashoid
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-03-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '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'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: net-http
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A Ruby gem for Easy Llama API client.
|
42
|
+
email:
|
43
|
+
- kashoyid@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/easyllama/client.rb
|
49
|
+
- lib/easyllama/client/version.rb
|
50
|
+
homepage: https://rubygems.org/gems/easyllama-client
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata:
|
54
|
+
source_code_uri: https://github.com/Kashoid23/easyllama-client.git
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 3.1.4
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubygems_version: 3.3.26
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: A Ruby gem for Easy Llama API client.
|
74
|
+
test_files: []
|