openname 0.4.9 → 0.4.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 230cf6eb56f09632379dd5c096ca5c84b9d651d0
4
- data.tar.gz: c7a8d3bd21dbcf425d9d484f2b1ee35f51ef3835
3
+ metadata.gz: 4433104db84be7239b1fa5927708e50f6096954e
4
+ data.tar.gz: 7969f727f1dfd5919187a06c3e97f445b0bddb8a
5
5
  SHA512:
6
- metadata.gz: 6a56ecf26935db8e94ed934cf6ba97e4a28f83cf67f5137110420b09eb7697375e8c8e761d4af137efc6948901bdda4e91f00fd7fcf3a4d289630e44bf7efd61
7
- data.tar.gz: afc53c53d14cb218c712bbf37ecada2ed94cadb7684db085506f2d1ebeaef92457175413e136762526667c8a47f4af9d45a8bbbe3f2e87dd9101b313c12b9818
6
+ metadata.gz: 674f7b7ca5b221c66d954b22fd0ab3e22d72d84f8ab6119ba24b24bba63b146d7c13b74b137cfc257043e35714522f5f721fef09c122b2c424deaf666ffd2aa4
7
+ data.tar.gz: cde1a9cb0310e80137cf1baa5734589390fb52191911bce282ef3f86ecce89f1a64630f419db4ed98703a2a5f222686c843bd88676a401fcb835397f0d76e6e6
data/lib/openname.rb CHANGED
@@ -7,217 +7,246 @@ require 'bitcoin'
7
7
  ##
8
8
  # A toolkit for the Openname distributed identity & naming system
9
9
  module Openname
10
- DEFAULT_ENDPOINT = "https://openname.nametiles.co/v2/"
11
- SCHEMA_VERSION = "0.2"
12
- USERAGENT = "openname-ruby #{VERSION}"
10
+ DEFAULT_ENDPOINT = "https://api.nametiles.co/v1/users/"
11
+ SCHEMA_VERSION = "0.2"
12
+ USERAGENT = "openname-ruby #{VERSION}"
13
13
 
14
- # https://github.com/openname/openname-specifications#usernames
15
- OPENNAME_REGEX = /^[a-z0-9_]{1,60}$/
14
+ # https://github.com/openname/openname-specifications#usernames
15
+ OPENNAME_REGEX = /^[a-z0-9_]{1,60}$/
16
16
 
17
- @@endpoint = nil
17
+ @@endpoint = nil
18
18
 
19
- @@suffix = ".json"
19
+ @@suffix = ""
20
20
 
21
- @@username = nil
22
- @@password = nil
21
+ @@profile_only = false
23
22
 
24
- ##
25
- # Current endpoint used by the library
26
- def self.endpoint
27
- if @@endpoint.nil?
28
- return DEFAULT_ENDPOINT
29
- else
30
- return @@endpoint
23
+ @@username = nil
24
+ @@password = nil
25
+
26
+ ##
27
+ # Current endpoint used by the library
28
+ def self.endpoint
29
+ if @@endpoint.nil?
30
+ return DEFAULT_ENDPOINT
31
+ else
32
+ return @@endpoint
33
+ end
31
34
  end
32
- end
33
-
34
- ##
35
- # Send basic authentication
36
- #
37
- def self.auth(username, password)
38
- @@username = username
39
- @@password = password
40
- end
41
-
42
- ##
43
- # Set suffix appended to openname to +suffix+
44
- # if +suffix+ is +nil+, no suffix will be appended
45
- def self.suffix=(suffix)
46
- @@suffix = suffix
47
- end
48
-
49
- ##
50
- # Suffix appended to openname on each endpoint request
51
- def self.suffix
52
- if @@suffix.nil?
53
- return ""
54
- else
55
- return @@suffix
35
+
36
+ ##
37
+ # Send basic authentication
38
+ # Openname.auth(nil,nil) disables basic auth
39
+ # Disabled by default
40
+ #
41
+ def self.auth(username, password)
42
+ @@username = username
43
+ @@password = password
56
44
  end
