Module: Openname

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

Overview

A toolkit for the Openname distributed identity & naming system

Defined Under Namespace

Classes: Org, User

Constant Summary collapse

DEFAULT_ENDPOINT =
"https://www.onename.io"
SCHEMA_VERSION =
"0.2"
USERAGENT =
"openname-ruby #{VERSION}"
OPENNAME_REGEX =
/^[a-z0-9_]{1,60}$/
VERSION =
"0.4"
@@endpoint =
nil

Class Method Summary collapse

Class Method Details

.check_schema_version(json_result) ⇒ Object



160
161
162
163
164
# File 'lib/openname.rb', line 160

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

.endpointObject

Current endpoint used by the library



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

def self.endpoint
  if @@endpoint.nil?
    return DEFAULT_ENDPOINT
  else
    return @@endpoint
  end
end

.endpoint=(url) ⇒ Object

Set endpoint to url if url is nil, DEFAULT_ENDPOINT is used as the endpoint



32
33
34
# File 'lib/openname.rb', line 32

def self.endpoint=(url)
  @@endpoint = url
end

.get(openname) ⇒ Object

Return a User representing the given openname



64
65
66
# File 'lib/openname.rb', line 64

def self.get(openname)
  User.from_json(self.get_json(openname),openname)
end

.get_bitcoin_address(openname_or_address) ⇒ Object

Takes either a bitcoin address or a openname Returns the bitcoin address associated with the openname or passes through address provided

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
# File 'lib/openname.rb', line 71

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

.get_json(openname) ⇒ Object

Retrieve JSON data stored in Openname record

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openname.rb', line 45

def self.get_json(openname) 
  raise ArgumentError.new("#{openname} is not a valid Openname") if !self.valid?(openname)
  uri = URI(self.endpoint + "/#{openname.downcase}.json")
  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})
  res = http.request(req)
  case res.code.to_s
    when "404" then raise NameError.new("User with Openname \"#{openname}\" does not exist")
    when "200" then return JSON.parse(res.body)
 else
      error = JSON.parse(res.body)
      raise RuntimeError.new("Openname endpoint returned error: #{error["error"]}")
  end  
  
end

.valid?(openname) ⇒ Boolean

Check if the given openname is in proper format Does not downcase input

Returns:

  • (Boolean)


39
40
41
# File 'lib/openname.rb', line 39

def self.valid?(openname)
  Openname::OPENNAME_REGEX.match(openname).nil? ? false : true
end