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.



553
554
555
# File 'lib/safenet.rb', line 553

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



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/safenet.rb', line 631

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



567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/safenet.rb', line 567

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”)



705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/safenet.rb', line 705

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



688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/safenet.rb', line 688

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



657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/safenet.rb', line 657

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



672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/safenet.rb', line 672

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



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/safenet.rb', line 593

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