Class: Sidetree::CAS::IPFS
- Inherits:
-
Object
- Object
- Sidetree::CAS::IPFS
- Defined in:
- lib/sidetree/cas/ipfs.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#fetch_timeout ⇒ Object
readonly
Returns the value of attribute fetch_timeout.
Instance Method Summary collapse
-
#id ⇒ String
Get node information from IPFS endpoint.
-
#initialize(schema: "http", host: "localhost", port: 5001, base_path: "/api/v0", fetch_timeout: nil) ⇒ IPFS
constructor
A new instance of IPFS.
-
#read(addr) ⇒ Sidetree::CAS::FetchResult
Get content from IPFS.
-
#up? ⇒ Boolean
Check IPFS endpoint are up and running.
-
#write(content) ⇒ String
Writes the given content to CAS.
Constructor Details
#initialize(schema: "http", host: "localhost", port: 5001, base_path: "/api/v0", fetch_timeout: nil) ⇒ IPFS
Returns a new instance of IPFS.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/sidetree/cas/ipfs.rb', line 12 def initialize( schema: "http", host: "localhost", port: 5001, base_path: "/api/v0", fetch_timeout: nil ) @base_url = "#{schema}://#{host}:#{port}#{base_path}" @fetch_timeout = fetch_timeout raise Sidetree::Error, "Failed to connect to IPFS endpoint." unless up? end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
8 9 10 |
# File 'lib/sidetree/cas/ipfs.rb', line 8 def base_url @base_url end |
#fetch_timeout ⇒ Object (readonly)
Returns the value of attribute fetch_timeout.
9 10 11 |
# File 'lib/sidetree/cas/ipfs.rb', line 9 def fetch_timeout @fetch_timeout end |
Instance Method Details
#id ⇒ String
Get node information from IPFS endpoint.
70 71 72 73 |
# File 'lib/sidetree/cas/ipfs.rb', line 70 def id res = Net::HTTP.post_form(URI("#{base_url}/id"), {}) res.body if res.is_a?(Net::HTTPSuccess) end |
#read(addr) ⇒ Sidetree::CAS::FetchResult
Get content from IPFS. The result code is set to FetchResultCode.MaxSizeExceeded if the content exceeds the max_bytesize
.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/sidetree/cas/ipfs.rb', line 52 def read(addr) fetch_url = "#{base_url}/cat?arg=#{addr}" begin res = Net::HTTP.post_form(URI(fetch_url), {}) if res.is_a?(Net::HTTPSuccess) FetchResult.new(FetchResult::CODE_SUCCESS, res.body) else FetchResult.new(FetchResult::CODE_NOT_FOUND) end rescue Errno::ECONNREFUSED FetchResult.new(FetchResult::CODE_CAS_NOT_REACHABLE) rescue StandardError FetchResult.new(FetchResult::CODE_NOT_FOUND) end end |
#up? ⇒ Boolean
Check IPFS endpoint are up and running.
77 78 79 80 81 82 83 84 |
# File 'lib/sidetree/cas/ipfs.rb', line 77 def up? begin id true rescue Errno::ECONNREFUSED false end end |
#write(content) ⇒ String
Writes the given content to CAS.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sidetree/cas/ipfs.rb', line 28 def write(content) multipart_boundary = SecureRandom.hex(32) uri = URI("#{base_url}/add") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == "https" req = Net::HTTP::Post.new(uri) req[ "Content-Type" ] = "multipart/form-data; boundary=#{multipart_boundary}" req["Content-Disposition"] = 'form-data; name=; filename=""' req.body = build_body(multipart_boundary, content) res = http.request(req) if res.is_a?(Net::HTTPSuccess) results = JSON.parse(res.body) results["Hash"] else raise Sidetree::Error, "Failed writing content. #{res.body}" end end |