Class: Cloutree::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cloutree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_keyObject

Returns the value of attribute app_key.



15
16
17
# File 'lib/cloutree.rb', line 15

def app_key
  @app_key
end

#app_secretObject

Returns the value of attribute app_secret.



15
16
17
# File 'lib/cloutree.rb', line 15

def app_secret
  @app_secret
end

#resultObject (readonly)

Returns the value of attribute result.



16
17
18
# File 'lib/cloutree.rb', line 16

def result
  @result
end

Class Method Details

.configure(hash) ⇒ Object

Raises:



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

def self.configure(hash)
  raise Cloutree::Error, "Specify :app_key" unless instance.app_key = hash[:app_key]
  raise Cloutree::Error, "Specify :app_secret" unless instance.app_secret = hash[:app_secret]
  instance
end

.resultObject



32
33
34
35
36
37
# File 'lib/cloutree.rb', line 32

def self.result
  unless instance.result 
    raise Cloutree::Error, "Try to upload something first. CC.upload(filepath)"
  end
  instance.result
end

.upload(file) ⇒ Object

Raises:



25
26
27
28
29
# File 'lib/cloutree.rb', line 25

def self.upload(file)
  raise Cloutree::Error, "Configure Cloutree::Client before uploading:  
    CC.configure(app_key: YOUR_KEY, app_secret: YOUR_SECRET)" unless instance.app_key && instance.app_secret
  instance.upload(file)
end

Instance Method Details

#upload(file) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cloutree.rb', line 40

def upload(file)
  filename = File.basename(file)
  time = Time.now.to_i
  string_to_sha = [app_key, time, filename, app_secret].join(":")
  checksum = Digest::SHA1.hexdigest(string_to_sha)

  c = Curl::Easy.new("https://cloutr.ee/upload")
  c.ssl_verify_peer = false
  c.headers["KEY"] = "#{app_key}"
  c.headers["CHECKSUM"] = "#{checksum}"
  c.headers["FILENAME"] = "#{filename}"
  c.headers["TIMESTAMP"] = "#{time}"
  c.http_post(File.read(file))
  @result = JSON.parse(c.body_str)
  @result["success"]
end