57
- end
58
-
59
- ##
60
- # Set endpoint to +url+
61
- # if +url+ is +nil+, +DEFAULT_ENDPOINT+ is used as the endpoint
62
- def self.endpoint=(url)
63
- @@endpoint = url
64
- end
65
-
66
-
67
-
68
- ##
69
- # Check if the given +openname+ is in proper format
70
- # Does not downcase input
71
- def self.valid?(openname)
72
- Openname::OPENNAME_REGEX.match(openname).nil? ? false : true
73
- end
74
-
75
- ##
76
- # Retrieve JSON data stored in Openname record
77
- def self.get_json(openname)
78
- raise ArgumentError.new("#{openname} is not a valid Openname") if !self.valid?(openname)
79
- uri = URI(self.endpoint + "/#{openname.downcase}#{self.suffix}")
80
- http = Net::HTTP.new(uri.host,uri.port)
81
- http.use_ssl = uri.scheme == "https" ? true : false
82
- req = Net::HTTP::Get.new(uri.path, {'User-Agent' => USERAGENT})
83
- req.basic_auth(@@username, @@password) if @@username && @@password
84
- res = http.request(req)
85
- case res.code.to_s
86
- when "404" then raise NameError.new("Openname \"#{openname}\" does not exist")
87
- when "200" then
88
- json_body = JSON.parse(res.body)
89
- if(json_body["status"] && json_body["status"] == "reserved")
90
- NameError.new("Openname \"#{openname}\" does not exist. It is reserved.")
91
- else
92
-
93
- # Current ONS resolver always returns 200
94
- # so we need to manually detect names that don't exist
95
- if (json_body["error"])
96
- raise NameError.new("Openname \"#{openname}\" does not exist")
97
- end
98
45
 
99
- # Current ONS resolver wraps profile
100
- # so as to also return proof verification
101
- # results. openname-ruby ignores verifications
102
- if (json_body["profile"])
103
- json_body["profile"]
104
- else
105
- json_body
106
- end
107
- end
108
- else
109
- error = JSON.parse(res.body)
110
- raise RuntimeError.new("Openname endpoint returned error: #{error["error"]}")
46
+ ##
47
+ # Profile only
48
+ # if +profile_only+ is true, Openname profile will not be wrapped in
49
+ # { "profile": {} }
50
+ #
51
+ def self.profile_only(profile_only)
52
+ @@profile_only = profile_only
111
53
  end
112
54
 
113
- end
114
-
115
- ##
116
- # Return a +User+ representing the given openname
117
- def self.get(openname)
118
- User.from_json(self.get_json(openname),openname)
119
- end
120
-
121
- ##
122
- # Takes either a bitcoin address or a openname
123
- # Returns the bitcoin address associated with the openname or passes through address provided
124
- def self.get_bitcoin_address(openname_or_address)
125
- return openname_or_address if Bitcoin.valid_address?(openname_or_address)
126
- raise ArgumentError.new("#{openname_or_address} is not a valid Openname or Bitcoin address") if !self.valid?(openname_or_address)
127
- user = get(openname_or_address)
128
- raise NameError.new("Openname user #{openname_or_address} does not have a Bitcoin address") if !Bitcoin.valid_address?(user.bitcoin_address)
129
- return user.bitcoin_address
130
- end
131
-
132
- class User
133
- def self.from_json(json,openname)
134
- User.new(json,openname)
55
+ def self.profile_only?
56
+ @@profile_only
135
57
  end
136
58
 
137
- attr_reader :openname
138
- attr_reader :name_formatted
139
- attr_reader :avatar_url
140
- attr_reader :cover_url
141
- attr_reader :location_formatted
142
- attr_reader :website
143
- attr_reader :bio
144
- attr_reader :angellist_username
145
- attr_reader :github_username
146
- attr_reader :facebook_username
147
- attr_reader :twitter_username
148
- attr_reader :instagram_username
149
- attr_reader :linkedin_url
150
- attr_reader :bitcoin_address
151
- attr_reader :bitmessage_address
152
- attr_reader :bitcoinotc_username
153
- attr_reader :pgp_fingerprint
154
- attr_reader :pgp_url
155
- attr_reader :orgs
156
- attr_reader :schema_version
157
-
158
-
159
- def initialize(json,openname)
160
- @openname = openname
161
- @name_formatted = json["name"]["formatted"] if json["name"]
162
- @avatar_url = json["avatar"]["url"] if json["avatar"]
163
- @cover_url = json["cover"]["url"] if json["cover"]
164
- @location_formatted = json["location"]["formatted"] if json["location"]
165
- @website = json["website"]
166
- @bio = json["bio"]
167
- @angellist_username = json["angellist"]["username"] if json["angellist"]
168
- @github_username = json["github"]["username"] if json["github"]
169
- @facebook_username = json["facebook"]["username"] if json["facebook"]
170
- @twitter_username = json["twitter"]["username"] if json["twitter"]
171
- @instagram_username = json["instagram"]["username"] if json["instagram"]
172
- @linkedin_url = json["linkedin"]["url"] if json["linkedin"]
173
- @bitcoin_address = json["bitcoin"]["address"] if json["bitcoin"]
174
- @bitmessage_address = json["bitmessage"]["address"] if json["bitmessage"]
175
- @bitcoinotc_username = json["bitcoinotc"]["username"] if json["bitcoinotc"]
176
- @pgp_fingerprint = json["pgp"]["fingerprint"] if json["pgp"]
177
- @pgp_url = json["pgp"]["url"] if json["pgp"]
178
- @schema_version = json["v"]
179
- @orgs = parse_orgs(json["orgs"])
59
+
60
+ ##
61
+ # Set suffix appended to openname to +suffix+
62
+ # if +suffix+ is +nil+, no suffix will be appended
63
+ def self.suffix=(suffix)
64
+ @@suffix = suffix
180
65
  end
