Class: BitBucket::API

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
Authorization, Constants, Normalizer, ParameterFilter, Request::Verbs, Validations
Defined in:
lib/bitbucket_rest_api/api.rb,
lib/bitbucket_rest_api/api/config.rb,
lib/bitbucket_rest_api/api/actions.rb,
lib/bitbucket_rest_api/api/factory.rb,
lib/bitbucket_rest_api/api/arguments.rb,
lib/bitbucket_rest_api/api/config/property.rb,
lib/bitbucket_rest_api/api/config/property_set.rb

Defined Under Namespace

Classes: Arguments, Config, Factory

Constant Summary

Constants included from Validations

Validations::VALID_API_KEYS

Constants included from Validations::Token

Validations::Token::TOKEN_REQUIRED, Validations::Token::TOKEN_REQUIRED_REGEXP

Constants included from Constants

Constants::ACCEPT, Constants::ACCEPT_CHARSET, Constants::CACHE_CONTROL, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::DATE, Constants::ETAG, Constants::LOCATION, Constants::META_FIRST, Constants::META_LAST, Constants::META_NEXT, Constants::META_PREV, Constants::META_REL, Constants::PARAM_PAGE, Constants::PARAM_START_PAGE, Constants::QUERY_STR_SEP, Constants::RATELIMIT_LIMIT, Constants::RATELIMIT_REMAINING, Constants::SERVER, Constants::USER_AGENT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

configuration, configure, require_all

Methods included from Normalizer

#normalize!

Methods included from ParameterFilter

#filter!

Methods included from Validations::Required

#assert_required_keys

Methods included from Validations::Token

#validates_token_for

Methods included from Validations::Format

#assert_valid_values

Methods included from Validations::Presence

#assert_presence_of

Methods included from Request::Verbs

#delete_request, #get_request, #options_request, #patch_request, #post_request, #put_request

Methods included from Authorization

#authenticated?, #authentication, #basic_authed?

Constructor Details

#initialize(options = {}, &block) ⇒ API

Creates new API



46
47
48
49
50
# File 'lib/bitbucket_rest_api/api.rb', line 46

def initialize(options={}, &block)
  super()
  setup(options)
  yield_or_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Responds to attribute query or attribute clear



86
87
88
89
90
91
92
93
94
95
# File 'lib/bitbucket_rest_api/api.rb', line 86

def method_missing(method, *args, &block) # :nodoc:
  case method.to_s
  when /^(.*)\?$/
    return !self.send($1.to_s).nil?
  when /^clear_(.*)$/
    self.send("#{$1.to_s}=", nil)
  else
    super
  end
end

Instance Attribute Details

#current_optionsObject

Returns the value of attribute current_options.



33
34
35
# File 'lib/bitbucket_rest_api/api.rb', line 33

def current_options
  @current_options
end

Class Method Details

.extract_class_name(name, options) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts class name from options

Parameters:

Options Hash (options):

  • :full_name (String)

    the full name for the class

  • :root (Boolean)

    if the class is at the root or not

Returns:

  • (String)


182
183
184
185
186
187
188
# File 'lib/bitbucket_rest_api/api.rb', line 182

def self.extract_class_name(name, options)
  converted  = options.fetch(:full_name, name).to_s
  converted  = converted.split('_').map(&:capitalize).join
  class_name = options.fetch(:root, false) ? '': "#{self.name}::"
  class_name += converted
  class_name
end

.inherited(klass) ⇒ Object

Returns all API public methods for a given class.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bitbucket_rest_api/api/actions.rb', line 7

