Class: Megam::Gogs

Inherits:
Object
  • Object
show all
Defined in:
lib/megam/gogs.rb,
lib/megam/gogs/repos.rb,
lib/megam/gogs/errors.rb,
lib/megam/gogs/tokens.rb,
lib/megam/gogs/version.rb,
lib/megam/gogs/accounts.rb

Defined Under Namespace

Modules: Errors

Constant Summary collapse

AUTH_PREFIX =
'Authorization'
HEADERS =
{
  'Accept' => 'application/json',
  'Accept-Encoding' => 'gzip',
  'User-Agent' => "megam-gogs/#{Megam::Gogs::VERSION}",
  'X-Ruby-Version' => RUBY_VERSION,
  'X-Ruby-Platform' => RUBY_PLATFORM
}
OPTIONS =
{
  headers: {},
  host: "localhost",
  port: "6001",
  nonblock: false,
  scheme: 'http'
}
API_REST =
'/api/v1'
VERSION =
"0.8.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Gogs

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



52
53
54
# File 'lib/megam/gogs.rb', line 52

def initialize(options = {})
  @options = OPTIONS.merge(options)
end

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



49
50
51
# File 'lib/megam/gogs.rb', line 49

def last_response
  @last_response
end

Instance Method Details

#get_accounts(uname) ⇒ Object

GET /accounts Yet to be tested



5
6
7
8
9
10
11
12
13
14
15
# 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) ⇒ Object

Yet to be tested



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object

The body content needs to be a json.



18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

#textObject



45
46
47
# File 'lib/megam/gogs.rb', line 45

def text
  @text ||= Megam::Dumpout.new(STDOUT, STDERR, STDIN)
end