librix 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 54f6886443843a52f60a21dbd5ac37c50ed63ec003d32b219c6a65f04188df48
4
+ data.tar.gz: d1565db4b82797814be40d38c9238ec43100bedbf473b7adb7df39f429138986
5
+ SHA512:
6
+ metadata.gz: 8ab9f5bad198443a8867b32e866f3ab6662eeeacfa6157c0914553bbe810d73bd30a921b129669d17150e17df424511b22c12a08d862249ebeb4c0b5e5ff7e4d
7
+ data.tar.gz: daf011c366f864d4554a849478304ed9adef25568815ee754b2b4e80681c243b60caa88b66375b5b187ce37b6e09ab11dc888b8e12b70d1c80450c1f19f12639
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-10-16
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Vinicius Coelho
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,66 @@
1
+ # Librix
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/librix`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ ```bash
12
+ bundle add librix
13
+ ```
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ ```bash
18
+ gem install librix
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "librix"
25
+
26
+ # Configure once (e.g., in an initializer)
27
+ Librix.configure do |config|
28
+ config.google_books_api_key = ENV["GOOGLE_BOOKS_API_KEY"]
29
+ # Optional tuning
30
+ config.http_open_timeout = 2
31
+ config.http_read_timeout = 5
32
+ config.user_agent = "my-app/1.0"
33
+ # Default provider is :google_books. You can set a default globally:
34
+ config.provider = :google_books
35
+ end
36
+
37
+ # Or request one explicitly at call-site:
38
+ provider = Librix.provider(:google_books)
39
+ result = provider.search(title: "Clean Code")
40
+
41
+ # Available providers
42
+ # Librix::Providers::Factory.available #=> [:google_books]
43
+
44
+ # Registering a custom provider
45
+ # class MyProvider
46
+ # def search(params)
47
+ # # ...
48
+ # end
49
+ # end
50
+ # Librix::Providers::Factory.register(:my_provider, MyProvider)
51
+ # provider = Librix.provider(:my_provider)
52
+ ```
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/librix. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/librix/blob/master/CODE_OF_CONDUCT.md).
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,21 @@
1
+ module Librix
2
+ class Configuration
3
+ attr_accessor :google_books_api_key,
4
+ :goodreads_api_key,
5
+ :open_library_enabled,
6
+ :provider,
7
+ :http_open_timeout,
8
+ :http_read_timeout,
9
+ :user_agent,
10
+ :google_books_base_url
11
+
12
+ def initialize
13
+ @open_library_enabled = false
14
+ @http_open_timeout = 2
15
+ @http_read_timeout = 5
16
+ @user_agent = "librix/#{Librix::VERSION}"
17
+ @google_books_base_url = "https://www.googleapis.com/books/v1"
18
+ @provider = :google_books
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ module Librix
2
+ module Providers
3
+ class Factory
4
+ class << self
5
+ def register(key, klass)
6
+ registry[key.to_sym] = klass
7
+ end
8
+
9
+ def build(name = nil)
10
+ provider_key = (name || Librix.configuration.provider || :google_books).to_sym
11
+ klass = registry[provider_key]
12
+ raise ArgumentError, "Unknown provider: #{provider_key}" unless klass
13
+ klass.new
14
+ end
15
+
16
+ def available
17
+ registry.keys
18
+ end
19
+
20
+ private
21
+
22
+ def registry
23
+ @registry ||= {
24
+ google_books: Librix::Providers::GoogleBooks
25
+ }
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+
@@ -0,0 +1,47 @@
1
+ require "uri"
2
+ require "httparty"
3
+ module Librix
4
+ module Providers
5
+ class GoogleBooks
6
+ def initialize
7
+ @config = Librix.configuration
8
+ @api_key = @config.google_books_api_key
9
+ end
10
+
11
+ def search(params)
12
+ raise ArgumentError, "title is required" unless params && params[:title].to_s.strip != ""
13
+
14
+ base = @config.google_books_base_url
15
+ query = URI.encode_www_form(q: params[:title])
16
+ url = "#{base}/volumes?#{query}"
17
+
18
+ headers = { "User-Agent" => @config.user_agent }
19
+ options = {
20
+ headers: headers,
21
+ timeout: @config.http_read_timeout,
22
+ open_timeout: @config.http_open_timeout,
23
+ query: (@api_key ? { key: @api_key } : {})
24
+ }
25
+
26
+ response = HTTParty.get(url, options)
27
+
28
+ unless response.success?
29
+ raise Librix::Error, "GoogleBooks HTTP #{response.code}: #{safe_body_snippet(response)}"
30
+ end
31
+
32
+ response.parsed_response
33
+ rescue Net::OpenTimeout, Net::ReadTimeout => e
34
+ raise Librix::Error, "GoogleBooks timeout: #{e.message}"
35
+ rescue SocketError, Errno::ECONNREFUSED => e
36
+ raise Librix::Error, "GoogleBooks connection failed: #{e.message}"
37
+ end
38
+
39
+ private
40
+
41
+ def safe_body_snippet(response)
42
+ body = response&.body.to_s
43
+ body.length > 200 ? body[0, 200] + "..." : body
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Librix
4
+ VERSION = "0.1.0"
5
+ end
data/lib/librix.rb ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "librix/version"
4
+ require_relative "librix/configuration"
5
+ require_relative "librix/providers/google_books"
6
+ require_relative "librix/providers/factory"
7
+
8
+ module Librix
9
+ class Error < StandardError; end
10
+
11
+ class << self
12
+ attr_writer :configuration
13
+ end
14
+
15
+ def self.configuration
16
+ @configuration ||= Configuration.new
17
+ end
18
+
19
+ def self.configure
20
+ yield(configuration)
21
+ end
22
+
23
+ def self.provider(name = nil)
24
+ Librix::Providers::Factory.build(name)
25
+ end
26
+ end
data/sig/librix.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Librix
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: librix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vinicius Coelho
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: httparty
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0.21'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0.21'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '2.0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: rake
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '13.0'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '14.0'
42
+ type: :development
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '13.0'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '14.0'
52
+ - !ruby/object:Gem::Dependency
53
+ name: rspec
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '3.0'
59
+ - - "<"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - - "<"
70
+ - !ruby/object:Gem::Version
71
+ version: '4.0'
72
+ description: Librix helps you search for books across multiple sources with a tiny,
73
+ friendly API. Also, add more providers when you need them.
74
+ email:
75
+ - vinicoelho.dev@outlook.com
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".rspec"
81
+ - CHANGELOG.md
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - lib/librix.rb
86
+ - lib/librix/configuration.rb
87
+ - lib/librix/providers/factory.rb
88
+ - lib/librix/providers/google_books.rb
89
+ - lib/librix/version.rb
90
+ - sig/librix.rbs
91
+ homepage: https://github.com/viniciusscoelho/librix
92
+ licenses:
93
+ - MIT
94
+ metadata:
95
+ allowed_push_host: https://rubygems.org
96
+ homepage_uri: https://github.com/viniciusscoelho/librix
97
+ source_code_uri: https://github.com/viniciusscoelho/librix
98
+ changelog_uri: https://github.com/viniciusscoelho/librix/blob/main/CHANGELOG.md
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 3.1.0
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.6.8
114
+ specification_version: 4
115
+ summary: Gem to manage book search across multiple providers
116
+ test_files: []