Permalink
Browse files

start of a huge refactoring of the backend codez

  • Loading branch information...
1 parent 0199ef2 commit a6ca3e61ed846ffb80143321f7a941aeef8dd5d5 @rauhryan rauhryan committed Mar 24, 2013
Showing with 198 additions and 43 deletions.
  1. +1 −1 lib/api.rb
  2. +2 −4 lib/app.rb
  3. +171 −0 lib/bridge/huboard.rb
  4. +2 −2 lib/github.rb
  5. +13 −4 lib/helpers.rb
  6. +9 −32 lib/pebble.rb
View
@@ -1,6 +1,6 @@
require_relative "helpers"
-module Huboard
+class Huboard
class API < Sinatra::Base
register Sinatra::Auth::Github
register Huboard::Common
View
@@ -5,7 +5,7 @@
require 'base64'
require_relative "helpers"
-module Huboard
+class Huboard
class App < Sinatra::Base
register Sinatra::Auth::Github
register Huboard::Common
@@ -51,9 +51,7 @@ def protected!
get '/:user/?' do
protected!
@parameters = params
- user = gh.users(params[:user])
- @repos = user.repos.all.sort_by{|r|r["pushed_at"] || "111111"}.reverse if user["type"] == "User"
- @repos = gh.orgs(user["login"]).repos.all.sort_by{|r|r["pushed_at"] || "111111"}.reverse if user["type"] == "Organization"
+ @repos = Huboard.repos_by_user(params[:user])
#@repos = pebble.all_repos.select {|r| r["owner"]["login"] == params[:user]}
@filtered = params[:user]
erb :index
View
@@ -0,0 +1,171 @@
+class Huboard
+
+ def self.column_pattern
+ return /(?<id>\d+) *- *(?<name>.+)/
+ end
+
+ def self.link_pattern
+ return /Link <=> (?<user_name>.*)\/(?<repo>.*)/
+ end
+
+ def self.settings_pattern
+ return /@huboard:(.*)/
+ end
+
+ def self.all_patterns
+ [self.column_pattern, self.link_pattern, self.settings_pattern]
+ end
+
+
+ module Config
+
+ VALID_OPTIONS_KEYS = [
+ :oauth_token,
+ :faraday_config_block,
+ :api_endpoint,
+ :access_token
+
+ ].freeze
+
+ attr_accessor(*VALID_OPTIONS_KEYS)
+
+ def configure
+ yield self
+ end
+
+ def options
+ VALID_OPTIONS_KEYS.inject({}){|o,k| o.merge!(k => send(k)) }
+ end
+
+ def api_endpoint=(value)
+ @api_endpoint = File.join(value, "")
+ end
+
+ def faraday_config(&block)
+ @faraday_config_block = block
+ end
+
+ end
+
+ extend Config
+
+ class << self
+
+ def client
+ @gh ||= Ghee.new(:access_token => access_token) do |conn|
+ faraday_config_block.call(conn) if faraday_config_block
+ end
+ end
+
+ def board_for(user, repo)
+ Board.new(user, repo)
+ end
+
+
+ end
+
+
+ module Repos
+
+ def repos(org = nil)
+
+ repos = org.nil? ? client.user.repos.all : client.orgs(org).repos.all
+
+ repos.reject { |r| !r.has_issues }.sort_by{|r| r["pushed_at"] || "1111111111111111"}.reverse
+
+ end
+
+ def all_repos
+ the_repos = repos
+ client.orgs.each do |org|
+ the_repos.concat(repos(org.login))
+ end
+ the_repos.sort_by{|r| r["pushed_at"] || "1111111111111111"}.reverse
+ end
+
+ def repos_by_user(user)
+ user = client.users user
+ the_repos = repos(user.login) if user.type == "Organization"
+ the_repos = user.repos.all.sort_by{|r| r["pushed_at"] || "1111111111111111"}.reverse if user.type == "User"
+ the_repos.reject { |r| !r.has_issues }.sort_by{|r| r["pushed_at"] || "1111111111111111"}.reverse
+ end
+
+
+ end
+
+ extend Repos
+
+
+ module Assignees
+ def assignees
+ gh.assignees.all
+ end
+ end
+
+
+ module Labels
+
+ def labels
+ gh.labels
+ end
+
+ def other_labels
+ labels.reject { |l| Huboard.all_patterns.any? {|p| p.match l.name } }
+ end
+
+ def settings_labels
+ labels.select{|l| Huboard.settings_pattern.match l.name }
+ end
+
+ def column_labels
+ labels.select{|l| Huboard.column_pattern.match l.name }
+ end
+
+ def link_labels
+ labels.select{|l| Huboard.link_pattern.match l.name }
+ end
+
+ end
+
+ module Settings
+
+ def settings
+ defaults = {
+ :show_all => true
+ }
+
+ the_settings = settings_labels.map do |l|
+ match = Huboard.settings_pattern.match l["name"]
+ begin
+ YAML.load(match[1])
+ rescue
+ return {}
+ end
+ end.reduce(:merge)
+
+ defaults.merge(the_settings || {})
+
+ end
+
+ end
+
+ class Board
+ attr_accessor :user, :repo
+
+ def initialize(user, repo, client = Huboard.client)
+ @gh = client
+ @user = user
+ @repo = repo
+ end
+
+ def gh
+ @gh.repos(user, repo)
+ end
+
+
+ include Assignees
+ include Labels
+ include Settings
+ end
+
+end
View
@@ -64,7 +64,7 @@ def create_label(user_name, repo, params)
end
def assignees(user_name, repo)
- gh.repos(user_name,repo).assignees.all
+ Huboard.board_for(user_name, repo).assignees
end
def get_milestones(user_name, repo)
@@ -115,7 +115,7 @@ def close_issue(user_name, repo, issue)
end
def labels(user_name, repo)
- gh.repos(user_name, repo).labels
+ Huboard.board_for(user_name, repo).labels
end
end
View
@@ -1,7 +1,7 @@
require 'ghee'
#require 'rack-cache'
#require 'active_support/cache'
-module Huboard
+class Huboard
module Common
module Settings
@@ -121,10 +121,19 @@ def h(input = "")
end
def gh(token = nil)
- @gh ||= Ghee.new(:access_token => token || user_token) do |conn|
- conn.use FaradayMiddleware::Caching, cache
- conn.use Mimetype
+ Huboard.configure do |client|
+ client.api_endpoint = ENV['GITHUB_API_ENDPOINT'] || 'https://api.github.com'
+
+ client.faraday_config do |conn|
+ conn.use FaradayMiddleware::Caching, cache
@eddanger

eddanger Apr 19, 2013

This speeds up loading, but makes Huboard out of sync until the rack app is restarted. I'd prefer slow loads to out of sync data.

+ conn.use Mimetype
+ end
+
+ client.access_token = token || user_token
end
+
+ Huboard.client
+
end
def socket_backend
View
@@ -1,6 +1,7 @@
require 'time'
require 'json'
require 'yaml'
+require_relative 'bridge/huboard'
module Stint
class Pebble
@@ -18,7 +19,7 @@ def build_backlog(user_name, repo)
return :milestones => milestones,
:unassigned => {:issues => issues.find_all {|i| i.milestone.nil? }, :milestone => {:title => "No milestone"}},
:assignees => github.assignees(user_name, repo).map{|a| a},
- other_labels: github.labels(user_name, repo).reject { |l| @huboard_patterns.any?{|p| p.match(l["name"]) } }
+ other_labels: Huboard.board_for(user_name, repo).other_labels
end
def build_board(user_name, repo)
@@ -55,26 +56,7 @@ def backlog_column(user_name, repo)
end
def settings(user_name, repo)
- defaults = {
- :show_all => true
- }
-
- labels = github.labels user_name, repo
-
- r = @settings_pattern
- settings = labels
- .select{|l| r.match l["name"]}
- .map do |l|
- match = r.match l["name"]
- begin
- YAML.load(match[1])
- rescue
- return {}
- end
- end.reduce(:merge)
-
- defaults.merge(settings || {})
-
+ Huboard.board_for(user_name,repo).settings
end
def board(user_name, repo)
@@ -344,24 +326,19 @@ def close_card(user_name, repo, the_issue)
end
def repos(user_name = nil)
- repos = github.repos user_name
- repos.sort_by{|r| r["pushed_at"] || "1111111111111111"}.reverse
+ Huboard.repos(user_name)
end
def all_repos
- the_repos = github.repos
- github.orgs.each do |org|
- the_repos.concat(github.repos(org["login"]))
- end
- the_repos.sort_by{|r| r["pushed_at"] || "1111111111111111"}.reverse
+ Huboard.all_repos
end
def initialize(github)
@github = github
- @column_pattern = /(?<id>\d+) *- *(?<name>.+)/
- @link_pattern = /Link <=> (?<user_name>.*)\/(?<repo>.*)/
- @settings_pattern = /@huboard:(.*)/
- @huboard_patterns = [@column_pattern, @link_pattern, @settings_pattern]
+ @column_pattern = Huboard.column_pattern
+ @link_pattern = Huboard.link_pattern
+ @settings_pattern = Huboard.settings_pattern
+ @huboard_patterns = Huboard.all_patterns
end
end
end

0 comments on commit a6ca3e6

Please sign in to comment.