Class: Trove
- Inherits:
-
Object
- Object
- Trove
- Defined in:
- lib/trove_gem.rb
Class Method Summary collapse
- .grab(drawer, key, token, chest, host = "trove.imtins.com/api/v1") ⇒ Object
- .key(drawer, key, token_name = "trove_token", chest = Rails.env, host = "trove.imtins.com/api/v1") ⇒ Object
Class Method Details
.grab(drawer, key, token, chest, host = "trove.imtins.com/api/v1") ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/trove_gem.rb', line 30 def self.grab(drawer, key, token, chest, host = "trove.imtins.com/api/v1" ) limit = 10 url = URI("https://#{host}/#{chest}/#{drawer}") http = Net::HTTP.new(url.host, url.port); http.use_ssl = true request = Net::HTTP::Get.new(url) request["Authorization"] = "Bearer #{token}" raise ArgumentError, 'too many HTTP redirects' if limit == 0 response = http.request(request) if response.kind_of? Net::HTTPSuccess response = JSON.parse(response.read_body, symbolize_names: true) result = response[:data].find {|x| x[:key] == key} return result[:value] else return "Error: Not found" end end |
.key(drawer, key, token_name = "trove_token", chest = Rails.env, host = "trove.imtins.com/api/v1") ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/trove_gem.rb', line 6 def self.key(drawer, key, token_name = "trove_token", chest = Rails.env, host = "trove.imtins.com/api/v1" ) limit = 10 url = URI("https://#{host}/#{chest}/#{drawer}") http = Net::HTTP.new(url.host, url.port); http.use_ssl = true request = Net::HTTP::Get.new(url) request["Authorization"] = "Bearer #{Rails.application.credentials.dig(token_name.to_sym)}" raise ArgumentError, 'too many HTTP redirects' if limit == 0 response = http.request(request) if response.kind_of? Net::HTTPSuccess response = JSON.parse(response.read_body, symbolize_names: true) result = response[:data].find {|x| x[:key] == key} return result[:value] else return "Error: Not found" end end |