# Maintainer: Tim Meusel <tim@bastelfreak.de>
# Contributor: Christian Rebischke <chris.rebischke@archlinux.org>
# Contributor: farwayer <farwayer@gmail.com>
# Contributor: Tim Meusel <tim@bastelfreak.de>

_gemname='httpclient'
pkgname="ruby-${_gemname}"
pkgver=2.8.3
pkgrel=13
pkgdesc='gives something like the functionality of libwww-perl (LWP) in Ruby'
arch=('any')
url='https://github.com/nahi/httpclient'
license=('Ruby')
makedepends=('ruby-rdoc' 'ruby-bundler')
checkdepends=('ruby-rake' 'ruby-http-cookie' 'ruby-test-unit' 'ruby-rack' 'ruby-webrick')
depends=('ruby' 'ruby-mutex_m')
options=(!emptydirs)
source=("${url}/archive/v${pkgver}/${pkgname}-v${pkgver}.tar.gz"
        'disable-broken-TLS-test.patch')
sha512sums=('b65b5adb97af069d7ff6b6f4cccd6a42c6d9c88b797e9495768fe1f36a6bd93defe50190edc201de5e39c4e306ba0bd17a8ff2f8eba4d636dacafa06c172967d'
            'fcdb88f83ea1663f1f21113f7943223ffc1c69dc0f1305d74e8a96b3752f905c1bfa3727f2eb9dae9742db16d222ba5971a072b065a6c7ce535615b51fdb8712')

prepare() {
  cd "${srcdir}/${_gemname}-${pkgver}"

  # Allow latest gem dependencies
  sed --in-place 's|~>|>=|g' Gemfile

  # fix wrong license name in gemspec
  sed --in-place "s/'ruby'/'Ruby'/" "${_gemname}.gemspec"

  # don't load the bundler setup. That would require us to
  # provide all 'development' dependencies
  sed --in-place '/setup/d' Rakefile

  # test_auth.rb does a bunch of windows specific NTLM tests
  # they don't apply to Arch
  rm test/test_auth.rb

  # one test verifies the openssl store.
  # a) this test has nothing to do with the actual httpclient
  # b) the used certificate is expiried so the test fails
  patch --forward --verbose --strip=1 --input='../disable-broken-TLS-test.patch'
}

build() {
  cd "${srcdir}/${_gemname}-${pkgver}"
  gem build "${_gemname}.gemspec"
}

check() {
  cd "${srcdir}/${_gemname}-${pkgver}"
  # Ignoring a few tests that fail on Ruby 3 (taken from Fedora)
  # https://src.fedoraproject.org/rpms/rubygem-httpclient/blob/rawhide/f/rubygem-httpclient.spec
  ruby -Ilib -e 'Dir.glob "./test/test_*.rb", &method(:require)' -- \
    --ignore-name /^test_post_async_with_default_internal$/ \
    --ignore-name /^test_timeout$/ \
    --ignore-name /^test_tcp_keepalive$/ \
    --ignore-name /^test_sync$/ \
    --ignore-name /^test_proxy_ssl$/ \
    --ignore-name /^test_cert_store$/ \
    --ignore-name /^test_verification_without_httpclient$/ \
    --ignore-name /^test_verification$/ \
    --ignore-name /^test_set_default_paths$/ \
    --ignore-name /^test_allow_tlsv1$/ \
    --ignore-name /^test_no_sslv3$/ \
    --ignore-name /^test_post_connection_check$/ \
    --ignore-name /^test_debug_dev$/ \
    --ignore-name /^test_ciphers$/ \
    --ignore-name /^test_redirect_see_other$/ \
    --ignore-name /^test_post_follow_redirect$/ \
    --ignore-name /^test_put$/ \
    --ignore-name /^test_post_empty$/ \
    --ignore-name /^test_post_content$/
}

package() {
  cd "${srcdir}/${_gemname}-${pkgver}"
  local _gemdir="$(gem env gemdir)"
  gem install --verbose --ignore-dependencies --no-user-install --install-dir "${pkgdir}/${_gemdir}" --bindir "${pkgdir}/usr/bin" "${_gemname}-${pkgver}.gem"

  install -Dm 644 README.md CHANGELOG.md -t "${pkgdir}/usr/share/doc/${pkgname}/"

  rm -rf "${pkgdir}/${_gemdir}/cache"
}

# vim: ts=2 sw=2 et:
