|
| 1 | +# OpenBeautyFacts Ruby SDK |
| 2 | + |
| 3 | +OpenBeautyFacts Ruby SDK is a Ruby gem that provides API access to the Open Food Facts database, the open database about food. The gem includes models for Product, Locale, User and many other food-related entities. |
| 4 | + |
| 5 | +Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Environment Setup |
| 10 | +- Install Ruby 2.5 or higher (project supports Ruby 2.5-3.3, current development uses Ruby 3.3.6) |
| 11 | +- Install Bundler for dependency management: |
| 12 | + ```bash |
| 13 | + gem install bundler --user-install |
| 14 | + export PATH="$PATH:$HOME/.local/share/gem/ruby/3.2.0/bin" |
| 15 | + ``` |
| 16 | +- Configure Bundler to install gems to user directory: |
| 17 | + ```bash |
| 18 | + bundle config path ~/.gems |
| 19 | + ``` |
| 20 | + |
| 21 | +### Bootstrap, Build, and Test |
| 22 | +- **Install dependencies**: `bundle install` -- takes 5-15 seconds. NEVER CANCEL. Set timeout to 60+ seconds. |
| 23 | +- **Run tests**: `bundle exec rake test` -- takes 2-3 seconds. NEVER CANCEL. Set timeout to 30+ seconds. |
| 24 | + - NOTE: Tests may have failures due to external API dependencies being unreachable in restricted environments |
| 25 | +- **Build gem**: `bundle exec rake build` -- takes 1 second. NEVER CANCEL. Set timeout to 30+ seconds. |
| 26 | +- **Install gem locally**: `bundle exec rake install:local` -- takes 1-2 seconds. |
| 27 | + |
| 28 | +### Development Commands |
| 29 | +- **List all rake tasks**: `bundle exec rake -T` |
| 30 | +- **Launch console**: `bundle exec rake console` (may hang, use `bundle exec irb -r openbeautyfacts` instead) |
| 31 | +- **Clean build artifacts**: `bundle exec rake clobber` |
| 32 | + |
| 33 | +## Validation |
| 34 | + |
| 35 | +### Always Test Core Functionality |
| 36 | +After making changes, always validate by running: |
| 37 | +```ruby |
| 38 | +# Create a test script to verify basic functionality |
| 39 | +require 'openbeautyfacts' |
| 40 | +puts "Version: #{Openbeautyfacts::VERSION}" |
| 41 | +product = Openbeautyfacts::Product.new(code: "test") |
| 42 | +puts "Product URL: #{Openbeautyfacts::Product.url('test', locale: 'world')}" |
| 43 | +``` |
| 44 | + |
| 45 | +### Linting |
| 46 | +- RuboCop is used for code style but is not included in development dependencies |
| 47 | +- Install and run manually: |
| 48 | + ```bash |
| 49 | + gem install rubocop --user-install |
| 50 | + rubocop lib/openbeautyfacts.rb |
| 51 | + ``` |
| 52 | +- CI pipeline includes RuboCop analysis (see `.github/workflows/rubocop-analysis.yml`) |
| 53 | + |
| 54 | +### Manual Testing Scenarios |
| 55 | +- Always test that the library loads: `require 'openbeautyfacts'` |
| 56 | +- Verify version access: `Openbeautyfacts::VERSION` should return current version (0.6.2) |
| 57 | +- Test Product model instantiation: `Openbeautyfacts::Product.new(code: "test")` |
| 58 | +- Verify URL generation: `Openbeautyfacts::Product.url("123", locale: "world")` |
| 59 | + |
| 60 | +## Common Tasks |
| 61 | + |
| 62 | +### Repo Structure |
| 63 | +``` |
| 64 | +. |
| 65 | +├── .github/ # GitHub workflows and configs |
| 66 | +├── lib/ |
| 67 | +│ ├── openbeautyfacts.rb # Main module file |
| 68 | +│ └── openbeautyfacts/ |
| 69 | +│ ├── product.rb # Core Product model |
| 70 | +│ ├── locale.rb # Locale handling |
| 71 | +│ ├── user.rb # User authentication |
| 72 | +│ └── [27 other models] # Brand, Category, etc. |
| 73 | +├── test/ |
| 74 | +│ ├── minitest_helper.rb # Test configuration |
| 75 | +│ ├── test_openbeautyfacts.rb # Main test file |
| 76 | +│ └── fixtures/ # VCR test fixtures |
| 77 | +├── Gemfile # Dependencies |
| 78 | +├── openbeautyfacts.gemspec # Gem specification |
| 79 | +├── Rakefile # Build tasks |
| 80 | +└── README.md # Usage documentation |
| 81 | +``` |
| 82 | + |
| 83 | +### Key Files to Know |
| 84 | +- **Main entry point**: `lib/openbeautyfacts.rb` - defines module and requires all classes |
| 85 | +- **Primary model**: `lib/openbeautyfacts/product.rb` - core Product class with API methods |
| 86 | +- **Version**: `lib/openbeautyfacts/version.rb` - contains `VERSION = "0.6.2"` |
| 87 | +- **Tests**: `test/test_openbeautyfacts.rb` - comprehensive test suite with VCR fixtures |
| 88 | +- **CI workflows**: |
| 89 | + - `.github/workflows/ruby.yml` - runs tests on Ruby 2.5-3.3 |
| 90 | + - `.github/workflows/rubocop-analysis.yml` - static code analysis |
| 91 | + |
| 92 | +### Dependencies |
| 93 | +- **Runtime**: `hashie` (>= 3.4, < 6.0), `nokogiri` (~> 1.7) |
| 94 | +- **Development**: `minitest`, `vcr`, `webmock`, `rake`, `bundler` |
| 95 | +- **Ruby requirement**: >= 2.5 |
| 96 | + |
| 97 | +### API and Network Dependencies |
| 98 | +- The gem connects to `openbeautyfacts.org` for live data |
| 99 | +- Tests use VCR (Video Cassette Recorder) to mock HTTP requests |
| 100 | +- In network-restricted environments, only basic functionality (class loading, URL generation) can be tested |
| 101 | +- Real API calls require setting `OPENBEAUTYFACTS_USER_AGENT` environment variable |
| 102 | + |
| 103 | +### Common Issues |
| 104 | +- **Permission errors during gem install**: Use `--user-install` flag and update PATH |
| 105 | +- **Bundle install fails**: Configure `bundle config path ~/.gems` first |
| 106 | +- **Test failures**: Expected when network access is restricted; focus on gem build success |
| 107 | +- **RuboCop not found**: Install separately with `gem install rubocop --user-install` |
| 108 | + |
| 109 | +### Development Workflow |
| 110 | +1. Make changes to lib/ files |
| 111 | +2. Run `bundle exec rake build` to verify gem builds |
| 112 | +3. Run `bundle exec rake test` to run test suite |
| 113 | +4. Create simple validation script to test core functionality |
| 114 | +5. Run RuboCop for style checking (if installed) |
| 115 | +6. Always test that the gem loads and basic models work |
| 116 | + |
| 117 | +This gem provides a Ruby interface to the Open Food Facts database with models for products, locales, users, and food-related metadata. Changes should maintain backward compatibility and follow the existing patterns in the codebase. |
0 commit comments