Class: Joumae::Client

Inherits:
Object
  • 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:, ttl:) ⇒ Client

Returns a new instance of Client.



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

def initialize(api_endpoint:, api_key:, ttl:)
  @api_endpoint = api_endpoint
  @api_key = api_key
  @ttl = ttl
end

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



7
8
9
# File 'lib/joumae/client.rb', line 7

def api_endpoint
  @api_endpoint
end

#api_keyObject (readonly)

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, ttl: 15) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/joumae/client.rb', line 10

def self.create(api_endpoint: nil, api_key: nil, ttl: 15)
  new(
    api_endpoint: api_endpoint || ENV['JOUMAE_API_ENDPOINT'],
    api_key: api_key || ENV['JOUMAE_API_KEY'],
	ttl: ttl
  )
end

Instance Method Details

#acquire(name, ttl: nil) ⇒ Object



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

def acquire(name, ttl: nil)
  params = {api_key: api_key, ttl: ttl || @ttl}
  post_json(acquire_resource_url(name), params)
end

#acquire_resource_url(name) ⇒ Object



78
79
80
# File 'lib/joumae/client.rb', line 78

def acquire_resource_url(name)
  URI.parse("#{api_endpoint}/resources/#{name}/lock/acquire")
end

#create(name) ⇒ Object



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

def create(name)
  post_json(resources_url, {api_key: api_key, name: name})
end

#post_json(url, params = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/joumae/client.rb', line 42

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
    # response = httpclient.post_content(url, params.to_json, header: {'Content-Type' => 'application/json'})

    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



38
39
40
# File 'lib/joumae/client.rb', line 38

def release(name)
  post_json(release_resource_url(name), {api_key: api_key})
end

#release_resource_url(name) ⇒ Object



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

def release_resource_url(name)
  URI.parse("#{api_endpoint}/resources/#{name}/lock/release")
end

#renew(name, ttl: nil) ⇒ Object



33
34
35
36
# File 'lib/joumae/client.rb', line 33

def renew(name, ttl: nil)
  params = {api_key: api_key, ttl: ttl || @ttl}
  post_json(renew_resource_url(name), params)
end

#renew_resource_url(name) ⇒ Object



82
83
84
# File 'lib/joumae/client.rb', line 82

def renew_resource_url(name)
  URI.parse("#{api_endpoint}/resources/#{name}/lock/renew")
end

#resources_urlObject



74
75
76
# File 'lib/joumae/client.rb', line 74

def resources_url
  URI.parse("#{api_endpoint}/resources")
end