heft 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/adapters/internet.rb +8 -1
- data/lib/heft.rb +4 -4
- data/lib/heft/data.rb +62 -0
- data/lib/heft/internal/hoist_connection.rb +35 -0
- data/lib/heft/users.rb +44 -0
- data/lib/heft/version.rb +1 -1
- data/spec/acceptance.tests/data/can_store_spec.rb +39 -0
- data/spec/acceptance.tests/users/the_basics_spec.rb +19 -61
- data/spec/helper.rb +9 -0
- metadata +8 -3
data/adapters/internet.rb
CHANGED
|
@@ -2,8 +2,15 @@ require "t"
|
|
|
2
2
|
|
|
3
3
|
module Adapters
|
|
4
4
|
class Internet
|
|
5
|
+
require 'audible'; include Audible;
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@internet = T::Internet.new
|
|
9
|
+
relay @internet, :requesting
|
|
10
|
+
end
|
|
11
|
+
|
|
5
12
|
def execute(request)
|
|
6
|
-
|
|
13
|
+
@internet.execute request
|
|
7
14
|
end
|
|
8
15
|
end
|
|
9
16
|
end
|
data/lib/heft.rb
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
dir = File.join(File.dirname(__FILE__))
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
end
|
|
3
|
+
Dir.glob(File.join(dir, "heft", "**", "*.rb")).each {|f| require f}
|
|
4
|
+
|
|
5
|
+
module Heft; end
|
data/lib/heft/data.rb
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
module Heft
|
|
2
|
+
class Key
|
|
3
|
+
attr_reader :id, :type
|
|
4
|
+
|
|
5
|
+
def initialize(opts={})
|
|
6
|
+
@id = opts[:id] || fail("You must supply :id")
|
|
7
|
+
@type = opts[:type] || fail("You must supply :type")
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class DataItem
|
|
12
|
+
attr_reader :key, :value
|
|
13
|
+
|
|
14
|
+
def initialize(opts={})
|
|
15
|
+
@key = opts[:key] || fail("You must supply :key")
|
|
16
|
+
@value = opts[:value] || fail("You must supply :value")
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class Data # http://docs.hoi.io/data/overview/api/data-api
|
|
21
|
+
class << self
|
|
22
|
+
def save(item)
|
|
23
|
+
reply = hoist.post(
|
|
24
|
+
:uri => earl(item.key),
|
|
25
|
+
:body => item.value
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
fail "Failed to save item. #{reply.body}" if reply.code > 299
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def get(key)
|
|
32
|
+
reply = hoist.get :uri => earl(key)
|
|
33
|
+
|
|
34
|
+
return nil if reply.code === 404
|
|
35
|
+
|
|
36
|
+
fail "Failed to fetch item with key <#{key}>. #{reply.body}" if reply.code > 299
|
|
37
|
+
|
|
38
|
+
JSON.parse reply.body
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def delete(key)
|
|
42
|
+
reply = hoist.delete :uri => earl(key)
|
|
43
|
+
|
|
44
|
+
fail "Failed delete item with key <#{key}>. #{reply.body}" if reply.code > 299
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def earl(key)
|
|
50
|
+
"https://data.hoi.io/#{URI.escape(key.type)}/#{URI.escape(key.id)}"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def hoist
|
|
54
|
+
Heft::Internal::HoistConnection.new :internet => internet, :api_key => ENV['APIKEY']
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def internet
|
|
58
|
+
Adapters::Internet.new
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Heft
|
|
2
|
+
module Internal
|
|
3
|
+
class HoistConnection
|
|
4
|
+
def initialize(opts={})
|
|
5
|
+
@internet = opts[:internet] || fail("You need to supply :internet")
|
|
6
|
+
@api_key = opts[:api_key] || fail("You need to supply :api_key")
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
[:get, :post, :delete].each do |m|
|
|
10
|
+
define_method m do |opts|
|
|
11
|
+
execute m, opts
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def execute(verb, opts={})
|
|
18
|
+
@internet.execute T::Request.new(
|
|
19
|
+
:headers => headers,
|
|
20
|
+
:uri => opts[:uri],
|
|
21
|
+
:verb => verb,
|
|
22
|
+
:body => opts[:body]
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def headers
|
|
27
|
+
{
|
|
28
|
+
'Authorization' => "HOIST #{@api_key}",
|
|
29
|
+
'Content-Type' => 'application/json',
|
|
30
|
+
'Accept' => 'application/json'
|
|
31
|
+
}
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
data/lib/heft/users.rb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
module Heft
|
|
2
|
+
class Users
|
|
3
|
+
require File.join '.', 'adapters', 'internet'
|
|
4
|
+
|
|
5
|
+
class << self
|
|
6
|
+
def add(opts={})
|
|
7
|
+
reply = add_core opts
|
|
8
|
+
|
|
9
|
+
fail "Failed => #{reply.body}" if reply.code > 299
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def delete(email)
|
|
13
|
+
reply = hoist.delete :uri => "https://auth.hoi.io/user/#{URI.escape(email)}"
|
|
14
|
+
|
|
15
|
+
fail "Failed to delete user. #{reply}" if reply.code > 299
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def contain?(email)
|
|
19
|
+
reply = add_core :email => email, :password => "******"
|
|
20
|
+
|
|
21
|
+
json = JSON.parse reply.body
|
|
22
|
+
|
|
23
|
+
error_message = json['message']
|
|
24
|
+
|
|
25
|
+
reply.code === 400 and error_message.match /already a user with that email address/
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def hoist
|
|
31
|
+
Heft::Internal::HoistConnection.new :internet => internet, :api_key => ENV['APIKEY']
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def internet; Adapters::Internet.new; end
|
|
35
|
+
|
|
36
|
+
def add_core(opts={})
|
|
37
|
+
hoist.post(
|
|
38
|
+
:uri => 'https://auth.hoi.io/user',
|
|
39
|
+
:body => opts
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/heft/version.rb
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
describe "Hoistapps and data" do
|
|
4
|
+
it "can store an item" do
|
|
5
|
+
key = Heft::Key.new(:id => "chicken pie", :type => "recipe")
|
|
6
|
+
|
|
7
|
+
item = Heft::DataItem.new(
|
|
8
|
+
:key => key,
|
|
9
|
+
:value => {"a" => "value a"}.to_json
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
Heft::Data.save item
|
|
13
|
+
|
|
14
|
+
result = Heft::Data.get key
|
|
15
|
+
|
|
16
|
+
expect(result).to_not be_nil
|
|
17
|
+
expect(result["a"]).to eql "value a"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Be sure to add permission on deletes: https://go.hoistapps.com/open-attempt/members/roles.
|
|
21
|
+
# Looks like we are connecting as anonymous user type.
|
|
22
|
+
it "can delete an item" do
|
|
23
|
+
key = Heft::Key.new(:id => "tacos", :type => "recipe")
|
|
24
|
+
|
|
25
|
+
item = Heft::DataItem.new(
|
|
26
|
+
:key => key,
|
|
27
|
+
:value => {}.to_json
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
Heft::Data.save item
|
|
31
|
+
|
|
32
|
+
Heft::Data.delete key
|
|
33
|
+
|
|
34
|
+
expect(Heft::Data.get key).to be_nil
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "data MUST be json otherwise you get error"
|
|
38
|
+
it "can update an item"
|
|
39
|
+
end
|
|
@@ -1,77 +1,35 @@
|
|
|
1
1
|
require 'helper'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class << self
|
|
8
|
-
def add(opts={})
|
|
9
|
-
headers = {'Authorization' => "HOIST #{ENV['APIKEY']}"}
|
|
10
|
-
|
|
11
|
-
reply = Adapters::Internet.new.execute T::Request.new(
|
|
12
|
-
:headers => headers,
|
|
13
|
-
:uri => 'https://auth.hoi.io/user',
|
|
14
|
-
:body => opts,
|
|
15
|
-
:verb => :post
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
fail "Failed => #{reply.body}" if reply.code > 299
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def contain?(email)
|
|
22
|
-
headers = {'Authorization' => "HOIST #{ENV['APIKEY']}"}
|
|
23
|
-
|
|
24
|
-
reply = Adapters::Internet.new.execute T::Request.new(
|
|
25
|
-
:headers => headers,
|
|
26
|
-
:uri => 'https://auth.hoi.io/user',
|
|
27
|
-
:body => {:email => email, :password => "******"},
|
|
28
|
-
:verb => :post
|
|
29
|
-
)
|
|
30
|
-
|
|
31
|
-
json = JSON.parse reply.body
|
|
32
|
-
|
|
33
|
-
error_message = json['message']
|
|
3
|
+
describe "Hoistapps and users" do
|
|
4
|
+
it "can be asked to add a new one" do # see: http://docs.hoi.io/authentication/overview/api/authentication-api
|
|
5
|
+
email = UniqueEmail.next
|
|
34
6
|
|
|
35
|
-
|
|
36
|
-
end
|
|
7
|
+
Heft::Users.add :email => email, :password => "xxx_not_a_password_xxx"
|
|
37
8
|
|
|
38
|
-
|
|
9
|
+
expect(Heft::Users.contain? email).to be_true, "User was not found"
|
|
10
|
+
end
|
|
39
11
|
|
|
40
|
-
|
|
41
|
-
|
|
12
|
+
it "fails when user exists already with the same email" do
|
|
13
|
+
email = UniqueEmail.next
|
|
42
14
|
|
|
43
|
-
|
|
44
|
-
:headers => headers,
|
|
45
|
-
:uri => 'https://auth.hoi.io/users',
|
|
46
|
-
)
|
|
15
|
+
any_password = "******"
|
|
47
16
|
|
|
48
|
-
|
|
17
|
+
Heft::Users.add :email => email, :password => any_password
|
|
49
18
|
|
|
50
|
-
|
|
51
|
-
end
|
|
52
|
-
end
|
|
19
|
+
expect{ Heft::Users.add :email => email, :password => any_password }.to raise_error /already a user with that email address/
|
|
53
20
|
end
|
|
54
|
-
|
|
21
|
+
|
|
22
|
+
it "can delete a user" do
|
|
23
|
+
email = UniqueEmail.next
|
|
55
24
|
|
|
56
|
-
|
|
57
|
-
class << self
|
|
58
|
-
def next
|
|
59
|
-
require 'securerandom'
|
|
60
|
-
"#{SecureRandom.uuid.to_s}@example.test.com"
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
|
25
|
+
any_password = "******"
|
|
64
26
|
|
|
65
|
-
|
|
66
|
-
it "can be asked to add a new one" do # see: http://docs.hoi.io/authentication/overview/api/authentication-api
|
|
67
|
-
email = UniqueEmail.next
|
|
27
|
+
Heft::Users.add :email => email, :password => any_password
|
|
68
28
|
|
|
69
|
-
Heft::Users.
|
|
29
|
+
Heft::Users.delete email
|
|
70
30
|
|
|
71
|
-
expect
|
|
31
|
+
expect{ Heft::Users.add :email => email, :password => any_password }.to_not raise_error
|
|
72
32
|
end
|
|
73
|
-
|
|
74
|
-
it "fails when user exists already with the same email"
|
|
75
|
-
it "can be asked to delete a user"
|
|
33
|
+
|
|
76
34
|
it "fails unless password is <= 6 characters long"
|
|
77
35
|
end
|
data/spec/helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: heft
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -58,7 +58,11 @@ files:
|
|
|
58
58
|
- adapters/internet.rb
|
|
59
59
|
- heft.gemspec
|
|
60
60
|
- lib/heft.rb
|
|
61
|
+
- lib/heft/data.rb
|
|
62
|
+
- lib/heft/internal/hoist_connection.rb
|
|
63
|
+
- lib/heft/users.rb
|
|
61
64
|
- lib/heft/version.rb
|
|
65
|
+
- spec/acceptance.tests/data/can_store_spec.rb
|
|
62
66
|
- spec/acceptance.tests/users/the_basics_spec.rb
|
|
63
67
|
- spec/helper.rb
|
|
64
68
|
homepage: ''
|
|
@@ -76,7 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
76
80
|
version: '0'
|
|
77
81
|
segments:
|
|
78
82
|
- 0
|
|
79
|
-
hash:
|
|
83
|
+
hash: 34913681
|
|
80
84
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
85
|
none: false
|
|
82
86
|
requirements:
|
|
@@ -85,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
85
89
|
version: '0'
|
|
86
90
|
segments:
|
|
87
91
|
- 0
|
|
88
|
-
hash:
|
|
92
|
+
hash: 34913681
|
|
89
93
|
requirements: []
|
|
90
94
|
rubyforge_project:
|
|
91
95
|
rubygems_version: 1.8.23
|
|
@@ -93,5 +97,6 @@ signing_key:
|
|
|
93
97
|
specification_version: 3
|
|
94
98
|
summary: A Hoistapps connector
|
|
95
99
|
test_files:
|
|
100
|
+
- spec/acceptance.tests/data/can_store_spec.rb
|
|
96
101
|
- spec/acceptance.tests/users/the_basics_spec.rb
|
|
97
102
|
- spec/helper.rb
|