Class: SKP::Gateway
- Inherits:
-
Object
- Object
- SKP::Gateway
- Defined in:
- lib/skp/gateway.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#domain ⇒ Object
Returns the value of attribute domain.
Instance Method Summary collapse
- #authenticate_key(key) ⇒ Object
- #download_content(content, folder:) ⇒ Object
-
#initialize(domain, key) ⇒ Gateway
constructor
A new instance of Gateway.
- #latest_version? ⇒ Boolean
- #list_content ⇒ Object
- #register_email(email) ⇒ Object
Constructor Details
#initialize(domain, key) ⇒ Gateway
Returns a new instance of Gateway.
8 9 10 11 |
# File 'lib/skp/gateway.rb', line 8 def initialize(domain, key) @domain = domain @key = key end |
Instance Attribute Details
#domain ⇒ Object
Returns the value of attribute domain.
6 7 8 |
# File 'lib/skp/gateway.rb', line 6 def domain @domain end |
Instance Method Details
#authenticate_key(key) ⇒ Object
15 16 17 |
# File 'lib/skp/gateway.rb', line 15 def authenticate_key(key) Excon.get(domain + "/license", user: key).status == 200 end |
#download_content(content, folder:) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/skp/gateway.rb', line 29 def download_content(content, folder:) ::CLI::UI::Progress.progress do || downloaded_file = File.open("#{folder}/#{content["s3_key"]}.partial", "w") streamer = lambda do |chunk, remaining_bytes, total_bytes| downloaded_file.write(chunk) finished = (remaining_bytes.to_f / total_bytes.to_f) .tick(set_percent: (1 - finished).round(2)) end response = Excon.get(content["url"], response_block: streamer) unless response.status == 200 puts response.inspect raise Error.new("Server problem: #{response.status}") end downloaded_file.close File.rename(downloaded_file, "#{folder}/#{content["s3_key"]}") .tick(set_percent: 1) end end |
#latest_version? ⇒ Boolean
48 49 50 51 52 |
# File 'lib/skp/gateway.rb', line 48 def latest_version? resp = Excon.get("https://rubygems.org/api/v1/gems/skp.json") data = JSON.parse resp.body Gem::Version.new(SKP::VERSION) >= Gem::Version.new(data["version"]) end |
#list_content ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/skp/gateway.rb', line 19 def list_content response = Excon.get(domain + "/contents?product=sip", user: @key) if response.status == 200 JSON.parse(response.body) else puts response.inspect raise Error, "There was a problem fetching this content." end end |
#register_email(email) ⇒ Object
54 55 56 |
# File 'lib/skp/gateway.rb', line 54 def register_email(email) Excon.put(domain + "/license?email=#{email}&key=#{@key}").status == 200 end |