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 => gogs_host,
  :port => gogs_port,
  :nonblock => false,
  :scheme => 'http'
}
API_REST =
"/api/v1"
VERSION =
"0.6.0"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Gogs

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



71
72
73
# File 'lib/megam/gogs.rb', line 71

def initialize(options={})
  @options = OPTIONS.merge(options)
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

#last_responseObject



66
67
68
# File 'lib/megam/gogs.rb', line 66

def last_response
  @last_response
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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/megam/gogs.rb', line 75

def request(params,&block)
  #just_color_debug("#{@options[:path]}")
  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)

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

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).to_s}s)")
  # reset (non-persistent) connection
  #@connection_token.reset
end

  response

end

#textObject



62
63
64
# File 'lib/megam/gogs.rb', line 62

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