sucharyruby 1.0.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/LICENSE +21 -0
- data/README.md +110 -0
- data/Rakefile +4 -0
- data/example/example.rb +25 -0
- data/lib/sucharyruby/version.rb +5 -0
- data/lib/sucharyruby.rb +83 -0
- data/sig/sucharyruby.rbs +4 -0
- metadata +48 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9663196acab2c9f57826c543809e4647c5bf6a289e25c3b5992bde463a19e40f
|
|
4
|
+
data.tar.gz: fb21ddae8fffd21afa363eaae47dd05f87b9dd8508aab3818c2f9cc6dc9b490f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: eb44365a90ebc3c6c3ef55a2f29f9f6a7aed9aab1e57d3fe5fa5a2eb0f87a430fa1e8f6d54929f5989730c9bb90ec5486c0bc162b3de0b71484aa75990db13f1
|
|
7
|
+
data.tar.gz: b32e8002226d10c301e560388a8091a7f654e03882756b42103807b9df49820596dd92b773efbd698f6ea4086d592a0a5696fdee852e92fc10e70f267a996174
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SucharyAPI
|
|
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,110 @@
|
|
|
1
|
+
# SucharyPL - oficjalna biblioteka bazy SucharyAPI
|
|
2
|
+
Rubyowa biblioteka do pobierania kawałów z ogólnopolskiej bazy SucharyAPI
|
|
3
|
+
|
|
4
|
+
[](https://www.ruby-lang.org/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 🚀 Instalacja
|
|
10
|
+
### Za pomocą gem
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
gem install sucharyruby
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Za pomocą Bundlera
|
|
17
|
+
|
|
18
|
+
(W pliku Gemfile)
|
|
19
|
+
```ruby
|
|
20
|
+
gem "sucharyruby"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
albo w nowym Bundlerze:
|
|
24
|
+
(komenda)
|
|
25
|
+
```bash
|
|
26
|
+
bundle add sucharyruby
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## ⚡ Szybki start
|
|
32
|
+
### Pobranie losowego suchara
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
require "sucharyruby"
|
|
36
|
+
|
|
37
|
+
suchar = Sucharyruby.dajKawal
|
|
38
|
+
puts suchar
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Pobranie suchara z konkretnej kategorii
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
require "sucharyruby"
|
|
45
|
+
|
|
46
|
+
# Możesz użyć ciągu znaków:
|
|
47
|
+
suchar = Sucharyruby.dajKawal("programowanie")
|
|
48
|
+
|
|
49
|
+
# Albo użyć stałej z klasy Kategoria:
|
|
50
|
+
suchar = Sucharyruby.dajKawal(Sucharyruby::Kategoria::PROGRAMOWANIE)
|
|
51
|
+
|
|
52
|
+
puts suchar
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Kategorie
|
|
58
|
+
- `"programowanie"` - Suchary o programowaniu
|
|
59
|
+
- `"dwuznaczne"` - Suchary dwuznaczne
|
|
60
|
+
- `"szkolne"` - Suchary szkolne
|
|
61
|
+
- `"suchary"` - Ogólne suchary
|
|
62
|
+
- `"wulgarne"` - Suchary wulgarne
|
|
63
|
+
- `"czarnyhumor"` - Czarny humor
|
|
64
|
+
- `"inne"` - Inne kategorie
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## 🛡️ Obsługa błędów
|
|
69
|
+
|
|
70
|
+
Biblioteka rzuca sformalizowane wyjątki, które możesz łatwo obsługiwać:
|
|
71
|
+
|
|
72
|
+
### Dostępne wyjątki
|
|
73
|
+
|
|
74
|
+
- `Sucharyruby::SucharyError` - Bazowy wyjątek, rodzic wszystkich błędów z biblioteki
|
|
75
|
+
- `Sucharyruby::KategoriaError` - Nieprawidłowa kategoria
|
|
76
|
+
- `Sucharyruby::ConnectionError` - Problemy z połączeniem (brak internetu, timeout)
|
|
77
|
+
- `Sucharyruby::HTTPError` - Błędy serwera (np. 404 dla nieistniejącej kategorii)
|
|
78
|
+
- `Sucharyruby::TimeoutError` - Przekroczony limit czasu zapytania
|
|
79
|
+
|
|
80
|
+
### Przykład obsługi błędów
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
require "sucharyruby"
|
|
84
|
+
|
|
85
|
+
begin
|
|
86
|
+
suchar = Sucharyruby.dajKawal("programowanie")
|
|
87
|
+
puts suchar
|
|
88
|
+
rescue Sucharyruby::ConnectionError
|
|
89
|
+
puts "❌ Błąd połączenia - sprawdź połączenie internetowe!"
|
|
90
|
+
rescue Sucharyruby::HTTPError => e
|
|
91
|
+
puts "❌ Błąd serwera: #{e.status} - #{e.message}"
|
|
92
|
+
rescue Sucharyruby::KategoriaError => e
|
|
93
|
+
puts "❌ Nieprawidłowa kategoria: #{e.message}"
|
|
94
|
+
rescue Sucharyruby::SucharyError => e
|
|
95
|
+
puts "❌ Nieoczekiwany błąd: #{e.message}"
|
|
96
|
+
end
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## 📡 API
|
|
102
|
+
|
|
103
|
+
Biblioteka jest produktem oraz korzysta z oficjalnej bazy SucharyAPI:
|
|
104
|
+
🔗 https://sucharyapi.onrender.com
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## 📝 Licencja
|
|
109
|
+
|
|
110
|
+
MIT
|
data/Rakefile
ADDED
data/example/example.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
|
4
|
+
require "sucharyruby"
|
|
5
|
+
|
|
6
|
+
puts "=== SucharyPL - Przykłady Użycia ==="
|
|
7
|
+
|
|
8
|
+
# 1. Losowy suchar
|
|
9
|
+
puts "\n1. Losowy suchar:"
|
|
10
|
+
puts Sucharyruby.dajKawal
|
|
11
|
+
|
|
12
|
+
# 2. Suchar z kategorii za pomocą stałej
|
|
13
|
+
puts "\n2. Suchar z kategorii (programowanie):"
|
|
14
|
+
suchar_dev = Sucharyruby.dajKawal(Sucharyruby::Kategoria::PROGRAMOWANIE)
|
|
15
|
+
puts suchar_dev
|
|
16
|
+
|
|
17
|
+
# 3. Obsługa błędów
|
|
18
|
+
puts "\n3. Przykład obsługi błędnej kategorii:"
|
|
19
|
+
begin
|
|
20
|
+
Sucharyruby.dajKawal("nieznana_kategoria")
|
|
21
|
+
rescue Sucharyruby::KategoriaError => e
|
|
22
|
+
puts "Wyłapano błąd kategorii: #{e.message}"
|
|
23
|
+
rescue Sucharyruby::SucharyError => e
|
|
24
|
+
puts "Wyłapano ogólny błąd biblioteki: #{e.message}"
|
|
25
|
+
end
|
data/lib/sucharyruby.rb
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "json"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
require_relative "sucharyruby/version"
|
|
8
|
+
|
|
9
|
+
module Sucharyruby
|
|
10
|
+
# Kategorie
|
|
11
|
+
class Kategoria
|
|
12
|
+
PROGRAMOWANIE = "programowanie"
|
|
13
|
+
DWUZNACZNE = "dwuznaczne"
|
|
14
|
+
SZKOLNE = "szkolne"
|
|
15
|
+
SUCHARY = "suchary"
|
|
16
|
+
WULGARNE = "wulgarne"
|
|
17
|
+
CZARNYHUMOR = "czarnyhumor"
|
|
18
|
+
INNE = "inne"
|
|
19
|
+
|
|
20
|
+
WSZYSTKIE = [
|
|
21
|
+
PROGRAMOWANIE,
|
|
22
|
+
DWUZNACZNE,
|
|
23
|
+
SZKOLNE,
|
|
24
|
+
SUCHARY,
|
|
25
|
+
WULGARNE,
|
|
26
|
+
CZARNYHUMOR,
|
|
27
|
+
INNE
|
|
28
|
+
].freeze
|
|
29
|
+
|
|
30
|
+
def self.poprawna?(kategoria)
|
|
31
|
+
WSZYSTKIE.include?(kategoria.to_s)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Customowe błędy
|
|
36
|
+
class SucharyError < StandardError; end
|
|
37
|
+
class KategoriaError < SucharyError; end
|
|
38
|
+
class ConnectionError < SucharyError; end
|
|
39
|
+
class HTTPError < SucharyError
|
|
40
|
+
attr_reader :status
|
|
41
|
+
def initialize(status, wiadomosc)
|
|
42
|
+
@status = status
|
|
43
|
+
super(wiadomosc)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
class TimeoutError < SucharyError; end
|
|
47
|
+
|
|
48
|
+
# Główna funkcja
|
|
49
|
+
def self.dajKawal(kategoria = nil)
|
|
50
|
+
if kategoria
|
|
51
|
+
kat_str = kategoria.to_s.downcase
|
|
52
|
+
if not(Kategoria.poprawna?(kat_str))
|
|
53
|
+
raise KategoriaError, "Nieprawidłowa kategoria: #{kategoria}"
|
|
54
|
+
else
|
|
55
|
+
url = "https://sucharyapi.onrender.com/#{kat_str}"
|
|
56
|
+
end
|
|
57
|
+
else
|
|
58
|
+
url = "https://sucharyapi.onrender.com"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
begin
|
|
62
|
+
adres = URI.parse(url)
|
|
63
|
+
http = Net::HTTP.new(adres.host, adres.port)
|
|
64
|
+
http.use_ssl = (adres.scheme == "https")
|
|
65
|
+
http.open_timeout = 6.5
|
|
66
|
+
http.read_timeout = 6.5
|
|
67
|
+
odpowiedz = http.request(Net::HTTP::Get.new(adres))
|
|
68
|
+
if odpowiedz.is_a?(Net::HTTPSuccess)
|
|
69
|
+
dane = JSON.parse(odpowiedz.body)
|
|
70
|
+
zart = dane["tresc"]
|
|
71
|
+
return zart
|
|
72
|
+
else
|
|
73
|
+
raise HTTPError.new(odpowiedz.code.to_i, "Serwer zwrócił kod błędu #{odpowiedz.code}")
|
|
74
|
+
end
|
|
75
|
+
rescue Net::OpenTimeout, Net::ReadTimeout => e
|
|
76
|
+
raise TimeoutError, "Zapytanie przekroczyło limit czasu: #{e.message}"
|
|
77
|
+
rescue SocketError, Errno::ECONNREFUSED => e
|
|
78
|
+
raise ConnectionError, "Błąd połączenia z serwerem: #{e.message}"
|
|
79
|
+
rescue JSON::ParserError => e
|
|
80
|
+
raise SucharyError, "Otrzymano niepoprawny format danych z serwera: #{e.message}"
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
data/sig/sucharyruby.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sucharyruby
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- SucharyAPI
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
email:
|
|
13
|
+
- sucharyapi@gmail.com
|
|
14
|
+
executables: []
|
|
15
|
+
extensions: []
|
|
16
|
+
extra_rdoc_files: []
|
|
17
|
+
files:
|
|
18
|
+
- LICENSE
|
|
19
|
+
- README.md
|
|
20
|
+
- Rakefile
|
|
21
|
+
- example/example.rb
|
|
22
|
+
- lib/sucharyruby.rb
|
|
23
|
+
- lib/sucharyruby/version.rb
|
|
24
|
+
- sig/sucharyruby.rbs
|
|
25
|
+
homepage: https://github.com/SucharyAPI/sucharypl-ruby
|
|
26
|
+
licenses:
|
|
27
|
+
- MIT
|
|
28
|
+
metadata:
|
|
29
|
+
homepage_uri: https://github.com/SucharyAPI/sucharypl-ruby
|
|
30
|
+
source_code_uri: https://github.com/SucharyAPI/sucharypl-ruby
|
|
31
|
+
rdoc_options: []
|
|
32
|
+
require_paths:
|
|
33
|
+
- lib
|
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: 3.2.0
|
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
requirements: []
|
|
45
|
+
rubygems_version: 4.0.17
|
|
46
|
+
specification_version: 4
|
|
47
|
+
summary: Oficjalna biblioteka bazy SucharyAPI dla Ruby
|
|
48
|
+
test_files: []
|