todaysplan 0.2.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f15a61fe0e7c44b9184e3eec252f2527f7f270d
4
- data.tar.gz: 702570cbc3b9e829c37a90d9cdc3e3e5f95ab5a1
3
+ metadata.gz: 282b2cf2f7d630c1ac5b3d5abc7a0af9a256c97f
4
+ data.tar.gz: f9e2c2653716d8407c4f3e3829168191170374e4
5
5
  SHA512:
6
- metadata.gz: 21b04b33a7f90c9b8a556f14494f12ff2ea0b51d93e58b061042aede1982fa8bea40d96788ade9f80c270f1f72ffeeaae299f598f48b1cf8c70a7f0ccf93e509
7
- data.tar.gz: bf26d14ee97f05b2991941b5fca413c12ea878b200bdf164a199eb9bb6add6bbab4cc9ee0278ffa60848d4d114429ea89f68a9c110de4b00485b52ca9f9c3ac4
6
+ metadata.gz: fb494b63767c70fa28c649abd389400bef271cfc8bbfac8ebace68c7d616d840bf680b73ece5ca07c704c4401a70b73de96f11a5bc42b48505b7ac209fbabcb4
7
+ data.tar.gz: 93fdbf177e80b1fae7cbf1b13ec07ad4c01f4bc1fce2b87b20ad07193bee45a49fb70f4e55d8dbaa45fae28ba0d1a8e6b224c7e32392d5eb02a2e361c7bcc7dc
data/lib/todays_plan.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'todays_plan/version'
2
2
  require 'todays_plan/client'
3
3
  require 'todays_plan/connector'
4
- require 'todays_plan/athlete'
4
+ require 'todays_plan/user'
5
5
  require 'todays_plan/activity'
6
6
  require 'todays_plan/errors'
7
7
  require 'rest_client'
@@ -1,7 +1,7 @@
1
1
  module TodaysPlan
2
2
  # Uses the TodaysPlan API for workouts and athlete information
3
3
 
4
- class Athlete
4
+ class User
5
5
 
6
6
  attr_reader :id
7
7
  attr_reader :name
@@ -28,51 +28,72 @@ module TodaysPlan
28
28
 
29
29
  # Find a single athlete
30
30
  # +id+:: the athlete id
31
+ # returns User
31
32
  def self.find(id)
32
33
  all().find{|a| a.id==id}
33
34
  end
34
35
 
35
36
  # Find all the coaches athletes
36
37
  # +client+:: the authenticated client
38
+ # returns [User]
37
39
  def self.all(client = nil)
38
40
  Connector.new('/users/delegates/users', client).get.map do |data|
39
41
  new(data)
40
42
  end
41
43
  end
42
44
 
43
- # Create new Athlete\
44
- # options
45
- # user_email - email address of new user
45
+
46
+ # Register a new user
47
+ # This is an unauthenticated request.
48
+ # options:
49
+ # email - email address of new user
46
50
  # firstname - first name of new user
47
51
  # lastname - last name of new user
48
52
  # password - password of new user
49
- # coach_email - coach email in todaysplan
50
- def self.create(options, client = nil)
51
-
53
+ # returns User
54
+ def self.register(options)
52
55
  #preregister
53
56
  response = RestClient.get("#{TodaysPlan.endpoint}/auth/preregister")
54
57
 
55
- # register the user
56
- payload = {email: options[:user_email], firstname: options[:firstname],
58
+ # register/create the user
59
+ payload = {email: options[:email], firstname: options[:firstname],
57
60
  lastname: options[:lastname], password: options[:password]}
58
61
  response = RestClient.post("#{TodaysPlan.endpoint}/auth/register",payload.to_json,
59
62
  { content_type: :json})
60
63
  response.body
61
- athlete = new(JSON.parse(response.body))
62
- # user invites coach
63
- user_client = TodaysPlan::Client.new(options[:user_email], options[:password])
64
- invite={"email"=>options[:coach_email],"state"=> "pending_coach",
65
- "relationship"=> "coach"}.to_json
66
- response = Connector.new('/users/delegates/invite', user_client).post(invite)
67
-
68
- # find invitation and accept
64
+ new(JSON.parse(response.body))
65
+ end
66
+
67
+ # Invite delegates
68
+ # This is an authenticated request of the new user to invite the delegates (coach)
69
+ # options
70
+ # relationship - relationship of delegate, i.e coach
71
+ # state - state of invite. i.e pending_coach
72
+ # email - delegates email in todaysplan
73
+ # returns Hash of the invite
74
+ def self.invite(options, client = nil)
75
+ # new user invites coach
76
+ invite={"email"=>options[:email],"state"=> options[:state],
77
+ "relationship"=> options[:relationship]}.to_json
78
+ Connector.new('/users/delegates/invite', client).post(invite)
79
+ end
80
+
81
+ # Accept pending invitations
82
+ # This is an authenticated request of the delegate (coach) to accept invites
83
+ # from the new user.
84
+ # queries for pending invitations and accept them.
85
+ # options
86
+ # id - id of the invite from #invite
87
+ # state - state of invite. i.e pending_coach
88
+ # returns [User]
89
+ def self.accept_invites(options, client = nil)
69
90
  Connector.new('/users/delegates/search/0/100', client).
70
- post({ "state"=> "pending_coach"}.to_json)["results"].each do |result|
71
- id = result["id"]
72
- Connector.new("/users/delegates/invite/accept/#{id}", client).get()
91
+ post({ "state"=> options[:state]}.to_json)["results"].map do |data|
92
+
93
+ # accept found invite
94
+ new(Connector.new("/users/delegates/invite/accept/#{data["id"]}").get()["client"])
73
95
 
74
96
  end
75
- athlete
76
97
  end
77
98
  end
78
99
  end
@@ -1,3 +1,3 @@
1
1
  module TodaysPlan
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -1,24 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe TodaysPlan::Athlete do
3
+ describe TodaysPlan::User do
4
4
 
5
- let(:response){File.read("spec/fixtures/athletes/search_response.json")}
5
+ let(:response){File.read("spec/fixtures/users/search_response.json")}
6
6
 
7
7
  it "expect to get all athletes" do
8
8
  stub_request(:get, "#{TodaysPlan.endpoint}/users/delegates/users").
9
9
  with(:headers => {'Accept'=>'application/json',
10
10
  'Authorization'=>'Bearer abc-123', }).
11
11
  to_return(:status => 200, :body => response, :headers => {})
12
- all = TodaysPlan::Athlete.all()
13
- expect(all).to be_a(Array)
14
- expect(all[0]).to be_a(TodaysPlan::Athlete)
15
- expect(all[0].first_name).to eq "Joe"
16
- expect(all[0].last_name).to eq "Athlete"
17
- expect(all[0].coach).to eq 7187033
18
- expect(all[0].timezone).to eq "US/Mountain"
19
- expect(all[0].id).to eq 7187033
20
- expect(all[0].name).to eq "Joe Athlete"
21
- expect(all[0].email).to eq "joeathlete@example.com"
12
+ users = TodaysPlan::User.all()
13
+ expect(users).to be_a(Array)
14
+ expect(users[0]).to be_a(TodaysPlan::User)
15
+ expect(users[0].first_name).to eq "Joe"
16
+ expect(users[0].last_name).to eq "Athlete"
17
+ expect(users[0].coach).to eq 7187033
18
+ expect(users[0].timezone).to eq "US/Mountain"
19
+ expect(users[0].id).to eq 7187033
20
+ expect(users[0].name).to eq "Joe Athlete"
21
+ expect(users[0].email).to eq "joeathlete@example.com"
22
22
  end
23
23
 
24
24
  it "expect to get one athlete" do
@@ -26,19 +26,20 @@ describe TodaysPlan::Athlete do
26
26
  with(:headers => {'Accept'=>'application/json',
27
27
  'Authorization'=>'Bearer abc-123', }).
28
28
  to_return(:status => 200, :body => response, :headers => {})
29
- athlete = TodaysPlan::Athlete.find(7187033)
30
- expect(athlete).to be_a(TodaysPlan::Athlete)
31
- expect(athlete).to respond_to(:first_name)
32
- expect(athlete).to respond_to(:last_name)
33
- expect(athlete).to respond_to(:coach)
34
- expect(athlete).to respond_to(:timezone)
35
- expect(athlete).to respond_to(:id)
36
- expect(athlete).to respond_to(:name)
37
- expect(athlete).to respond_to(:email)
38
- expect(athlete).to respond_to(:dob)
29
+ user = TodaysPlan::User.find(7187033)
30
+ expect(user).to be_a(TodaysPlan::User)
31
+ expect(user).to respond_to(:first_name)
32
+ expect(user).to respond_to(:last_name)
33
+ expect(user).to respond_to(:coach)
34
+ expect(user).to respond_to(:timezone)
35
+ expect(user).to respond_to(:id)
36
+ expect(user).to respond_to(:name)
37
+ expect(user).to respond_to(:email)
38
+ expect(user).to respond_to(:dob)
39
+ expect(user).to respond_to(:country)
39
40
  end
40
41
 
41
- it "expect to create one athlete" do
42
+ it "register user" do
42
43
  options = {user_email: 'joeathlete@example.com', firstname: 'joe',
43
44
  lastname: 'athlete', password:'password', coach_email: 'coach@email.com'}
44
45
  # preregister
@@ -50,23 +51,33 @@ describe TodaysPlan::Athlete do
50
51
  with(:body => '{"email":"joeathlete@example.com","firstname":"joe","lastname":"athlete","password":"password"}',
51
52
  :headers => {'Accept'=>'*/*'}).
52
53
  to_return(:status => 200,
53
- :body => File.read("spec/fixtures/athletes/register_response.json"),
54
+ :body => File.read("spec/fixtures/users/register_response.json"),
54
55
  :headers => {})
55
- #login as new user
56
- stub_request(:post, TodaysPlan.endpoint+'/auth/login').
57
- with(:body => "{\"username\":\"joeathlete@example.com\",\"password\":\"password\",\"token\":true}",
58
- :headers => {'Accept'=>'application/json',
59
- 'Content-Type'=>'application/json', }).
60
- to_return(:status => 200, :body => '{"token":"abc-456"}', :headers => {})
56
+
57
+ user = TodaysPlan::User.register(options)
58
+ expect(user.first_name).to eq "joe"
59
+ expect(user.last_name).to eq "athlete"
60
+ expect(user.email).to eq "joeathlete@example.com"
61
+ expect(user.id).to eq(1234)
62
+ end
63
+
64
+ it "invite delegate" do
65
+ options = { relationship: 'coach', state: 'pending_coach', email: 'coach@email.com'}
61
66
  #invite the coach
62
67
  stub_request(:post, "#{TodaysPlan.endpoint}/users/delegates/invite").
63
68
  with(:body => '{"email":"coach@email.com","state":"pending_coach","relationship":"coach"}',
64
69
  :headers => {'Accept'=>'application/json',
65
- 'Authorization'=>'Bearer abc-456',
70
+ 'Authorization'=>'Bearer abc-123',
66
71
  'Content-Type'=>'application/json'}).
67
72
  to_return(:status => 200,
68
- :body => File.read("spec/fixtures/athletes/invite_response.json"),
73
+ :body => File.read("spec/fixtures/users/invite_response.json"),
69
74
  :headers => {})
75
+ invite = TodaysPlan::User.invite(options)
76
+ expect(invite["id"]).to eq 8765
77
+ end
78
+
79
+ it "accept invite" do
80
+ options = { state: 'pending_coach'}
70
81
  # search for pending invites
71
82
  stub_request(:post, "#{TodaysPlan.endpoint}/users/delegates/search/0/100").
72
83
  with(:body => '{"state":"pending_coach"}',
@@ -74,19 +85,16 @@ describe TodaysPlan::Athlete do
74
85
  'Authorization'=>'Bearer abc-123',
75
86
  'Content-Type'=>'application/json'}).
76
87
  to_return(:status => 200,
77
- :body => File.read("spec/fixtures/athletes/delegates_response.json"),
88
+ :body => File.read("spec/fixtures/users/delegates_response.json"),
78
89
  :headers => {})
79
90
  # accept invite
80
91
  stub_request(:get, "#{TodaysPlan.endpoint}/users/delegates/invite/accept/98765").
81
92
  with(:headers => {'Accept'=>'application/json', 'Authorization'=>'Bearer abc-123'}).
82
93
  to_return(:status => 200,
83
- :body => File.read("spec/fixtures/athletes/accept_response.json"),
94
+ :body => File.read("spec/fixtures/users/accept_response.json"),
84
95
  :headers => {})
85
- athlete = TodaysPlan::Athlete.create(options)
86
- expect(athlete.first_name).to eq "joe"
87
- expect(athlete.last_name).to eq "athlete"
88
- expect(athlete.email).to eq "joeathlete@example.com"
89
- expect(athlete.id).to eq(1234)
96
+ users = TodaysPlan::User.accept_invites(options)
97
+ expect(users[0].id).to eq 7189071
90
98
  end
91
99
 
92
100
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: todaysplan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Curt Wilhelm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-16 00:00:00.000000000 Z
11
+ date: 2017-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -92,23 +92,23 @@ files:
92
92
  - Rakefile
93
93
  - lib/todays_plan.rb
94
94
  - lib/todays_plan/activity.rb
95
- - lib/todays_plan/athlete.rb
96
95
  - lib/todays_plan/client.rb
97
96
  - lib/todays_plan/connector.rb
98
97
  - lib/todays_plan/errors.rb
98
+ - lib/todays_plan/user.rb
99
99
  - lib/todays_plan/version.rb
100
100
  - spec/fixtures/activities/incomplete.json
101
101
  - spec/fixtures/activities/incomplete_response.json
102
- - spec/fixtures/athletes/accept_response.json
103
- - spec/fixtures/athletes/delegates_response.json
104
- - spec/fixtures/athletes/invite_response.json
105
- - spec/fixtures/athletes/register_response.json
106
- - spec/fixtures/athletes/search_response.json
102
+ - spec/fixtures/users/accept_response.json
103
+ - spec/fixtures/users/delegates_response.json
104
+ - spec/fixtures/users/invite_response.json
105
+ - spec/fixtures/users/register_response.json
106
+ - spec/fixtures/users/search_response.json
107
107
  - spec/spec_helper.rb
108
108
  - spec/todays_plan/activity_spec.rb
109
- - spec/todays_plan/athlete_spec.rb
110
109
  - spec/todays_plan/client_spec.rb
111
110
  - spec/todays_plan/connector_spec.rb
111
+ - spec/todays_plan/user_spec.rb
112
112
  - spec/todays_plan_spec.rb
113
113
  homepage: https://github.com/9ksoftware/todaysplan-ruby
114
114
  licenses:
@@ -137,14 +137,14 @@ summary: A Ruby Library for TodaysPlan
137
137
  test_files:
138
138
  - spec/fixtures/activities/incomplete.json
139
139
  - spec/fixtures/activities/incomplete_response.json
140
- - spec/fixtures/athletes/accept_response.json
141
- - spec/fixtures/athletes/delegates_response.json
142
- - spec/fixtures/athletes/invite_response.json
143
- - spec/fixtures/athletes/register_response.json
144
- - spec/fixtures/athletes/search_response.json
140
+ - spec/fixtures/users/accept_response.json
141
+ - spec/fixtures/users/delegates_response.json
142
+ - spec/fixtures/users/invite_response.json
143
+ - spec/fixtures/users/register_response.json
144
+ - spec/fixtures/users/search_response.json
145
145
  - spec/spec_helper.rb
146
146
  - spec/todays_plan/activity_spec.rb
147
- - spec/todays_plan/athlete_spec.rb
148
147
  - spec/todays_plan/client_spec.rb
149
148
  - spec/todays_plan/connector_spec.rb
149
+ - spec/todays_plan/user_spec.rb
150
150
  - spec/todays_plan_spec.rb