Class: SafeNet::DNS

Inherits:
Object
  • Object
show all
Defined in:
lib/safenet.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_obj) ⇒ DNS

Returns a new instance of DNS.



528
529
530
# File 'lib/safenet.rb', line 528

def initialize(client_obj)
  @client = client_obj
end

Instance Method Details

#add_service(long_name, service_name, service_home_dir_path) ⇒ Object

Add a service to a registered long name. Only authorised requests can invoke the API.

Usage: my_client.dns.add_service(“my-domain”, “www”, “/sources”) Fail: “description”=>“FfiError::InvalidPath” Success: true

Reference: maidsafe.readme.io/docs/dns



606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/safenet.rb', line 606

def add_service(long_name, service_name, service_home_dir_path)
  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/dns"

  # Payload
  payload = {
    longName: long_name,
    serviceName: service_name,
    rootPath: 'app',
    serviceHomeDirPath: service_home_dir_path,
  }

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Put.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'application/json'
  })
  req.body = payload.to_json
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end

#create_long_name(long_name) ⇒ Object

Register a long name. Long names are public names that can be shared. Only authorised requests can invoke the API.

Usage: my_client.dns.create_long_name(“my-domain”) Fail: “description”=>“FfiError::InvalidPath” Success: true

Reference: maidsafe.readme.io/docs/dns-create-long-name



542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/safenet.rb', line 542

def create_long_name(long_name)
  # entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/dns/#{CGI.escape(long_name)}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'text/plain'
  })
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end

#get_file_unauth(long_name, service_name, file_path) ⇒ Object

maidsafe.readme.io/docs/dns-get-file-unauth get_file_unauth(“thegoogle”, “www”, “index.html”)



680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/safenet.rb', line 680

def get_file_unauth(long_name, service_name, file_path)
  # entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/dns/#{CGI.escape(service_name)}/#{CGI.escape(long_name)}/#{CGI.escape(file_path)}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path)
  res = http.request(req)
  res_headers = {}
  res.response.each_header {|k,v| res_headers[k] = v}
  res.code == "200" ? {"headers" => res_headers, "body" => res.body} : JSON.parse(res.body)
end

#get_home_dir(long_name, service_name) ⇒ Object



663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/safenet.rb', line 663

def get_home_dir(long_name, service_name)
  # entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/dns/#{CGI.escape(service_name)}/#{CGI.escape(long_name)}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path)
  res = http.request(req)
  res = JSON.parse(res.body)
  res["info"]["metadata"] = Base64.strict_decode64(res["info"]["metadata"]) if res.has_key?("info") && res["info"].has_key?("metadata")
  res
end

#list_long_namesObject



632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/safenet.rb', line 632

def list_long_names
  # entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/dns"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  })
  res = http.request(req)
  JSON.parse(res.body)
end

#list_services(long_name) ⇒ Object



647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/safenet.rb', line 647

def list_services(long_name)
  # entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/dns/#{CGI.escape(long_name)}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  })
  res = http.request(req)
  JSON.parse(res.body)
end

#register_service(long_name, service_name, service_home_dir_path, options = {}) ⇒ Object

Register a long name and a service. Only authorised requests can invoke the API.

Usage: my_client.dns.register_service(“my-domain”, “www”, “/sources”) Fail: “description”=>“FfiError::InvalidPath” Success: true

Reference: maidsafe.readme.io/docs/dns-register-service



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/safenet.rb', line 568

def register_service(long_name, service_name, service_home_dir_path, options = {})
  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/dns"

  # Payload
  payload = {
    longName: long_name,
    serviceName: service_name,
    rootPath: 'app',
    serviceHomeDirPath: service_home_dir_path,
  }

  # Optional
  payload["metadata"] = options[:meta] if options.has_key?(:meta)

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'application/json'
  })
  req.body = payload.to_json
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end