Class: Bucketkit::Client

Inherits:
Object
  • Object
show all
Includes:
Authentication, Commits, PullRequests, Configurable
Defined in:
lib/bucketkit/client.rb,
lib/bucketkit/client/commits.rb,
lib/bucketkit/client/pull_requests.rb

Defined Under Namespace

Modules: Commits, PullRequests

Constant Summary collapse

CONVENIENCE_HEADERS =
Set.new([:accept, :content_type])

Constants included from Commits

Commits::API_ENDPOINT

Constants included from PullRequests

PullRequests::API_ENDPOINT

Instance Attribute Summary

Attributes included from Configurable

#api_endpoint, #auto_paginate, #connection_options, #default_media_type, #login, #middleware, #oauth_tokens, #password, #per_page, #proxy, #user_agent, #web_endpoint

Instance Method Summary collapse

Methods included from Commits

#approve_commit, #commit, #commit_comment, #commit_comments, #commits, #delete_commit_approval

Methods included from PullRequests

#approve_pull_request, #create_pull_request, #decline_pull_request, #delete_pull_request_approval, #merge_pull_request, #pull_request, #pull_request_activity, #pull_request_comment, #pull_request_comments, #pull_request_commits, #pull_request_diff, #pull_requests, #update_pull_request

Methods included from Configurable

#configure, keys, #reset!

Methods included from Authentication

#basic_authenticated?, #oauth_authenticated?, #user_authenticated?

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
# File 'lib/bucketkit/client.rb', line 18

def initialize(options={})
  Bucketkit::Configurable.keys.each do |key|
    instance_variable_set(:"@#{key}", options[key] || Bucketkit.instance_variable_get(:"@#{key}"))
  end
end

Instance Method Details

#agentObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bucketkit/client.rb', line 95

def agent
  @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
    http.headers[:accept] = default_media_type
    http.headers[:content_type] = default_media_type
    http.headers[:user_agent] = user_agent
    if basic_authenticated?
      http.basic_auth(@login, @password)
    elsif oauth_authenticated?
      http.request :oauth, @oauth_tokens
    end
  end
end

#delete(url, options = {}) ⇒ Object



44
45
46
# File 'lib/bucketkit/client.rb', line 44

def delete(url, options={})
  request :delete, url, options
end

#get(url, options = {}) ⇒ Object



28
29
30
# File 'lib/bucketkit/client.rb', line 28

def get(url, options={})
  request :get, url, parse_query_and_convenience_headers(options)
end

#head(url, options = {}) ⇒ Object



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

def head(url, options={})
  request :head, url, parse_query_and_convenience_headers(options)
end

#last_responseObject



76
77
78
# File 'lib/bucketkit/client.rb', line 76

def last_response
  @last_response if defined? @last_response
end

#login=(value) ⇒ Object



80
81
82
83
# File 'lib/bucketkit/client.rb', line 80

def login=(value)
  reset_agent
  @login = value
end

#oauth_tokens=(value) ⇒ Object



90
91
92
93
# File 'lib/bucketkit/client.rb', line 90

def oauth_tokens=(value)
  reset_agent
  @oauth_tokens = value
end

#paginate(url, options = {}, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bucketkit/client.rb', line 52

def paginate(url, options={}, &block)
  opts = parse_query_and_convenience_headers(options.dup)
  if @auto_paginate || @per_page
    opts[:query][:per_page] ||= @per_page || (@auto_paginate ? 100 : nil)
  end
  data = request(:get, url, opts)
  if @auto_paginate
    loop do
      break unless @last_response.rels[:next]
      @last_response = @last_response.rels[:next].get
      if block_given?
        yield(data, @last_response)
      else
        data.concat(@last_response.data) if @last_response.data.is_a?(Array)
      end
    end
  end
  data
end

#password=(value) ⇒ Object



85
86
87
88
# File 'lib/bucketkit/client.rb', line 85

def password=(value)
  reset_agent
  @password = value
end

#patch(url, options = {}) ⇒ Object



40
41
42
# File 'lib/bucketkit/client.rb', line 40

def patch(url, options={})
  request :patch, url, options
end

#post(url, options = {}) ⇒ Object



32
33
34
# File 'lib/bucketkit/client.rb', line 32

def post(url, options={})
  request :post, url, options
end

#put(url, options = {}) ⇒ Object



36
37
38
# File 'lib/bucketkit/client.rb', line 36

def put(url, options={})
  request :put, url, options
end

#rootObject



72
73
74
# File 'lib/bucketkit/client.rb', line 72

def root
  get '/'
end

#same_options?(opts) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/bucketkit/client.rb', line 24

def same_options?(opts)
  opts.hash == options.hash
end