def self.inherited(klass)
  klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
    def self.actions
      self.new.api_methods_in(#{klass})
    end
    def actions
      api_methods_in(#{klass})
    end
  RUBY_EVAL
  super
end

.namespace(*names) ⇒ self

Defines a namespace

Parameters:

  • names (Array[Symbol])

    the name for the scope

Returns:

  • (self)


157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/bitbucket_rest_api/api.rb', line 157

def self.namespace(*names)
  options = names.last.is_a?(Hash) ? names.pop : {}
  names   = names.map(&:to_sym)
  name    = names.pop
  return if public_method_defined?(name)

  class_name = extract_class_name(name, options)
  define_method(name) do |*args, &block|
    options = args.last.is_a?(Hash) ? args.pop : {}
    API::Factory.new(class_name, current_options.merge(options), &block)
  end
  self
end

Instance Method Details

#_merge_user_into_params!(params) ⇒ Object

:nodoc:



195
196
197
# File 'lib/bitbucket_rest_api/api.rb', line 195

def _merge_user_into_params!(params)  #  :nodoc:
  params.merge!({ 'user' => self.user }) if user?
end

#_merge_user_repo_into_params!(params) ⇒ Object

:nodoc:



199
200
201
# File 'lib/bitbucket_rest_api/api.rb', line 199

def _merge_user_repo_into_params!(params)   #  :nodoc:
  { 'user' => self.user, 'repo' => self.repo }.merge!(params)
end

#_update_user_repo_params(user_name, repo_name = nil) ⇒ Object

:nodoc:



190
191
192
193
# File 'lib/bitbucket_rest_api/api.rb', line 190

def _update_user_repo_params(user_name, repo_name=nil) # :nodoc:
  self.user = user_name || self.user
  self.repo = repo_name || self.repo
end

#api_methods_in(klass) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bitbucket_rest_api/api/actions.rb', line 19

def api_methods_in(klass)
  puts "---"
  (klass.send(:instance_methods, false) - ['actions']).sort.each do |method|
    puts "|--> #{method}"
  end
  klass.included_modules.each do |mod|
    if mod.to_s =~ /#{klass}/
      puts "| \\ #{mod.to_s}"
      mod.instance_methods(false).each do |met|
        puts "|  |--> #{met}"
      end
      puts "| /"
    end
  end
  puts "---"
  nil
end

#append_arguments(method) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bitbucket_rest_api/api/actions.rb', line 37

def append_arguments(method)
  _method = self.method(method)
  if _method.arity == 0
    args = "()"
  elsif _method.arity > 0
    args = "(few)"
  else
    args = "(else)"
  end
  args
end

#arguments(args = (not_set = true), options = {}, &block) ⇒ Object

Returns Arguments instance.



101
102
103
104
105
106
107
# File 'lib/bitbucket_rest_api/api.rb', line 101

def arguments(args=(not_set = true), options={}, &block)
  if not_set
    @arguments
  else
    @arguments = Arguments.new(options.merge!(api: self)).parse(*args, &block)
  end
end

#process_basic_auth(auth) ⇒ Object

Extract login and password from basic_auth parameter



70
71
72
73
74
75
76
77
78
# File 'lib/bitbucket_rest_api/api.rb', line 70

def process_basic_auth(auth)
  case auth
  when String
    self., self.password = auth.split(':', 2)
  when Hash
    self.    = auth[:login]
    self.password = auth[:password]
  end
end

#set(option, value = (not_set=true), ignore_setter = false, &block) ⇒ self

Set a configuration option for a given namespace

Parameters:

  • option (String)
  • value (Object) (defaults to: (not_set=true))
  • ignore_setter (Boolean) (defaults to: false)

Returns:

  • (self)

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bitbucket_rest_api/api.rb', line 132

def set(option, value=(not_set=true), ignore_setter=false, &block)
  raise ArgumentError, 'value not set' if block and !not_set
  return self if !not_set and value.nil?

  if not_set
    set_options option
    return self
  end

  if respond_to?("#{option}=") and not ignore_setter
    return __send__("#{option}=", value)
  end

  define_accessors option, value
  self
end

#set_api_clientObject

Assigns current api class



81
82
83
# File 'lib/bitbucket_rest_api/api.rb', line 81

def set_api_client
  BitBucket.api_client = self
end

#setup(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bitbucket_rest_api/api.rb', line 57

def setup(options={})
  options = BitBucket.configuration.fetch.merge(options)
  self.current_options = options
  if self.class.instance_variable_get('@version') == '2.0'
    options[:endpoint] = BitBucket.endpoint_v2
  end
  BitBucket.configuration.property_names.each do |key|
    send("#{key}=", options[key])
  end
  process_basic_auth(options[:basic_auth])
end

#with(args) ⇒ Object

Scope for passing request required arguments.



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bitbucket_rest_api/api.rb', line 111

def with(args)
  case args
  when Hash
    set args
  when /.*\/.*/i
    user, repo = args.split('/')
    set :user => user, :repo => repo
  else
    ::Kernel.raise ArgumentError, 'This api does not support passed in arguments'
  end
end

#yield_or_eval(&block) ⇒ Object



52
53
54
55
# File 'lib/bitbucket_rest_api/api.rb', line 52

def yield_or_eval(&block)
  return unless block
  block.arity > 0 ? yield(self) : self.instance_eval(&block)
end