Class: RakeCircleCI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rake_circle_ci/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Client

Returns a new instance of Client.



75
76
77
78
79
# File 'lib/rake_circle_ci/client.rb', line 75

def initialize(opts)
  @urls = URLs.new(opts)
  @api_token = opts[:api_token]
  @http = HTTPClient.new
end

Instance Method Details

#create_checkout_key(type) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/rake_circle_ci/client.rb', line 148

def create_checkout_key(type)
  type_strings = {
    deploy_key: 'deploy-key',
    github_user_key: 'github-user-key'
  }
  body = JSON.dump(type: type_strings[type.to_sym] || type.to_s)
  @http.post(@urls.checkout_keys_url, { body:, headers: })
end

#create_env_var(name, value) ⇒ Object



91
92
93
94
# File 'lib/rake_circle_ci/client.rb', line 91

def create_env_var(name, value)
  body = JSON.dump(name:, value:)
  @http.post(@urls.env_vars_url, { body:, headers: })
end

#create_ssh_key(private_key, opts = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/rake_circle_ci/client.rb', line 113

def create_ssh_key(private_key, opts = {})
  body = {
    fingerprint: SSHKey.new(private_key).sha1_fingerprint,
    private_key:
  }
  body = body.merge(hostname: opts[:hostname]) if opts[:hostname]
  body = JSON.dump(body)
  @http.post(@urls.ssh_keys_url, { body:, headers: })
end

#delete_checkout_key(fingerprint) ⇒ Object



157
158
159
# File 'lib/rake_circle_ci/client.rb', line 157

def delete_checkout_key(fingerprint)
  @http.delete(@urls.checkout_key_url(fingerprint), { headers: })
end

#delete_checkout_keysObject



161
162
163
164
165
166
# File 'lib/rake_circle_ci/client.rb', line 161

def delete_checkout_keys
  checkout_keys = find_checkout_keys
  checkout_keys.each do |checkout_key|
    delete_checkout_key(checkout_key[:fingerprint])
  end
end

#delete_env_var(name) ⇒ Object



96
97
98
# File 'lib/rake_circle_ci/client.rb', line 96

def delete_env_var(name)
  @http.delete(@urls.env_var_url(name), { headers: })
end

#delete_env_varsObject



100
101
102
103
104
105
# File 'lib/rake_circle_ci/client.rb', line 100

def delete_env_vars
  env_vars = find_env_vars
  env_vars.each do |env_var|
    delete_env_var(env_var)
  end
end

#delete_ssh_key(fingerprint, opts = {}) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/rake_circle_ci/client.rb', line 123

def delete_ssh_key(fingerprint, opts = {})
  body = {
    fingerprint:
  }
  body = body.merge(hostname: opts[:hostname]) if opts[:hostname]
  body = JSON.dump(body)
  @http.delete(@urls.ssh_keys_url, { body:, headers: })
end

#delete_ssh_keysObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/rake_circle_ci/client.rb', line 132

def delete_ssh_keys
  ssh_keys = find_ssh_keys
  ssh_keys.each do |ssh_key|
    fingerprint = ssh_key[:fingerprint]
    hostname = ssh_key[:hostname]
    options = hostname && { hostname: }
    args = [fingerprint, options].compact
    delete_ssh_key(*args)
  end
end

#find_checkout_keysObject



143
144
145
146
# File 'lib/rake_circle_ci/client.rb', line 143

def find_checkout_keys
  response = @http.get(@urls.checkout_keys_url, { headers: })
  JSON.parse(response.body, symbolize_names: true)
end

#find_env_varsObject



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

def find_env_vars
  response = @http.get(@urls.env_vars_url, { headers: })
  body = JSON.parse(response.body)
  body['items'].map { |item| item['name'] }
end

#find_ssh_keysObject



107
108
109
110
111
# File 'lib/rake_circle_ci/client.rb', line 107

def find_ssh_keys
  response = @http.get(@urls.settings_url, { headers: })
  body = JSON.parse(response.body, symbolize_names: true)
  body[:ssh_keys]
end

#follow_projectObject



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

def follow_project
  @http.post(@urls.follow_url, { headers: })
end