module Openname
A toolkit for the Openname
distributed identity & naming system
Constants
- DEFAULT_ENDPOINT
- OPENNAME_REGEX
- SCHEMA_VERSION
- USERAGENT
- VERSION
Public Class Methods
Send basic authentication Openname.auth
(nil,nil) disables basic auth Disabled by default
# File lib/openname.rb, line 41 def self.auth(username, password) @@username = username @@password = password end
Current endpoint used by the library
# File lib/openname.rb, line 28 def self.endpoint if @@endpoint.nil? return DEFAULT_ENDPOINT else return @@endpoint end end
Set endpoint to url
if url
is nil
, DEFAULT_ENDPOINT
is used as the endpoint
# File lib/openname.rb, line 80 def self.endpoint=(url) @@endpoint = url end
Return a User
representing the given openname
# File lib/openname.rb, line 135 def self.get(openname) User.from_json(self.get_json(openname),openname) end
Takes either a bitcoin address or a openname Returns the bitcoin address associated with the openname or passes through address provided
# File lib/openname.rb, line 142 def self.get_bitcoin_address(openname_or_address) return openname_or_address if Bitcoin.valid_address?(openname_or_address) raise ArgumentError.new("#{openname_or_address} is not a valid Openname or Bitcoin address") if !self.valid?(openname_or_address) user = get(openname_or_address) raise NameError.new("Openname user #{openname_or_address} does not have a Bitcoin address") if !Bitcoin.valid_address?(user.bitcoin_address) return user.bitcoin_address end
Retrieve JSON data stored in Openname
record
# File lib/openname.rb, line 95 def self.get_json(openname) raise ArgumentError.new("#{openname} is not a valid Openname") if !self.valid?(openname) uri = URI(self.endpoint + "/#{openname.downcase}#{self.suffix}") http = Net::HTTP.new(uri.host,uri.port) http.use_ssl = uri.scheme == "https" ? true : false req = Net::HTTP::Get.new(uri.path, {'User-Agent' => USERAGENT}) req.basic_auth(@@username, @@password) if @@username && @@password res = http.request(req) case res.code.to_s when "404" then raise NameError.new("Openname \"#{openname}\" does not exist") when "200" then json_body = JSON.parse(res.body) if(json_body["status"] && json_body["status"] == "reserved") NameError.new("Openname \"#{openname}\" does not exist. It is reserved.") else # Current ONS resolver always returns 200 # so we need to manually detect names that don't exist if (json_body["error"]) raise NameError.new("Openname \"#{openname}\" does not exist") end # Current ONS resolver wraps profile # so as to also return proof verification # results. openname-ruby ignores verifications if (profile_only? && json_body["profile"]) json_body["profile"] else json_body end end else error = JSON.parse(res.body) raise RuntimeError.new("Openname endpoint returned error: #{error["error"]}") end end
Profile only if profile_only
is true, Openname
profile will not be wrapped in { “profile”: {} }
# File lib/openname.rb, line 51 def self.profile_only(profile_only) @@profile_only = profile_only end
# File lib/openname.rb, line 55 def self.profile_only? @@profile_only end
Suffix appended to openname on each endpoint request
# File lib/openname.rb, line 69 def self.suffix if @@suffix.nil? return "" else return @@suffix end end
Set suffix appended to openname to suffix
if suffix
is nil
, no suffix will be appended
# File lib/openname.rb, line 63 def self.suffix=(suffix) @@suffix = suffix end
Check if the given openname
is in proper format Does not downcase input
# File lib/openname.rb, line 89 def self.valid?(openname) Openname::OPENNAME_REGEX.match(openname).nil? ? false : true end
Protected Class Methods
# File lib/openname.rb, line 246 def self.check_schema_version(json_result) if json_result["v"] != SCHEMA_VERSION warn "Openname gem only supports Openname schema version #{SCHEMA_VERSION}" end end