Module: Unchanging

Defined in:
lib/unchanging.rb,
lib/unchanging/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

API =
"http://127.0.0.1:5001/api/v0"
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.<<(content) ⇒ String

Upload content to IPFS and return the CID

Parameters:

  • content (String, File)

    String content or File object to upload

Returns:

  • (String)

    CID (Content Identifier) of uploaded content



21
22
23
24
25
26
27
28
29
# File 'lib/unchanging.rb', line 21

def self.<< content
  uri = URI(Unchanging::API + "/add")
  request = Net::HTTP::Post.new(uri)
  request.set_form([['file', content]], 'multipart/form-data')
  response = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(request) }
  raise "HTTP Error: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
  result = JSON.parse(response.body)
  result['Hash']
end

.[](cid) ⇒ String

Retrieve content from IPFS by CID

Parameters:

  • cid (String)

    Content Identifier to retrieve

Returns:

  • (String)

    Content as string



34
35
36
37
38
39
40
41
# File 'lib/unchanging.rb', line 34

def self.[] cid
  uri = URI(Unchanging::API + "/cat")
  uri.query = URI.encode_www_form(arg: cid)
  request = Net::HTTP::Post.new(uri)
  response = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(request) }
  raise "HTTP Error: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
  response.body
end

.url(cid) ⇒ String

Get public url for cid resource

Parameters:

  • cid (String)

    Content Identifier to use

Returns:

  • (String)

    public url



14
15
16
# File 'lib/unchanging.rb', line 14

def self.url cid
  %[https://ipfs.io/ipfs/#{cid}]
end