Class: Stash::Explorer

Inherits:
Object
  • Object
show all
Defined in:
lib/stash-api/explorer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, server) ⇒ Explorer

Returns a new instance of Explorer.



8
9
10
11
12
13
14
15
16
17
# File 'lib/stash-api/explorer.rb', line 8

def initialize(username, password, server)
  raise 'API username must be specified' if !username
  raise 'API password must be specified' if !password
  raise 'Stash server must be specified' if !server
  @username = username
  @password = password
  @server = server

  @get_repos_url = File.join("https://#{@server}", 'rest', 'api', '1.0', 'projects')
end

Instance Attribute Details

#serverObject

Returns the value of attribute server.



6
7
8
# File 'lib/stash-api/explorer.rb', line 6

def server
  @server
end

Instance Method Details

#get_file(project_key, repo, path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/stash-api/explorer.rb', line 33

def get_file(project_key, repo, path)
  RestClient::Request.new(
    :method => :get,
    :url => URI::encode("#{File.join(@get_repos_url, project_key, 'repos', repo, 'browse', path)}?limit=1000"),
    :user => @username,
    :password => @password,
    :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result|
      return response.code.to_s.match(/^2\d{2}$/) ?  JSON::parse(response)['lines'].map{|t| t['text']}.join("\n") : nil
  end
end

#get_repositories(project_key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stash-api/explorer.rb', line 19

def get_repositories(project_key)
  repos = []
  RestClient::Request.new(
    :method => :get,
    :url => URI::encode("#{File.join(@get_repos_url, project_key, 'repos')}?limit=1000"),
    :user => @username,
    :password => @password,
    :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result|
      raise "Could not get repositories - #{JSON::pretty_generate(JSON::parse(response.body))}" if !response.code.to_s.match(/^2\d{2}$/)
      JSON::parse(response)['values'].each{ |h| repos << h['slug'] }
  end
  repos
end