181
66
 
182
- protected
67
+ ##
68
+ # Suffix appended to openname on each endpoint request
69
+ def self.suffix
70
+ if @@suffix.nil?
71
+ return ""
72
+ else
73
+ return @@suffix
74
+ end
75
+ end
183
76
 
184
- def parse_orgs(orgs_json)
185
- orgs = Array.new
186
- if orgs_json
187
- for org_json in orgs_json
188
- orgs << Org.new(org_json)
77
+ ##
78
+ # Set endpoint to +url+
79
+ # if +url+ is +nil+, +DEFAULT_ENDPOINT+ is used as the endpoint
80
+ def self.endpoint=(url)
81
+ @@endpoint = url
82
+ end
83
+
84
+
85
+
86
+ ##
87
+ # Check if the given +openname+ is in proper format
88
+ # Does not downcase input
89
+ def self.valid?(openname)
90
+ Openname::OPENNAME_REGEX.match(openname).nil? ? false : true
91
+ end
92
+
93
+ ##
94
+ # Retrieve JSON data stored in Openname record
95
+ def self.get_json(openname)
96
+ raise ArgumentError.new("#{openname} is not a valid Openname") if !self.valid?(openname)
97
+ uri = URI(self.endpoint + "/#{openname.downcase}#{self.suffix}")
98
+ http = Net::HTTP.new(uri.host,uri.port)
99
+ http.use_ssl = uri.scheme == "https" ? true : false
100
+ req = Net::HTTP::Get.new(uri.path, {'User-Agent' => USERAGENT})
101
+ req.basic_auth(@@username, @@password) if @@username && @@password
102
+ res = http.request(req)
103
+ case res.code.to_s
104
+ when "404" then raise NameError.new("Openname \"#{openname}\" does not exist")
105
+ when "200" then
106
+ json_body = JSON.parse(res.body)
107
+ if(json_body["status"] && json_body["status"] == "reserved")
108
+ NameError.new("Openname \"#{openname}\" does not exist. It is reserved.")
109
+ else
110
+
111
+ # Current ONS resolver always returns 200
112
+ # so we need to manually detect names that don't exist
113
+ if (json_body["error"])
114
+ raise NameError.new("Openname \"#{openname}\" does not exist")
115
+ end
116
+
117
+ # Current ONS resolver wraps profile
118
+ # so as to also return proof verification
119
+ # results. openname-ruby ignores verifications
120
+ if (profile_only? && json_body["profile"])
121
+ json_body["profile"]
122
+ else
123
+ json_body
124
+ end
125
+ end
126
+ else
127
+ error = JSON.parse(res.body)
128
+ raise RuntimeError.new("Openname endpoint returned error: #{error["error"]}")
189
129
  end
190
- end
191
- orgs
130
+
192
131
  end
193
132
 
194
- end
133
+ ##
134
+ # Return a +User+ representing the given openname
135
+ def self.get(openname)
136
+ User.from_json(self.get_json(openname),openname)
137
+ end
195
138
 
196
- class Org
197
- def self.from_json(json)
198
- Org.new(json)
139
+ ##
140
+ # Takes either a bitcoin address or a openname
141
+ # Returns the bitcoin address associated with the openname or passes through address provided
142
+ def self.get_bitcoin_address(openname_or_address)
143
+ return openname_or_address if Bitcoin.valid_address?(openname_or_address)
144
+ raise ArgumentError.new("#{openname_or_address} is not a valid Openname or Bitcoin address") if !self.valid?(openname_or_address)
145
+ user = get(openname_or_address)
146
+ raise NameError.new("Openname user #{openname_or_address} does not have a Bitcoin address") if !Bitcoin.valid_address?(user.bitcoin_address)
147
+ return user.bitcoin_address
199
148
  end
200
149
 
