Module: GitBak::Services

Defined in:
lib/gitbak/services.rb

Overview

git hosting services

Defined Under Namespace

Classes: AuthError

Constant Summary collapse

SERVICES =

avaliable services

%w{ bitbucket github gist }.map(&:to_sym)
USE_AUTH =

use another service’s authentication

{ gist: :github }
APIS =

API urls

{
  bitbucket:  ->(user) { "api.bitbucket.org/1.0/users/#{user}"  },
  github:     ->(user) { "api.github.com/users/#{user}/repos"   },
  gist:       ->(user) { "api.github.com/users/#{user}/gists"   },
}
REMOTES =

remote urls

{
  bitbucket: {
    ssh:    ->(u, r)  { "[email protected]:#{u}/#{r}.git"     },
    https:  ->(u, r)  { "https://bitbucket.org/#{u}/#{r}.git" },
  },
  github: {
    ssh:    ->(u, r)  { "[email protected]:#{u}/#{r}.git"        },
    https:  ->(u, r)  { "https://github.com/#{u}/#{r}.git"    },
  },
  gist: {
    ssh:    ->(id)    { "[email protected]:#{id}.git"       },
    https:  ->(id)    { "https://gist.github.com/#{id}.git"   },
  },
}
AUTH =

long keyword^wsymbol ;-)

:http_basic_authentication

Class Method Summary collapse

Class Method Details

.api_get(service, user, auth) ⇒ Object

get data from API

Raises:

  • AuthError on 401



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gitbak/services.rb', line 67

def self.api_get (service, user, auth)                      # {{{1
  url   = "https://#{APIS[service][user]}"
  opts  = auth ? { AUTH => [auth[:user], auth[:pass]] } : {}

  begin
    data = open url, opts
  rescue OpenURI::HTTPError => e
    if e.io.status[0] == '401'
      raise AuthError,
        "401 for #{auth[:user]} on #{service}/#{user}"
    else
      raise
    end
  end

  data
end

.bitbucket(cfg, data, rem) ⇒ Object

turn bitbucket API data into a list of repositories



100
101
102
103
104
105
106
107
# File 'lib/gitbak/services.rb', line 100

def self.bitbucket (cfg, data, rem)
  data_ = JSON.load data
  repos = data_['repositories'].select { |r| r['scm'] == 'git' }
  repos.map do |r|
    { name: r['name'], remote: rem[cfg[:user], r['name']],
      description: r['description'] }
  end
end

.gist(cfg, data, rem) ⇒ Object

turn gist API data into a list of repositories



118
119
120
121
122
123
# File 'lib/gitbak/services.rb', line 118

def self.gist (cfg, data, rem)
  JSON.load(data).map do |r|
    { name: r['id'], remote: rem[r['id']],
      description: r['description'] }
  end
end

.github(cfg, data, rem) ⇒ Object

turn github API data into a list of repositories



110
111
112
113
114
115
# File 'lib/gitbak/services.rb', line 110

def self.github (cfg, data, rem)
  JSON.load(data).map do |r|
    { name: r['name'], remote: rem[cfg[:user], r['name']],
      description: r['description'] }
  end
end

.repositories(service, cfg, auth) ⇒ <Hash>

get repositories from service; uses api_get if service in APIS, api_get_<service> otherwise

Returns:

  • (<Hash>)
    {name:, remote:, description:},…


89
90
91
92
93
94
95
# File 'lib/gitbak/services.rb', line 89

def self.repositories (service, cfg, auth)
  rem   = REMOTES[service][cfg.fetch(:method, :ssh).to_sym]
  args  = [service, cfg[:user], auth]
  data  = APIS[service] ? api_get(*args) :
            send("api_get_#{service}", *args)
  send service, cfg, data, rem
end