class Megam::Gogs

Constants

API_REST
AUTH_PREFIX
HEADERS
OPTIONS
VERSION

Attributes

last_response[R]

Public Class Methods

new(options = {}) click to toggle source

It is assumed that every API call will NOT use an API_KEY/email.

# File lib/megam/gogs.rb, line 52
def initialize(options = {})
  @options = OPTIONS.merge(options)
end

Public Instance Methods

get_accounts(uname) click to toggle source
GET /accounts

Yet to be tested

# File lib/megam/gogs/accounts.rb, line 5
def get_accounts(uname)

  @options = {:path => "/users/:uname",
    :body => ''}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end
get_repos(token=nil) click to toggle source

Yet to be tested

# File lib/megam/gogs/repos.rb, line 6
def get_repos(token=nil)



  @options = {:path => "/user/repos",
    :body => ''}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :token => token,
    :body => @options[:body]

  )
end
get_tokens(username=nil, password=nil) click to toggle source
# File lib/megam/gogs/tokens.rb, line 4
def get_tokens(username=nil, password=nil)


  @options = {:path => "/users/#{username}/tokens",
    :body => ''}.merge(@options)
   
    request(
    :expects  => 200,
    :method   => :get,
    :username => username,
    :password => password,
    :body     => @options[:body]
   )


  end
post_accounts(json_hash) click to toggle source

The body content needs to be a json.

# File lib/megam/gogs/accounts.rb, line 18
def post_accounts(json_hash)
  @options = {:path => '/users.json',
  :body => json_hash[:json]}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
    )
end
request(params, &block) click to toggle source
# File lib/megam/gogs.rb, line 56
def request(params, &block)
  start = Time.now

  dummy_params =  {
    expects: params[:expects],
    method: params[:method],
    body: params[:body]
  }

  text.msg "#{text.color('START', :cyan, :bold)}"
  params.each do |pkey, pvalue|
    text.msg("> #{pkey}: #{pvalue}")
  end

  if params[:token].nil?
    @uname = params[:username]
    @pass = params[:password]
    @cred = "#{@uname}:#{@pass}"
    @final_cred64 = Base64.encode64(@cred)

    @final_hash = { creds: "Basic #{@final_cred64}" }

    response = connection_repo.request(dummy_params, &block)
    text.msg("END(#{(Time.now - start)}s)")
  else

    @tokens = params[:token]
    @final_token = { token: "token #{@tokens}" }
    response = connection_token.request(dummy_params, &block)

    puts response.inspect
    text.msg("END(#{(Time.now - start)}s)")
    # reset (non-persistent) connection
    # @connection_token.reset
  end

  response
end
text() click to toggle source
# File lib/megam/gogs.rb, line 45
def text
  @text ||= Megam::Dumpout.new(STDOUT, STDERR, STDIN)
end

Private Instance Methods

connection_repo() click to toggle source

Make a lazy connection.

# File lib/megam/gogs.rb, line 98
def connection_repo
  @options[:path] = API_REST + @options[:path]

  @options[:headers] = HEADERS.merge(AUTH_PREFIX => @final_hash[:creds]).merge(@options[:headers])

  text.msg('HTTP Request Data:')
  text.msg("> HTTP #{@options[:scheme]}://#{@options[:host]}")
  @options.each do |key, value|
    text.msg("> #{key}: #{value}")
  end
  text.msg('End HTTP Request Data.')
  if @options[:scheme] == 'https'
    @connection = Excon.new("#{@options[:scheme]}://#{@options[:host]}", @options)
  else
    Excon.defaults[:ssl_verify_peer] = false
    @connection = Excon.new("#{@options[:scheme]}://#{@options[:host]}:6001", @options)
  end
  @connection
  end
connection_token() click to toggle source
# File lib/megam/gogs.rb, line 118
def connection_token
  @options[:path] = API_REST + @options[:path]
  @options[:headers] = HEADERS.merge(AUTH_PREFIX => @final_token[:token]).merge(@options[:headers])
  text.msg('HTTP Request Data:')
  text.msg("> HTTP #{@options[:scheme]}://#{@options[:host]}")
  @options.each do |key, value|
    text.msg("> #{key}: #{value}")
  end
  text.msg('End HTTP Request Data.')
  if @options[:scheme] == 'https'
    @connection = Excon.new("#{@options[:scheme]}://#{@options[:host]}", @options)
  else
    Excon.defaults[:ssl_verify_peer] = false
    @connection = Excon.new("#{@options[:scheme]}://#{@options[:host]}:6001", @options)
  end
  @connection
  end