Class: BitBucket::API

Inherits:
Object
  • Object
show all
Includes:
Authorization, Connection, Normalizer, ParameterFilter, Request, Validations
Defined in:
lib/bitbucket_rest_api/api.rb,
lib/bitbucket_rest_api/api/actions.rb

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 Request

Request::METHODS, Request::METHODS_WITH_BODIES

Constants included from Connection

Connection::ALLOWED_OPTIONS

Constants included from Constants

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Normalizer

#normalize!

Methods included from ParameterFilter

#filter!

Methods included from AutoloadHelper

#autoload_all, #lookup_constant, #register_constant

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

#_validate_presence_of, #_validate_user_repo_params

Methods included from Request

#delete_request, #get_request, #patch_request, #post_request, #put_request, #request

Methods included from Connection

#caching?, #clear_cache, #connection, #default_middleware, #default_options, #stack

Methods included from Authorization

#authenticated?, #authentication, #basic_authed?

Constructor Details

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

Creates new API



39
40
41
42
43
44
45
# File 'lib/bitbucket_rest_api/api.rb', line 39

def initialize(options={}, &block)
  super()
  setup options
  set_api_client

  self.instance_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



72
73
74
75
76
77
78
79
80
81
# File 'lib/bitbucket_rest_api/api.rb', line 72

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

Class Method Details

.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

Instance Method Details

#_hash_traverse(hash, &block) ⇒ Object

TODO: See whether still needed, consider adding to core_exts



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/bitbucket_rest_api/api.rb', line 97

def _hash_traverse(hash, &block)
  hash.each do |key, val|
    block.call(key)
    case val
    when Hash
      val.keys.each do |k|
        _hash_traverse(val, &block)
      end
    when Array
      val.each do |item|
        _hash_traverse(item, &block)
      end
    end
  end
  return hash
end

#_merge_mime_type(resource, params) ⇒ Object



114
115
116
117
# File 'lib/bitbucket_rest_api/api.rb', line 114

def _merge_mime_type(resource, params) # :nodoc:
                                       #       params['resource'] = resource
                                       #       params['mime_type'] = params['mime_type'] || :raw
end

#_merge_user_into_params!(params) ⇒ Object

:nodoc:



88
89
90
# File 'lib/bitbucket_rest_api/api.rb', line 88

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

#_merge_user_repo_into_params!(params) ⇒ Object

:nodoc:



92
93
94
# File 'lib/bitbucket_rest_api/api.rb', line 92

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:



83
84
85
86
# File 'lib/bitbucket_rest_api/api.rb', line 83

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

#process_basic_auth(auth) ⇒ Object

Extract login and password from basic_auth parameter



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

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_api_clientObject

Assigns current api class



67
68
69
# File 'lib/bitbucket_rest_api/api.rb', line 67

def set_api_client
  BitBucket.api_client = self
end

#setup(options = {}) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/bitbucket_rest_api/api.rb', line 47

def setup(options={})
  options = BitBucket.options.merge(options)
  Configuration::VALID_OPTIONS_KEYS.each do |key|
    send("#{key}=", options[key])
  end
  process_basic_auth(options[:basic_auth])
end