201
- attr_reader :url
202
- attr_reader :relationship
203
- attr_reader :name
150
+ class User
151
+ def self.from_json(json,openname)
152
+ User.new(json,openname)
153
+ end
154
+
155
+ attr_reader :username
156
+ attr_reader :name_formatted
157
+ attr_reader :avatar_url
158
+ attr_reader :cover_url
159
+ attr_reader :location_formatted
160
+ attr_reader :website
161
+ attr_reader :bio
162
+ attr_reader :angellist_username
163
+ attr_reader :github_username
164
+ attr_reader :facebook_username
165
+ attr_reader :twitter_username
166
+ attr_reader :instagram_username
167
+ attr_reader :linkedin_url
168
+ attr_reader :bitcoin_address
169
+ attr_reader :bitmessage_address
170
+ attr_reader :bitcoinotc_username
171
+ attr_reader :pgp_fingerprint
172
+ attr_reader :pgp_url
173
+ attr_reader :orgs
174
+ attr_reader :schema_version
175
+
176
+
177
+ def initialize(json,username)
178
+ if(json["profile"])
179
+ json = json["profile"]
180
+ end
204
181
 
182
+ @username = username
183
+ @name_formatted = json["name"]["formatted"] if json["name"]
184
+ @avatar_url = json["avatar"]["url"] if json["avatar"]
185
+ @cover_url = json["cover"]["url"] if json["cover"]
186
+ @location_formatted = json["location"]["formatted"] if json["location"]
187
+ @website = json["website"]
188
+ @bio = json["bio"]
189
+ @angellist_username = json["angellist"]["username"] if json["angellist"]
190
+ @github_username = json["github"]["username"] if json["github"]
191
+ @facebook_username = json["facebook"]["username"] if json["facebook"]
192
+ @twitter_username = json["twitter"]["username"] if json["twitter"]
193
+ @instagram_username = json["instagram"]["username"] if json["instagram"]
194
+ @linkedin_url = json["linkedin"]["url"] if json["linkedin"]
195
+ @bitcoin_address = json["bitcoin"]["address"] if json["bitcoin"]
196
+ @bitmessage_address = json["bitmessage"]["address"] if json["bitmessage"]
197
+ @bitcoinotc_username = json["bitcoinotc"]["username"] if json["bitcoinotc"]
198
+ @pgp_fingerprint = json["pgp"]["fingerprint"] if json["pgp"]
199
+ @pgp_url = json["pgp"]["url"] if json["pgp"]
200
+ @schema_version = json["v"]
201
+ @orgs = parse_orgs(json["orgs"])
202
+
203
+
204
+ end
205
+
206
+ def openname
207
+ warn "[DEPRECATION] `openname` is deprecated. Please use `username` instead."
208
+ username
209
+ end
210
+
211
+ protected
212
+
213
+ def parse_orgs(orgs_json)
214
+ orgs = Array.new
215
+ if orgs_json
216
+ for org_json in orgs_json
217
+ orgs << Org.new(org_json)
218
+ end
219
+ end
220
+ orgs
221
+ end
205
222
 
206
- def initialize(json)
207
- @url = json["url"] if json["url"]
208
- @relationship = json["relationship"] if json["relationship"]
209
- @name = json["name"] if json["name"]
210
223
  end
211
224
 
225
+ class Org
226
+ def self.from_json(json)
227
+ Org.new(json)
228
+ end
229
+
230
+ attr_reader :url
231
+ attr_reader :relationship
232
+ attr_reader :name
233
+
234
+
235
+ def initialize(json)
236
+ @url = json["url"] if json["url"]
237
+ @relationship = json["relationship"] if json["relationship"]
238
+ @name = json["name"] if json["name"]
239
+ end
212
240
 
213
- end
214
241
 
215
- protected
242
+ end
216
243
 
217
- def self.check_schema_version(json_result)
218
- if json_result["v"] != SCHEMA_VERSION
219
- warn "Openname gem only supports Openname schema version #{SCHEMA_VERSION}"
244
+ protected
245
+
246
+ def self.check_schema_version(json_result)
247
+ if json_result["v"] != SCHEMA_VERSION
248
+ warn "Openname gem only supports Openname schema version #{SCHEMA_VERSION}"
249
+ end
220
250
  end
221
- end
222
251
 
223
252
  end
@@ -1,3 +1,3 @@
1
1
  module Openname
2
- VERSION = "0.4.9"
2
+ VERSION = "0.4.10"
3
3
  end
@@ -12,7 +12,7 @@ describe Openname, :vcr => { :cassette_name => "openname", :record => :new_episo
12
12
  Openname.endpoint = "https://www.example.com"
13
13
  Openname.endpoint.should == "https://www.example.com"
14
14
  Openname.endpoint = nil
15
- Openname.endpoint.should == "https://openname.nametiles.co/v2/"
15
+ Openname.endpoint.should == "https://api.nametiles.co/v1/users"
16
16
  end
17
17
 
18
18
  it "should retrieve openname user" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openname
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.9
4
+ version: 0.4.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Larry Salibra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-02 00:00:00.000000000 Z
11
+ date: 2015-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler