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://api.nametiles.co/v1/users/"
SCHEMA_VERSION =
"0.2"
USERAGENT =
"openname-ruby #{VERSION}"
OPENNAME_REGEX =
/^[a-z0-9_]{1,60}$/
VERSION =
"0.4.10"
@@endpoint =
nil
@@suffix =
""
@@profile_only =
false
@@username =
nil
@@password =
nil

Class Method Summary collapse

Class Method Details

.auth(username, password) ⇒ Object

Send basic authentication Openname.auth(nil,nil) disables basic auth Disabled by default



41
42
43
44
# File 'lib/openname.rb', line 41

def self.auth(username, password)
    @@username = username
    @@password = password
end

.endpointObject

Current endpoint used by the library



28
29
30
31
32
33
34
# File 'lib/openname.rb', line 28

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



80
81
82
# File 'lib/openname.rb', line 80

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

.get(openname) ⇒ Object

Return a User representing the given openname



135
136
137
# File 'lib/openname.rb', line 135

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)


142
143
144
145
146
147
148
# 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

.get_json(openname) ⇒ Object

Retrieve JSON data stored in Openname record

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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(profile_only) ⇒ Object

Profile only if profile_only is true, Openname profile will not be wrapped in { “profile”: {} }



51
52
53
# File 'lib/openname.rb', line 51

def self.profile_only(profile_only)
    @@profile_only = profile_only
end

.profile_only?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/openname.rb', line 55

def self.profile_only?
    @@profile_only
end

.suffixObject

Suffix appended to openname on each endpoint request



69
70
71
72
73
74
75
# File 'lib/openname.rb', line 69

def self.suffix
    if @@suffix.nil?
        return ""
    else
        return @@suffix
    end
end

.suffix=(suffix) ⇒ Object

Set suffix appended to openname to suffix if suffix is nil, no suffix will be appended



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

def self.suffix=(suffix)
    @@suffix = suffix
end

.valid?(openname) ⇒ Boolean

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

Returns:

  • (Boolean)


89
90
91
# File 'lib/openname.rb', line 89

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