simplewoo 0.0.1

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.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +29 -0
  6. data/Rakefile +1 -0
  7. data/lib/simplewoo/authentication.rb +35 -0
  8. data/lib/simplewoo/client/entity.rb +32 -0
  9. data/lib/simplewoo/client/match.rb +20 -0
  10. data/lib/simplewoo/client/personality.rb +31 -0
  11. data/lib/simplewoo/client/root.rb +10 -0
  12. data/lib/simplewoo/client/slider.rb +32 -0
  13. data/lib/simplewoo/client/tag.rb +32 -0
  14. data/lib/simplewoo/client/user.rb +87 -0
  15. data/lib/simplewoo/client.rb +33 -0
  16. data/lib/simplewoo/configuration.rb +34 -0
  17. data/lib/simplewoo/connection.rb +69 -0
  18. data/lib/simplewoo/error.rb +47 -0
  19. data/lib/simplewoo/request.rb +54 -0
  20. data/lib/simplewoo/version.rb +3 -0
  21. data/lib/simplewoo.rb +14 -0
  22. data/simplewoo.gemspec +37 -0
  23. data/spec/simplewoo/client/entity_spec.rb +34 -0
  24. data/spec/simplewoo/client/match_spec.rb +29 -0
  25. data/spec/simplewoo/client/personality_spec.rb +40 -0
  26. data/spec/simplewoo/client/root_spec.rb +53 -0
  27. data/spec/simplewoo/client/slider_spec.rb +37 -0
  28. data/spec/simplewoo/client/tag_spec.rb +35 -0
  29. data/spec/simplewoo/client/user_spec.rb +111 -0
  30. data/spec/simplewoo/error_spec.rb +61 -0
  31. data/spec/spec_helper.rb +36 -0
  32. data/spec/support/mocks/bad_app_secret.json +1 -0
  33. data/spec/support/mocks/empty.json +3 -0
  34. data/spec/support/mocks/entities.json +1554 -0
  35. data/spec/support/mocks/entity.json +77 -0
  36. data/spec/support/mocks/match_results.json +96 -0
  37. data/spec/support/mocks/me.json +20 -0
  38. data/spec/support/mocks/root.json +10 -0
  39. data/spec/support/mocks/slider.json +44 -0
  40. data/spec/support/mocks/sliders.json +23 -0
  41. data/spec/support/mocks/tag.json +20 -0
  42. data/spec/support/mocks/updated_user.json +19 -0
  43. data/spec/support/mocks/user.json +19 -0
  44. metadata +312 -0
@@ -0,0 +1,34 @@
1
+ # spec/simplewoo/client/entity_spec.rb
2
+ require "spec_helper"
3
+
4
+ describe Simplewoo::Client do
5
+ before do
6
+ Simplewoo.reset!
7
+ Simplewoo.configure do |client|
8
+ client.app_secret = "app_secret"
9
+ client.api_server_host = "api.woofound.com"
10
+ client.ssl = true
11
+ end
12
+ end
13
+
14
+ describe ".entities" do
15
+ let(:client) { Simplewoo::Client.new(api_token: "some_token") }
16
+ let(:response) { client.entities(1, 1) }
17
+
18
+ it "gets the entities for a slider" do
19
+ stub_woo(:get, "/sliders/1/entities/page/1", 200, ":some_token@", "entities")
20
+ expect(response["_embedded"]["entities"][0].name).to eq("Nursery Worker")
21
+ end
22
+ end
23
+
24
+ describe ".entity" do
25
+ let(:client) { Simplewoo::Client.new(api_token: "some_token") }
26
+ let(:response) { client.entity(2, 12914) }
27
+
28
+ it "gets an entity for a slider" do
29
+ stub_woo(:get, "/sliders/2/entities/12914", 200, ":some_token@", "entity")
30
+ expect(response.name).to eq("Bartender")
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,29 @@
1
+ # spec/simplewoo/client/match_spec.rb
2
+ require "spec_helper"
3
+
4
+ describe Simplewoo::Client do
5
+ before do
6
+ Simplewoo.reset!
7
+ Simplewoo.configure do |client|
8
+ client.app_secret = "app_secret"
9
+ client.api_server_host = "api.woofound.com"
10
+ client.ssl = true
11
+ end
12
+ end
13
+
14
+ before(:each) do
15
+ stub_woo(:get, "/sliders/1/results", 200, ":some_token@", "match_results")
16
+ end
17
+
18
+ describe ".match_for_slider" do
19
+ let(:client) { Simplewoo::Client.new(:api_token => "some_token") }
20
+
21
+ it "returns the entities matched to the authenticated user" do
22
+ expect(client.match_for_slider(1)).to respond_to(:entities)
23
+ end
24
+
25
+ it "returns the personality of the authenticated user" do
26
+ expect(client.match_for_slider(1)).to respond_to(:personalities)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ # spec/simplewoo/client/personality_spec.rb
2
+ require "spec_helper"
3
+
4
+ describe Simplewoo::Client do
5
+ before do
6
+ Simplewoo.reset!
7
+ Simplewoo.configure do |client|
8
+ client.app_secret = "app_secret"
9
+ client.api_server_host = "api.woofound.com"
10
+ client.ssl = true
11
+ end
12
+ end
13
+
14
+ before(:each) do
15
+ stub_woo(:get, "/sliders/1/results", 200, ":some_token@", "match_results")
16
+ end
17
+
18
+ describe ".personality_for_slider" do
19
+ let(:client) { Simplewoo::Client.new(:api_token => "some_token") }
20
+
21
+ it "personality for the slider for the current user" do
22
+ expect(client.personality_for_slider(1)).to be_a(Array)
23
+ end
24
+
25
+ it "returns the personality of the authenticated user" do
26
+ expect(client.personality_for_slider(1)).to_not be_empty
27
+ end
28
+ end
29
+
30
+ describe ".reset" do
31
+ let(:client) { Simplewoo::Client.new(:api_token => "some_token") }
32
+
33
+ it "resets the personality" do
34
+ stub_woo(:delete, "/sliders/1/reset", 200, ":some_token@", "match_results")
35
+ client.reset(1)
36
+
37
+ expect(WebMock).to have_requested(:delete, "https://:some_token@api.woofound.com/sliders/1/reset").once
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,53 @@
1
+ # spec/simplewoo/client/root.rb
2
+ require "spec_helper"
3
+
4
+ describe Simplewoo::Client do
5
+ before do
6
+ Simplewoo.reset!
7
+ Simplewoo.configure do |client|
8
+ client.app_secret = "app_secret"
9
+ client.api_server_host = "api.woofound.com"
10
+ client.ssl = true
11
+ end
12
+ end
13
+
14
+ let(:email) { "user@example.com" }
15
+ let(:password) { "password" }
16
+ let(:api_token) { "token" }
17
+
18
+ context "with basic authentication" do
19
+ let(:client) { Simplewoo::Client.new(:email => email, :password => password) }
20
+
21
+ describe ".root" do
22
+ it "returns the HAL links to the api" do
23
+ stub_woo(:get, "/", 200, "#{email}:#{password}@", "root")
24
+
25
+ expect(client.root).to respond_to(:_links)
26
+ end
27
+ end
28
+ end
29
+
30
+ context "with token authentication" do
31
+ let(:client) { Simplewoo::Client.new(:api_token => api_token) }
32
+
33
+ describe ".root" do
34
+ it "returns the HAL links to the api" do
35
+ stub_woo(:get, "/", 200, ":#{api_token}@", "root")
36
+
37
+ expect(client.root).to respond_to(:_links)
38
+ end
39
+ end
40
+ end
41
+
42
+ context "without authentication" do
43
+ let(:client) { Simplewoo::Client.new }
44
+
45
+ describe "#root" do
46
+ it "returns the HAL links to the api" do
47
+ stub_woo(:get, "/", "root")
48
+
49
+ expect(client.root).to respond_to(:_links)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ # spec/simplewoo/client/slider_spec.rb
2
+ require "spec_helper"
3
+
4
+ describe Simplewoo::Client do
5
+ before do
6
+ Simplewoo.reset!
7
+ Simplewoo.configure do |client|
8
+ client.app_secret = "app_secret"
9
+ client.api_server_host = "api.woofound.com"
10
+ client.ssl = true
11
+ end
12
+ end
13
+
14
+ describe ".sliders" do
15
+ let(:client) { Simplewoo::Client.new }
16
+
17
+ before(:each) do
18
+ stub_woo(:get, "/sliders", "sliders")
19
+ end
20
+
21
+ it "returns the sliders for the app" do
22
+ expect(client.sliders).to respond_to(:_embedded)
23
+ end
24
+ end
25
+
26
+ describe ".slider" do
27
+ let(:client) { Simplewoo::Client.new }
28
+
29
+ before(:each) do
30
+ stub_woo(:get, "/sliders/1", "slider")
31
+ end
32
+
33
+ it "returns the sliders for the app" do
34
+ expect(client.slider(1)).to respond_to(:_embedded)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,35 @@
1
+ # spec/simplewoo/client/tag_spec.rb
2
+ require "spec_helper"
3
+
4
+ describe Simplewoo::Client do
5
+ before do
6
+ Simplewoo.reset!
7
+ Simplewoo.configure do |client|
8
+ client.app_secret = "app_secret"
9
+ client.api_server_host = "api.woofound.com"
10
+ client.ssl = true
11
+ end
12
+ end
13
+
14
+ describe ".add_tag" do
15
+ let(:client) { Simplewoo::Client.new(api_token: "some_token") }
16
+ let(:response) { client.add_tag(1, 1, true) }
17
+
18
+ it "adds a tag for a user" do
19
+ stub_woo(:post, "/sliders/1/tags/1/add?me=true", 204, ":some_token@", "empty")
20
+ response
21
+ expect(client.last_response.status).to eq(204)
22
+ end
23
+ end
24
+
25
+ describe ".show_tag" do
26
+ let(:client) { Simplewoo::Client.new(api_token: "some_token") }
27
+ let(:response) { client.tag(1) }
28
+
29
+ it "adds a tag for a user" do
30
+ stub_woo(:get, "/tags/1", 200, ":some_token@", "tag")
31
+ expect(response.name).to eq("Going Camping")
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,111 @@
1
+ # spec/simplewoo/client/user.rb
2
+ require "spec_helper"
3
+
4
+ describe Simplewoo::Client do
5
+ before do
6
+ Simplewoo.reset!
7
+ Simplewoo.configure do |client|
8
+ client.app_secret = "app_secret"
9
+ client.api_server_host = "api.woofound.com"
10
+ client.ssl = true
11
+ end
12
+ end
13
+
14
+ describe ".create_user" do
15
+ let(:client) { Simplewoo::Client.new }
16
+
17
+ context "valid" do
18
+ let(:response) {
19
+ client.create_user("jason@example.com", "password", "password", "Jason", "Truluck", { :bio => "Some awesome bio.", :birthday => "1988-01-01"})
20
+ }
21
+
22
+ before(:each) do
23
+ stub_woo(:post, "/users?user%5Bbirthday%5D=Jason&user%5Bemail%5D=jason@example.com&user%5Bfirst_name%5D=Truluck&user%5Blast_name%5D%5Bbio%5D=Some%20awesome%20bio.&user%5Blast_name%5D%5Bbirthday%5D=1988-01-01&user%5Bpassword%5D=password&user%5Bpassword_confirmation%5D=password", "user")
24
+ stub_woo(:get, "/users/me", 200, "jason@example.com:password@", "me")
25
+
26
+ # Initializes the user for the client
27
+ response
28
+ end
29
+
30
+
31
+ it "returns the users email" do
32
+ expect(response).to respond_to(:email)
33
+ end
34
+
35
+ it "queries the Core API" do
36
+ expect(WebMock).to have_requested(:get, "https://jason@example.com:password@api.woofound.com/users/me").once
37
+ end
38
+
39
+ it "sets the api_token" do
40
+ expect(client.api_token).to eq("acce$$token")
41
+ end
42
+ end
43
+ end
44
+
45
+ describe ".me" do
46
+ context "authenticated" do
47
+ let(:client) { Simplewoo::Client.new(:api_token => "some_token") }
48
+
49
+ before(:each) do
50
+ stub_woo(:get, "/users/me", 200, ":some_token@", "me")
51
+ end
52
+
53
+ it "returns the authenticated user" do
54
+ expect(client.me).to respond_to(:email)
55
+ end
56
+ end
57
+
58
+ context "unauthenticated" do
59
+ let(:client) { Simplewoo::Client.new }
60
+
61
+ it "raises a Simplewoo::Unauthorized error" do
62
+ expect{ client.me }.to raise_error("No authorized user.")
63
+ end
64
+ end
65
+ end
66
+
67
+ describe ".update_user" do
68
+ let(:response) { client.update_user({:email => "tom@example.com"}) }
69
+
70
+ context "authenticated" do
71
+ let(:client) { Simplewoo::Client.new(:api_token => "some_token") }
72
+
73
+ it "updates user email field" do
74
+ stub_woo(:put, "/users/me?user%5Bemail%5D=tom@example.com", 200, ":some_token@", "updated_user")
75
+
76
+ expect(response.email).to eq("tom@example.com")
77
+ end
78
+ end
79
+
80
+ context "unauthenticated" do
81
+ let(:client) { Simplewoo::Client.new }
82
+
83
+ it "raises a Simplewoo::Unauthorized error" do
84
+ stub_woo(:put, "/users/me?user%5Bemail%5D=tom@example.com", "updated_user")
85
+
86
+ expect{ response }.to raise_error("No authorized user.")
87
+ end
88
+ end
89
+ end
90
+
91
+ describe ".reset_password" do
92
+ let(:response) { client.update_user({:password => "awesome_password", :password_confirmation => "awesome_password" }) }
93
+
94
+ context "authenticated" do
95
+ let(:client) { Simplewoo::Client.new(:api_token => "some_token") }
96
+
97
+ # PENDING Update the core to revalidate the access token on successful password change
98
+ it "updates user password"
99
+ end
100
+
101
+ context "unauthenticated" do
102
+ let(:client) { Simplewoo::Client.new }
103
+
104
+ it "raises a Simplewoo::Unauthorized error" do
105
+ stub_woo(:put, "/users/me?user%5Bpassword%5D=awesome_password%5Bpassword_confirmation%5D=awesome_password", "updated_user")
106
+
107
+ expect{ response }.to raise_error("No authorized user.")
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,61 @@
1
+ # spec/simplewoo/error.rb
2
+ require "spec_helper"
3
+
4
+ describe Simplewoo::Error do
5
+ before do
6
+ Simplewoo.reset!
7
+ Simplewoo.configure do |client|
8
+ client.app_secret = "app_secret"
9
+ client.api_server_host = "api.woofound.com"
10
+ client.ssl = true
11
+ end
12
+ end
13
+
14
+ let(:client) { Simplewoo::Client.new }
15
+
16
+ describe ".errors" do
17
+ it "returns a hash of methods" do
18
+ stub_woo(:get, "/", 401, "bad_app_secret")
19
+
20
+ begin
21
+ client.root
22
+ rescue Simplewoo::Unauthorized => e
23
+ expect(e.errors).to eq({"errors"=>["Missing app secret"]})
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "#from" do
29
+ describe "400 Error" do
30
+ it "raises a Simplewoo::BadRequest error" do
31
+ stub_woo(:get, "/", 400, "root")
32
+
33
+ expect{ client.root }.to raise_error(Simplewoo::BadRequest)
34
+ end
35
+ end
36
+
37
+ describe "401 Error" do
38
+ it "raises a Simplewoo::Unauthorized error" do
39
+ stub_woo(:get, "/", 401, "root")
40
+
41
+ expect{ client.root }.to raise_error(Simplewoo::Unauthorized)
42
+ end
43
+ end
44
+
45
+ describe "404 Error" do
46
+ it "raises a Simplewoo::NotFound error" do
47
+ stub_woo(:get, "/", 404, "root")
48
+
49
+ expect{ client.root }.to raise_error(Simplewoo::NotFound)
50
+ end
51
+ end
52
+
53
+ describe "422 Error" do
54
+ it "raises a Simplewoo::UnprocessibleEntity error" do
55
+ stub_woo(:get, "/", 422, "root")
56
+
57
+ expect{ client.root }.to raise_error(Simplewoo::UnprocessableEntity)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+
5
+ ENV["RAILS_ENV"] ||= 'test'
6
+
7
+ require 'simplecov'
8
+ SimpleCov.start 'rails'
9
+ class SimpleCov::Formatter::QualityFormatter
10
+ def format(result)
11
+ SimpleCov::Formatter::HTMLFormatter.new.format(result)
12
+ File.open("coverage/covered_percent", "w") do |f|
13
+ f.puts result.source_files.covered_percent.to_f
14
+ end
15
+ end
16
+ end
17
+ SimpleCov.formatter = SimpleCov::Formatter::QualityFormatter
18
+
19
+ require 'simplewoo'
20
+ require "webmock/rspec"
21
+
22
+ Dir[File.expand_path("spec/support/**/*.rb", __FILE__)].each {|f| require f}
23
+
24
+ RSpec.configure do |config|
25
+ config.treat_symbols_as_metadata_keys_with_true_values = true
26
+ config.color_enabled = true
27
+ config.order = "random"
28
+ end
29
+
30
+ def stub_woo(http_method = :any, endpoint = "/", status = 200, auth = nil, response)
31
+ url = "https://#{auth}api.woofound.com#{endpoint}"
32
+ stub_request(http_method, url).
33
+ to_return(:status => status,
34
+ :body => File.read(File.expand_path("../support/mocks/#{response}.json", __FILE__)),
35
+ :headers =>{'Accept' => "application/json", 'Content-type' => "application/json"})
36
+ end
@@ -0,0 +1 @@
1
+ {"errors":["Missing app secret"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "name" : "something"
3
+ }