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
#initialize(api_endpoint:, api_key:) ⇒ Client
Returns a new instance of Client.
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
#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
|
#api_key ⇒ Object
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
.create(api_endpoint: nil, api_key: nil) ⇒ Object
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
#acquire(name) ⇒ Object
23
24
25
|
# File 'lib/joumae/client.rb', line 23
def acquire(name)
post_json(acquire_resource_url(name), {api_key: api_key})
end
|
#acquire_resource_url(name) ⇒ Object
71
72
73
|
# File 'lib/joumae/client.rb', line 71
def acquire_resource_url(name)
URI.parse("#{api_endpoint}/resources/#{name}/lock/acquire")
end
|
#create(name) ⇒ Object
19
20
21
|
# File 'lib/joumae/client.rb', line 19
def create(name)
post_json(resources_url, {api_key: api_key, name: name})
end
|
#post_json(url, params = {}) ⇒ Object
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
|
#release(name) ⇒ Object
31
32
33
|
# File 'lib/joumae/client.rb', line 31
def release(name)
post_json(release_resource_url(name), {api_key: api_key})
end
|
#release_resource_url(name) ⇒ Object
79
80
81
|
# File 'lib/joumae/client.rb', line 79
def release_resource_url(name)
URI.parse("#{api_endpoint}/resources/#{name}/lock/release")
end
|
#renew(name) ⇒ Object
27
28
29
|
# File 'lib/joumae/client.rb', line 27
def renew(name)
post_json(renew_resource_url(name), {api_key: api_key})
end
|
#renew_resource_url(name) ⇒ Object
75
76
77
|
# File 'lib/joumae/client.rb', line 75
def renew_resource_url(name)
URI.parse("#{api_endpoint}/resources/#{name}/lock/renew")
end
|
#resources_url ⇒ Object
67
68
69
|
# File 'lib/joumae/client.rb', line 67
def resources_url
URI.parse("#{api_endpoint}/resources")
end
|