Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: bundler
directory: /
schedule:
interval: monthly

- package-ecosystem: github-actions
directory: /
schedule:
interval: monthly
43 changes: 43 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: CI

on:
push:
# Avoid duplicate builds on PRs.
branches:
- main
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
bundler-cache: true

- name: Run StandardRB
run: bundle exec standardrb

test:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
ruby-version: ['3.2', '3.3', '3.4', '4.0']

steps:
- uses: actions/checkout@v6

- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true

- name: Run tests
run: bundle exec rake test
1 change: 1 addition & 0 deletions .standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby_version: 3.2
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source 'https://rubygems.org'
source "https://rubygems.org"

gemspec
gemspec
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# GitHubBub

[![Build Status](https://travis-ci.org/schneems/git_hub_bub.svg?branch=master)](https://travis-ci.org/schneems/git_hub_bub)
[![CI](https://github.com/schneems/git_hub_bub/actions/workflows/ci.yml/badge.svg)](https://github.com/schneems/git_hub_bub/actions/workflows/ci.yml)
[![Help Contribute to Open Source](https://www.codetriage.com/schneems/git_hub_bub/badges/users.svg)](https://www.codetriage.com/schneems/git_hub_bub)

A low level GitHub client that makes the disgusting issue of header based url pagination simple.
Expand Down
17 changes: 8 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
require 'bundler/gem_tasks'
require 'git_hub_bub'
require "bundler/gem_tasks"
require "git_hub_bub"

task :default => [:test]
task default: [:test]

require 'rake'
require 'rake/testtask'
require "rake"
require "rake/testtask"

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.libs << "lib"
t.libs << "test"
t.pattern = "test/**/*_test.rb"
t.verbose = true
end

4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## Master
## Unreleased

- Minimum supported Ruby version updated to 3.2

## 1.0.1

Expand Down
34 changes: 17 additions & 17 deletions git_hub_bub.gemspec
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'git_hub_bub/version'
require "git_hub_bub/version"

Gem::Specification.new do |gem|
gem.name = "git_hub_bub"
gem.version = GitHubBub::VERSION
gem.authors = ["Richard Schneeman"]
gem.email = ["richard.schneeman+rubygems@gmail.com"]
gem.description = %q{git_hub_bub makes github requests}
gem.summary = %q{git_hub_bub makes github requests}
gem.homepage = "https://github.com/schneems/git_hub_bub"
gem.license = "MIT"
gem.name = "git_hub_bub"
gem.version = GitHubBub::VERSION
gem.authors = ["Richard Schneeman"]
gem.email = ["richard.schneeman+rubygems@gmail.com"]
gem.description = "git_hub_bub makes github requests"
gem.summary = "git_hub_bub makes github requests"
gem.homepage = "https://github.com/schneems/git_hub_bub"
gem.license = "MIT"

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
gem.require_paths = ["lib"]

gem.add_dependency "rrrretry"
gem.add_dependency "excon"
gem.add_dependency "base64"
gem.add_development_dependency "timecop"
gem.add_development_dependency "test-unit"
gem.add_development_dependency "mocha"
gem.add_development_dependency "rake"
gem.add_development_dependency "vcr", '~> 2.5.0'
gem.add_development_dependency "webmock", '~> 1.11.0'
gem.add_development_dependency "vcr"
gem.add_development_dependency "webmock"
gem.add_development_dependency "dotenv"
gem.add_development_dependency "standard"

gem.required_ruby_version = '>= 2.2'
gem.required_ruby_version = ">= 3.2"
end
24 changes: 11 additions & 13 deletions lib/git_hub_bub.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
# frozen_string_literal: true

require 'securerandom'
require 'json'
require 'uri'
require 'cgi'
require "securerandom"
require "json"
require "uri"

require 'excon'
require 'rrrretry'
require "excon"
require "rrrretry"

require 'git_hub_bub/request'
require 'git_hub_bub/response'
require "git_hub_bub/request"
require "git_hub_bub/response"

module GitHubBub
class << self

def valid_token?(token)
response = Request.get("https://#{ENV['GITHUB_APP_ID']}:#{ENV['GITHUB_APP_SECRET']}@api.github.com/applications/#{ENV['GITHUB_APP_ID']}/tokens/#{token}", {}, {skip_token: true})
response = Request.get("https://#{ENV["GITHUB_APP_ID"]}:#{ENV["GITHUB_APP_SECRET"]}@api.github.com/applications/#{ENV["GITHUB_APP_ID"]}/tokens/#{token}", {}, {skip_token: true})
return response if response.success?
return false if response.status == 404
false if response.status == 404
rescue GitHubBub::RequestError => e
if Request::RAISE_ON_FAIL
return false
false
else
raise e
end
Expand Down Expand Up @@ -50,4 +48,4 @@ def delete(*args)
Request.delete(*args)
end
end
end
end
59 changes: 31 additions & 28 deletions lib/git_hub_bub/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@ module GitHubBub
class RequestError < StandardError; end

class Request
attr_accessor :url, :options, :token
BASE_URI = 'https://api.github.com'
USER_AGENT ||= SecureRandom.hex(16)
attr_accessor :url, :options
attr_writer :token
BASE_URI = "https://api.github.com"
USER_AGENT = defined?(USER_AGENT) ? USER_AGENT : SecureRandom.hex(16)
GITHUB_VERSION = "vnd.github.3.raw+json"
EXTRA_HEADERS ||= {}
BASE_HEADERS = EXTRA_HEADERS.merge({'Accept' => "application/#{GITHUB_VERSION}", "User-Agent" => USER_AGENT})
BASE_OPTIONS = { omit_default_port: true }
RETRIES = 1
RAISE_ON_FAIL = ENV["GIT_HUB_BUB_RAISE_ON_FAIL"]
EXTRA_HEADERS = defined?(EXTRA_HEADERS) ? EXTRA_HEADERS : {}
BASE_HEADERS = EXTRA_HEADERS.merge({"Accept" => "application/#{GITHUB_VERSION}", "User-Agent" => USER_AGENT})
BASE_OPTIONS = {omit_default_port: true}
RETRIES = 1
RAISE_ON_FAIL = ENV["GIT_HUB_BUB_RAISE_ON_FAIL"]

def initialize(url, query = {}, options = {})
self.url = url =~ /^http(\w?)\:\/\// ? url : File.join(BASE_URI, url)
@skip_token = options.delete(:skip_token)
self.options = BASE_OPTIONS.merge(options || {})
self.options[:query] = query if query && !query.empty?
self.options[:headers] = BASE_HEADERS.merge(options[:headers]|| {})
self.url = /^http(\w?):\/\//.match?(url) ? url : File.join(BASE_URI, url)
@skip_token = options.delete(:skip_token)
self.options = BASE_OPTIONS.merge(options || {})
self.options[:query] = query if query && !query.empty?
self.options[:headers] = BASE_HEADERS.merge(options[:headers] || {})
end

def skip_token?
@skip_token
end

def self.head(url, query = {}, options = {})
self.new(url, query, options).head
new(url, query, options).head
end

def head
Expand All @@ -37,19 +38,21 @@ def head
end

def self.get(url, query = {}, options = {})
self.new(url, query, options).get
new(url, query, options).get
end

def get
wrap_request do
ex = Excon.get(url, options)
ex = Excon.get(@location, options) if @location = ex.headers["Location"]
if (@location = ex.headers["Location"])
ex = Excon.get(@location, options)
end
ex
end
end

def self.post(url, query = {}, options = {})
self.new(url, query, options).post
new(url, query, options).post
end

def post
Expand All @@ -59,7 +62,7 @@ def post
end

def self.patch(url, query = {}, options = {})
self.new(url, query, options).patch
new(url, query, options).patch
end

def patch
Expand All @@ -69,7 +72,7 @@ def patch
end

def self.put(url, query = {}, options = {})
self.new(url, query, options).put
new(url, query, options).put
end

def put
Expand All @@ -79,7 +82,7 @@ def put
end

def self.delete(url, query = {}, options = {})
self.new(url, query, options).delete
new(url, query, options).delete
end

def delete
Expand All @@ -97,9 +100,9 @@ def wrap_request(&block)
end

if RAISE_ON_FAIL
raise RequestError, "message: '#{response.json_body['message']}', url: '#{url}', response: '#{response.inspect}'" unless response.success?
raise RequestError, "message: '#{response.json_body["message"]}', url: '#{url}', response: '#{response.inspect}'" unless response.success?
end
return response
response
end

# do they take query params? do they take :body?
Expand All @@ -114,22 +117,22 @@ def set_auth_from_token!
end

def token
@token ||= if options[:headers] && token_string = options[:headers]["Authorization"]
@token ||= if options[:headers] && (token_string = options[:headers]["Authorization"])
token_string.split(/\s/).last
elsif options[:query] && token = options[:query].delete(:token)
token
elsif options[:query] && (query_token = options[:query].delete(:token))
query_token
else
skip_token?
end
end
alias :token? :token
alias_method :token?, :token

def self.set_before_callback(&block)
before_callbacks << block
end

def self.before_callbacks
@before_callbacks ||=[]
@before_callbacks ||= []
end

def self.clear_callbacks
Expand All @@ -138,7 +141,7 @@ def self.clear_callbacks

def before_callbacks!
self.class.before_callbacks.each do |callback|
run_callback &callback
run_callback(&callback)
end
end

Expand Down
Loading
Loading