Class: Bitbucket::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bitbucket/base.rb

Direct Known Subclasses

Changesets, Repository, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bitbucket/base.rb', line 7

def initialize options = nil
  if options.nil?
    options = Bitbucket.default_auth_options
  end
  if options[:token] && options[:secret]
    @access_token = OAuth::AccessToken.new(
      Bitbucket.config.oauth_consumer,
      options[:token], options[:secret]
    )
  else
    # TODO for basic auth method...
    @usernaem = options[:username]
    @password = options[:password]
  end
  if @username.nil? && @access_token.nil?
    raise Error
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



5
6
7
# File 'lib/bitbucket/base.rb', line 5

def access_token
  @access_token
end

Instance Method Details

#delete(path) ⇒ Object



53
54
55
56
57
58
# File 'lib/bitbucket/base.rb', line 53

def delete path
  res = @access_token.delete(path)
  # value() raise Net::HTTPError
  res.value
  return res.body
end

#get(path, data = nil, raw = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bitbucket/base.rb', line 27

def get path, data = nil, raw = false
  if !data.nil? && data.is_a?(Hash)
    path = path + '?' +
      data.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')
  end
  res = @access_token.get(path)
  # value() raise Net::HTTPError
  res.value
  unless raw
    return JSON.parse(res.body)
  else
    return res.body
  end
end

#post(path, data = nil, raw = false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/bitbucket/base.rb', line 42

def post path, data = nil, raw = false
  res = @access_token.post(path,data)
  # value() raise Net::HTTPError
  res.value
  unless raw
    return JSON.parse(res.body)
  else
    return res.body
  end
end