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, #assert_required_values_present, #parse_values

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



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

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



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

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

#_merge_mime_type(resource, params) ⇒ Object



100
101
102
103
# File 'lib/bitbucket_rest_api/api.rb', line 100

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:



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

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

#_merge_user_repo_into_params!(params) ⇒ Object

:nodoc:



96
97
98
# File 'lib/bitbucket_rest_api/api.rb', line 96

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:



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

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
# File 'lib/bitbucket_rest_api/api/actions.rb', line 19

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

#process_basic_auth(auth) ⇒ Object

Extract login and password from basic_auth parameter



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

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



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

def set_api_client
  BitBucket.api_client = self
end

#setup(options = {}) ⇒ Object



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

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

#update_and_validate_user_repo_params(user_name, repo_name = nil) ⇒ Object



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

def update_and_validate_user_repo_params(user_name, repo_name=nil)
  _update_user_repo_params(user_name, repo_name)
  _validate_user_repo_params(user, repo) unless user? && repo?
end