Class: Joumae::Client
- Inherits:
-
Object
show all
- Defined in:
- lib/joumae/client.rb
Defined Under Namespace
Classes: ResourceAlreadyLockedError, ResourceNotFoundError, UnexpectedError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
permalink
#initialize(api_endpoint:, api_key:) ⇒ Client
Returns a new instance of Client.
[View source]
14
15
16
17
|
# File 'lib/joumae/client.rb', line 14
def initialize(api_endpoint:, api_key:)
@api_endpoint = api_endpoint
@api_key = api_key
end
|
Instance Attribute Details
permalink
#api_endpoint ⇒ Object
Returns the value of attribute api_endpoint.
7
8
9
|
# File 'lib/joumae/client.rb', line 7
def api_endpoint
@api_endpoint
end
|
Returns the value of attribute api_key.
8
9
10
|
# File 'lib/joumae/client.rb', line 8
def api_key
@api_key
end
|
Class Method Details
permalink
.create(api_endpoint: nil, api_key: nil) ⇒ Object
[View source]
10
11
12
|
# File 'lib/joumae/client.rb', line 10
def self.create(api_endpoint: nil, api_key: nil)
new(api_endpoint: api_endpoint || ENV['JOUMAE_API_ENDPOINT'], api_key: api_key || ENV['JOUMAE_API_KEY'])
end
|
Instance Method Details
permalink
#acquire(name) ⇒ Object
[View source]
23
24
25
|
# File 'lib/joumae/client.rb', line 23
def acquire(name)
post_json(acquire_resource_url(name), {api_key: api_key})
end
|
permalink
#acquire_resource_url(name) ⇒ Object
[View source]
71
72
73
|
# File 'lib/joumae/client.rb', line 71
def acquire_resource_url(name)
URI.parse("#{api_endpoint}/resources/#{name}/lock/acquire")
end
|
permalink
#create(name) ⇒ Object
[View source]
19
20
21
|
# File 'lib/joumae/client.rb', line 19
def create(name)
post_json(resources_url, {api_key: api_key, name: name})
end
|
permalink
#post_json(url, params = {}) ⇒ Object
[View source]
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/joumae/client.rb', line 35
def post_json(url, params={})
begin
debug "POST #{url} #{params.to_json}"
request = Net::HTTP::Post.new(url.path, {'Content-Type' =>'application/json'})
request.body = params.to_json
https = Net::HTTP.new(url.hostname, 443)
https.use_ssl = true
response = https.start do
https.request(request)
end
if response.code == '404'
fail ResourceNotFoundError, "Not found."
elsif response.code == '423'
response_body_as_json = JSON.parse(response.body)
raise ResourceAlreadyLockedError, response_body_as_json["message"]
elsif response.code != '200'
fail UnexpectedError, "Unexpected status: #{response.code}"
end
debug "Code: #{response.code}"
debug "Result: #{response.body}"
response_body = JSON.parse(response.body)
response_body
rescue => e
logger.debug "Dumping HTTP response:\n#{response.inspect} #{response.body}"
fail e
end
end
|
permalink
#release(name) ⇒ Object
[View source]
31
32
33
|
# File 'lib/joumae/client.rb', line 31
def release(name)
post_json(release_resource_url(name), {api_key: api_key})
end
|
permalink
#release_resource_url(name) ⇒ Object
[View source]
79
80
81
|
# File 'lib/joumae/client.rb', line 79
def release_resource_url(name)
URI.parse("#{api_endpoint}/resources/#{name}/lock/release")
end
|
permalink
#renew(name) ⇒ Object
[View source]
27
28
29
|
# File 'lib/joumae/client.rb', line 27
def renew(name)
post_json(renew_resource_url(name), {api_key: api_key})
end
|
permalink
#renew_resource_url(name) ⇒ Object
[View source]
75
76
77
|
# File 'lib/joumae/client.rb', line 75
def renew_resource_url(name)
URI.parse("#{api_endpoint}/resources/#{name}/lock/renew")
end
|
permalink
#resources_url ⇒ Object
[View source]
67
68
69
|
# File 'lib/joumae/client.rb', line 67
def resources_url
URI.parse("#{api_endpoint}/resources")
end
|