Class: Stash::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/stash/client.rb,
lib/stash/client/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stash/client.rb', line 11

def initialize(opts = {})
  if opts[:client]
    @client = opts[:client]
  else
    if opts[:host] && opts[:scheme]
      @url = Addressable::URI.parse(opts[:scheme] + '://' + opts[:host] + '/rest/api/1.0/')
    elsif opts[:host]
      @url = Addressable::URI.parse('http://' + opts[:host] + '/rest/api/1.0/')
    elsif opts[:url]
      @url = Addressable::URI.parse(opts[:url])
    elsif opts[:uri] && opts[:uri].kind_of?(Addressable::URI)
      @url = opts[:uri]
    else
      raise ArgumentError, "must provide :url or :host"
    end

    @url.userinfo = opts[:credentials] if opts[:credentials]

    @client = Faraday.new(@url.site)
  end

end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/stash/client.rb', line 9

def url
  @url
end

Instance Method Details

#changes_for(repo, sha, opts = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/stash/client.rb', line 91

def changes_for(repo, sha, opts = {})
  path = remove_leading_slash repo.fetch('link').fetch('url').sub('browse', 'changes')
  uri = @url.join(path)

  query_values = { 'until' =>  sha }
  query_values['since'] = opts[:parent] if opts[:parent]
  query_values['limit'] = opts[:limit] if opts[:limit]

  uri.query_values = query_values

  if query_values['limit']
    fetch(uri).fetch('values')
  else
    fetch_all(uri)
  end
end

#commits_for(repo, opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/stash/client.rb', line 67

def commits_for(repo, opts = {})
  query_values = {}

  path = remove_leading_slash repo.fetch('link').fetch('url').sub('browse', 'commits')
  uri = @url.join(path)

  query_values['since'] = opts[:since] if opts[:since]
  query_values['until'] = opts[:until] if opts[:until]
  query_values['limit'] = Integer(opts[:limit]) if opts[:limit]

  if query_values.empty?
    # default limit to 100 commits
    query_values['limit'] = 100
  end

  uri.query_values = query_values

  if query_values['limit'] && query_values['limit'] < 100
    fetch(uri).fetch('values')
  else
    fetch_all(uri)
  end
end

#create_project(opts = {}) ⇒ Object



38
39
40
# File 'lib/stash/client.rb', line 38

def create_project(opts={})
  post @url.join('projects'), opts
end

#delete_project(project) ⇒ Object



47
48
49
50
# File 'lib/stash/client.rb', line 47

def delete_project(project)
  relative_project_path = project.fetch('link').fetch('url')
  delete @url.join(remove_leading_slash(relative_project_path))
end

#project_named(name) ⇒ Object



59
60
61
# File 'lib/stash/client.rb', line 59

def project_named(name)
  projects.find { |e| e['name'] == name }
end

#projectsObject



34
35
36
# File 'lib/stash/client.rb', line 34

def projects
  fetch_all @url.join('projects')
end

#repositoriesObject



52
53
54
55
56
57
# File 'lib/stash/client.rb', line 52

def repositories
  projects.map do |project|
    relative_project_path = project.fetch('link').fetch('url') + '/repos'
    fetch_all @url.join(remove_leading_slash(relative_project_path))
  end.flatten
end

#repository_named(name) ⇒ Object



63
64
65
# File 'lib/stash/client.rb', line 63

def repository_named(name)
  repositories.find { |e| e['name'] == name }
end

#update_project(project, opts = {}) ⇒ Object



42
43
44
45
# File 'lib/stash/client.rb', line 42

def update_project(project, opts={})
  relative_project_path = project.fetch('link').fetch('url')
  put @url.join(remove_leading_slash(relative_project_path)), opts
end