Class: ForestLiana::AllowedUsersGetter

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/allowed_users_getter.rb

Instance Method Summary collapse

Instance Method Details

#perform(renderingId) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/forest_liana/allowed_users_getter.rb', line 3

def perform(renderingId)
  uri = URI.parse("#{forest_url}/forest/renderings/#{renderingId}/allowed-users")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if forest_url.start_with?('https')

  begin
    http.start do |client|
      request = Net::HTTP::Get.new(uri.path)
      request['Content-Type'] = 'application/json'
      request['forest-secret-key'] = ForestLiana.env_secret
      response = client.request(request)

      if response.is_a?(Net::HTTPOK)
        body = JSON.parse(response.body)
        ForestLiana.allowed_users = body['data'].map do |d|
          user = d['attributes']
          user['id'] = d['id']

          user
        end
      elsif response.is_a?(Net::HTTPNotFound)
        FOREST_LOGGER.error "Cannot retrieve the project you\'re trying " \
          "to unlock. Can you check that you properly copied the Forest " \
          "env_secret in the forest_liana initializer?"
      else
        FOREST_LOGGER.error "Cannot retrieve any users for the project " \
          "you\'re trying to unlock. An error occured in Forest API."
        []
      end
    end
  rescue => exception
    FOREST_LOGGER.error "Cannot retrieve any users for the project " \
      "you\'re trying to unlock. Forest API seems to be down right now."
  end
end