├── .ruby-version ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── Procfile ├── README.md └── lib ├── maven_central.rb ├── shields.rb └── web.rb /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.2.3 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | ruby '2.2.3' 3 | 4 | gem 'httparty', '~> 0.13' 5 | gem 'sinatra', '~> 1.4' 6 | gem 'thin', '~> 1.6' 7 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | daemons (1.2.3) 5 | eventmachine (1.0.8) 6 | httparty (0.13.7) 7 | json (~> 1.8) 8 | multi_xml (>= 0.5.2) 9 | json (1.8.3) 10 | multi_xml (0.5.5) 11 | rack (1.6.4) 12 | rack-protection (1.5.3) 13 | rack 14 | sinatra (1.4.6) 15 | rack (~> 1.4) 16 | rack-protection (~> 1.4) 17 | tilt (>= 1.3, < 3) 18 | thin (1.6.4) 19 | daemons (~> 1.0, >= 1.0.9) 20 | eventmachine (~> 1.0, >= 1.0.4) 21 | rack (~> 1.0) 22 | tilt (2.0.1) 23 | 24 | PLATFORMS 25 | ruby 26 | 27 | DEPENDENCIES 28 | httparty (~> 0.13) 29 | sinatra (~> 1.4) 30 | thin (~> 1.6) 31 | 32 | BUNDLED WITH 33 | 1.10.6 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright 2013 Jakub Jirutka . 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. -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: bundle exec ruby lib/web.rb -p $PORT 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Badges for Maven projects 2 | ========================= 3 | 4 | ## Outdated 5 | 6 | We have migrated this project to NodeJS and you can find a new version [here](https://github.com/softwaremill/maven-badges). Please report any issues there, PRs are welcome as well :) 7 | 8 | ## Description 9 | 10 | Badges! These tiny pictures with label and some numbers, you see them in many GitHub readmes. We all love them, yeah? Travis, Coveralls, Code Climate, Gemnasium, Gem, PyPi, npm… 11 | However, most of them are not usable for Java/Groovy guys and that’s quite sad, isn’t it? 12 | 13 | Well, I put together [shields.io](http://shields.io/), [Heroku](https://heroku.com) and some Ruby code to generate a shiny badge that shows an artifact’s version available in Maven Central. 14 | 15 | 16 | Usage 17 | ----- 18 | 19 | https://maven-badges.herokuapp.com/maven-central/{group_id}/{artifact_id}/badge.(svg|png)?style={style} 20 | 21 | For example: 22 | 23 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/cz.jirutka.rsql/rsql-parser/badge.svg)](https://maven-badges.herokuapp.com/maven-central/cz.jirutka.rsql/rsql-parser) 24 | 25 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/cz.jirutka.rsql/rsql-parser/badge.svg)](https://maven-badges.herokuapp.com/maven-central/cz.jirutka.rsql/rsql-parser) 26 | 27 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/cz.jirutka.rsql/rsql-parser/badge.svg?style=plastic)](https://maven-badges.herokuapp.com/maven-central/cz.jirutka.rsql/rsql-parser) 28 | 29 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/cz.jirutka.rsql/rsql-parser/badge.svg?style=plastic)](https://maven-badges.herokuapp.com/maven-central/cz.jirutka.rsql/rsql-parser) 30 | 31 | License 32 | ------- 33 | This project is licensed under [MIT license](http://opensource.org/licenses/MIT). 34 | -------------------------------------------------------------------------------- /lib/maven_central.rb: -------------------------------------------------------------------------------- 1 | require 'httparty' 2 | require 'uri' 3 | 4 | class MavenCentral 5 | include HTTParty 6 | 7 | base_uri 'http://search.maven.org' 8 | default_params wt: 'json' 9 | headers 'Accept' => 'application/json' 10 | format :json 11 | 12 | # Requests the last version of the specified artifact. 13 | def self.last_artifact_version(group_id, artifact_id) 14 | resp = get('/solrsearch/select', query: { 15 | q: format_query(group_id, artifact_id), rows: 1 16 | }) 17 | raise HTTParty::ResponseError.new(resp) if resp.code != 200 18 | 19 | doc = resp.parsed_response['response'] 20 | if doc['numFound'] > 0 21 | doc['docs'][0]['latestVersion'] 22 | else 23 | raise NotFoundError 24 | end 25 | end 26 | 27 | # Returns URL of the web page with details about the specified artifact. 28 | def self.artifact_details_url(group_id, artifact_id, version) 29 | "#{base_uri}/#artifactdetails%7C#{group_id}%7C#{artifact_id}%7C#{version}%7C" 30 | end 31 | 32 | # Returns URL of the web page with search results for searching by the 33 | # artifact's coordinates. 34 | def self.search_by_ga_url(group_id, artifact_id) 35 | "#{base_uri}/#search%7Cga%7C1%7C#{format_query(group_id, artifact_id)}" 36 | end 37 | 38 | private 39 | 40 | def self.format_query(group_id, artifact_id) 41 | %{g:"#{group_id}" AND a:"#{artifact_id}"} 42 | end 43 | end 44 | 45 | class NotFoundError < StandardError; end -------------------------------------------------------------------------------- /lib/shields.rb: -------------------------------------------------------------------------------- 1 | require 'httparty' 2 | 3 | class Shields 4 | include HTTParty 5 | 6 | REPLACEMENTS = {'_' => '__', ' ' => '_', '-' => '--' } 7 | 8 | base_uri 'img.shields.io' 9 | 10 | def self.badge_image(subject, status, color, format='svg', style='default') 11 | resp = get("/badge/#{encode(subject)}-#{encode(status)}-#{color}.#{format}?style=#{style}") 12 | 13 | raise HTTParty::ResponseError.new(resp) unless resp.code == 200 14 | resp.body 15 | end 16 | 17 | private 18 | def self.encode(input) 19 | result = input.dup 20 | REPLACEMENTS.each do |needle, repl| 21 | result.gsub! needle, repl 22 | end 23 | result 24 | end 25 | end -------------------------------------------------------------------------------- /lib/web.rb: -------------------------------------------------------------------------------- 1 | require 'sinatra' 2 | require_relative 'maven_central' 3 | require_relative 'shields' 4 | 5 | DEFAULT_SUBJECT = 'maven central' 6 | DEFAULT_STYLE = 'default' 7 | DEFAULT_COLOR = 'brightgreen' 8 | PROJECT_SITE = 'https://github.com/jirutka/maven-badges' 9 | 10 | configure :production do 11 | disable :static 12 | before { cache_control :public, :max_age => 3600 } 13 | end 14 | 15 | get '/' do 16 | content_type :text 17 | "Nothing is here, see #{PROJECT_SITE}." 18 | end 19 | 20 | # Returns badge image with the artifact's last version number 21 | get '/maven-central/:group/:artifact/badge.:format' do |group, artifact, format| 22 | halt 415 unless ['svg', 'png'].include? format 23 | 24 | content_type format 25 | subject = params['subject'] || DEFAULT_SUBJECT 26 | 27 | begin 28 | version = MavenCentral.last_artifact_version(group, artifact) 29 | color = params[:color] || DEFAULT_COLOR 30 | rescue NotFoundError 31 | version = 'unknown' 32 | color = :lightgray 33 | end 34 | 35 | style = params[:style] || DEFAULT_STYLE 36 | 37 | Shields.badge_image(subject, version, color, format, style) 38 | end 39 | 40 | # Returns the artifact's last version number in plain text 41 | get '/maven-central/:group/:artifact/last_version' do |group, artifact| 42 | content_type :text 43 | 44 | MavenCentral.last_artifact_version(group, artifact) 45 | end 46 | 47 | # Redirects to the artifact's page on search.maven.org 48 | get '/maven-central/:group/:artifact/?' do |group, artifact| 49 | begin 50 | version = MavenCentral.last_artifact_version(group, artifact) 51 | redirect to MavenCentral.artifact_details_url(group, artifact, version) 52 | rescue NotFoundError 53 | redirect to MavenCentral.search_by_ga_url(group, artifact) 54 | end 55 | end 56 | 57 | error do 58 | content_type :text 59 | halt 500, "Something went wrong, please open an issue on #{PROJECT_SITE}/issues" 60 | end --------------------------------------------------------------------------------