coach4rb 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 (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +31 -0
  6. data/Rakefile +8 -0
  7. data/coach4rb.gemspec +29 -0
  8. data/lib/coach4rb.rb +64 -0
  9. data/lib/coach4rb/builder.rb +142 -0
  10. data/lib/coach4rb/client.rb +116 -0
  11. data/lib/coach4rb/coach.rb +836 -0
  12. data/lib/coach4rb/json_response_parser.rb +11 -0
  13. data/lib/coach4rb/mixin/as_hash.rb +34 -0
  14. data/lib/coach4rb/mixin/auto_constructor.rb +71 -0
  15. data/lib/coach4rb/mixin/basic_auth.rb +23 -0
  16. data/lib/coach4rb/mixin/iterable.rb +40 -0
  17. data/lib/coach4rb/mixin/track_reader.rb +24 -0
  18. data/lib/coach4rb/mixin/track_writer.rb +19 -0
  19. data/lib/coach4rb/privacy.rb +11 -0
  20. data/lib/coach4rb/proxy.rb +143 -0
  21. data/lib/coach4rb/resource/entity.rb +47 -0
  22. data/lib/coach4rb/resource/entry.rb +126 -0
  23. data/lib/coach4rb/resource/page.rb +74 -0
  24. data/lib/coach4rb/resource/partnership.rb +37 -0
  25. data/lib/coach4rb/resource/sport.rb +19 -0
  26. data/lib/coach4rb/resource/subscription.rb +47 -0
  27. data/lib/coach4rb/resource/user.rb +49 -0
  28. data/lib/coach4rb/response_parser.rb +11 -0
  29. data/lib/coach4rb/version.rb +3 -0
  30. data/test/test_access_proxy.rb +58 -0
  31. data/test/test_client.rb +41 -0
  32. data/test/test_coach_client.rb +21 -0
  33. data/test/test_coach_entry.rb +141 -0
  34. data/test/test_coach_partnership.rb +115 -0
  35. data/test/test_coach_subscription.rb +110 -0
  36. data/test/test_coach_user.rb +191 -0
  37. data/test/test_entity.rb +63 -0
  38. data/test/test_entry_resource.rb +36 -0
  39. data/test/test_helper.rb +3 -0
  40. data/test/test_page.rb +82 -0
  41. data/test/test_partnership_resource.rb +69 -0
  42. data/test/test_sport_resource.rb +33 -0
  43. data/test/test_subscription_resource.rb +74 -0
  44. data/test/test_user_resource.rb +104 -0
  45. data/tools/.keep +0 -0
  46. metadata +190 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 412f9ffde0208d1f386a7a179ca397d044e90179
4
+ data.tar.gz: e862eea49492255fa1cd90240ffc8ca1493d0851
5
+ SHA512:
6
+ metadata.gz: eea2b892f25a2c60673bf387be62016c98ba6cec03e9fdc3c24a0148e989963a8def1f46c26b7ef064fd46f2ecaa5356b2496d4e69f11c0e9cea22a72e09f4ba
7
+ data.tar.gz: e789f89d7244757f89c7d7245d7645edb6671a80442b70aad9584b791e5e07b2295bb5e7728963f66eb7eba538838b4c24ecf35243e48fc15c3cf369c14550ce
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ .idea/
2
+ projectFilesBackup/*
3
+ /.bundle/
4
+ /.yardoc
5
+ /Gemfile.lock
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
11
+ /tmp/
12
+ *.bundle
13
+ *.so
14
+ *.o
15
+ *.a
16
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in coach4rb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexander Rueedlinger
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ Coach4rb
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'coach4rb'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install coach4rb
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/coach4rb/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |task|
5
+ task.libs = ['.', 'lib', 'test']
6
+ task.test_files = FileList['test/test_*.rb']
7
+ task.verbose = true
8
+ end
data/coach4rb.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'coach4rb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "coach4rb"
8
+ spec.version = Coach4rb::VERSION
9
+ spec.authors = ["Alexander Rueedlinger","Stefan Wanzenried","Roman Kuepper","Sveta Krasikova"]
10
+ spec.email = ["a.rueedlinger@gmail.com"]
11
+ spec.summary = %q{Coach4rb is client for the cyber coach webservice.}
12
+ spec.description = %q{Coach4rb is client for the cyber coach webservice @unifr.ch. }
13
+ spec.homepage = "https://github.com/lexruee/coach4rb"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", '~>0'
24
+
25
+ spec.add_runtime_dependency "rest-client", '~>1.7'
26
+ spec.add_runtime_dependency "addressable", '~>2.3'
27
+ spec.add_runtime_dependency "gyoku", '~>1.2'
28
+
29
+ end
data/lib/coach4rb.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+ require 'addressable/uri'
4
+ require 'gyoku'
5
+ require 'base64'
6
+ require 'ostruct'
7
+
8
+ require 'coach4rb/mixin/auto_constructor'
9
+ require 'coach4rb/mixin/as_hash'
10
+ require 'coach4rb/mixin/iterable'
11
+ require 'coach4rb/mixin/basic_auth'
12
+ require 'coach4rb/mixin/track_reader'
13
+ require 'coach4rb/mixin/track_writer'
14
+
15
+ require 'coach4rb/privacy'
16
+ require 'coach4rb/resource/entity'
17
+ require 'coach4rb/resource/page'
18
+ require 'coach4rb/resource/user'
19
+ require 'coach4rb/resource/sport'
20
+ require 'coach4rb/resource/entry'
21
+ require 'coach4rb/resource/partnership'
22
+ require 'coach4rb/resource/subscription'
23
+
24
+
25
+ require 'coach4rb/builder'
26
+ require 'coach4rb/proxy'
27
+ require 'coach4rb/response_parser'
28
+ require 'coach4rb/json_response_parser'
29
+ require 'coach4rb/coach'
30
+ require 'coach4rb/client'
31
+ require 'coach4rb/version'
32
+
33
+
34
+ module Coach4rb
35
+
36
+ # Creates a Coach4rb client given the configuration hash config.
37
+ #
38
+ # @param [Hash] config
39
+ # @return [Coach]
40
+ #
41
+ # ====Example
42
+ #
43
+ # @client = Coach4rb::Client.new(
44
+ # scheme: 'http',
45
+ # host: 'diufvm31.unifr.ch',
46
+ # port: 8090,
47
+ # path: '/CyberCoachServer/resources'
48
+ # )
49
+ #
50
+ def self.configure(config)
51
+ client = Client.new(
52
+ scheme: config[:scheme],
53
+ host: config[:host],
54
+ port: config[:port],
55
+ path: config[:path]
56
+ )
57
+
58
+ config[:debug] ||= false
59
+
60
+ response_parser = JsonResponseParser.new
61
+ Coach.new(client, response_parser, config[:debug])
62
+ end
63
+
64
+ end
@@ -0,0 +1,142 @@
1
+ module Coach4rb
2
+
3
+ module Builder
4
+
5
+ class Base
6
+
7
+ def initialize(a_hash={})
8
+ @struct = OpenStruct.new(a_hash)
9
+ end
10
+
11
+ def to_xml
12
+ a_hash = to_adjusted_hash
13
+ Gyoku.xml(type => a_hash)
14
+ end
15
+
16
+
17
+ def method_missing(meth,*args,&block)
18
+ struct.send meth, *args, &block
19
+ end
20
+
21
+
22
+ def type
23
+ raise 'Error'
24
+ end
25
+
26
+
27
+ private
28
+
29
+ def struct
30
+ @struct
31
+ end
32
+
33
+
34
+ def to_adjusted_hash
35
+ properties = struct.to_h
36
+ new_hash = {}
37
+ properties.each do |key, value|
38
+ string_key = key.to_s
39
+ string_key = string_key.gsub('_', '')
40
+ new_hash[string_key.to_sym] = value
41
+ end
42
+ new_hash
43
+ end
44
+
45
+ end
46
+
47
+
48
+ class Partnership < Base
49
+
50
+ def type
51
+ :partnership
52
+ end
53
+
54
+ end
55
+
56
+
57
+ class Subscription < Base
58
+
59
+ def type
60
+ :subscription
61
+ end
62
+
63
+ end
64
+
65
+
66
+ class User < Base
67
+
68
+ def type
69
+ :user
70
+ end
71
+
72
+ end
73
+
74
+
75
+ class Entry < Base
76
+
77
+
78
+ end
79
+
80
+
81
+ class Running < Entry
82
+
83
+ include Mixin::TrackWriter
84
+
85
+ def type
86
+ :entryrunning
87
+ end
88
+
89
+ end
90
+
91
+
92
+ class Cycling < Entry
93
+
94
+ include Mixin::TrackWriter
95
+
96
+ def type
97
+ :entrycycling
98
+ end
99
+
100
+ end
101
+
102
+
103
+ class Boxing < Entry
104
+
105
+ def type
106
+ :entryboxing
107
+ end
108
+
109
+ end
110
+
111
+
112
+ class Soccer < Entry
113
+
114
+ def type
115
+ :entrysoccer
116
+ end
117
+
118
+ end
119
+
120
+ class Entry
121
+
122
+ def self.builder(type)
123
+ klass = case type
124
+ when :running
125
+ Running
126
+ when :cycling
127
+ Cycling
128
+ when :boxing
129
+ Boxing
130
+ when :soccer
131
+ Soccer
132
+ else
133
+ raise 'Error: %s is not supported!' % type
134
+ end
135
+ klass.new(public_visible: Privacy::Public)
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+
142
+ end
@@ -0,0 +1,116 @@
1
+ module Coach4rb
2
+
3
+ # This class provides a simple http client.
4
+ #
5
+ #
6
+ class Client
7
+
8
+ def initialize(a_hash)
9
+ @service_uri = Addressable::URI.new a_hash
10
+ @basic_options = {
11
+ accept: :json,
12
+ content_type: :xml
13
+ }
14
+ end
15
+
16
+
17
+ def service_uri
18
+ @service_uri
19
+ end
20
+
21
+
22
+ def path
23
+ @service_uri.path
24
+ end
25
+
26
+
27
+ def site
28
+ @service_uri.site
29
+ end
30
+
31
+
32
+ def port
33
+ @service_uri.port
34
+ end
35
+
36
+
37
+ def host
38
+ @service_uri.host
39
+ end
40
+
41
+
42
+ def scheme
43
+ @service_uri.scheme
44
+ end
45
+
46
+
47
+ # Performs a get request.
48
+ #
49
+ # @param [String] url
50
+ # @param [Hash] options
51
+ # @param [Block] block
52
+ # @return [String] the payload of the response as string
53
+ def get(url, options={}, &block)
54
+ http_options = options.merge(@basic_options)
55
+ if block_given?
56
+ RestClient.get(url, http_options, &block)
57
+ else
58
+ RestClient.get(url, http_options)
59
+ end
60
+ end
61
+
62
+
63
+ # Performs a put request.
64
+ #
65
+ # @param [String] url
66
+ # @param [String] payload
67
+ # @param [Block] block
68
+ # @return [String] the payload of the response as string
69
+ #
70
+ def put(url, payload, options={}, &block)
71
+ http_options = options.merge(@basic_options)
72
+ if block_given?
73
+ RestClient.put(url, payload, http_options, &block)
74
+ else
75
+ RestClient.put(url, payload, http_options)
76
+ end
77
+
78
+ end
79
+
80
+
81
+ # Performs a post request.
82
+ #
83
+ # @param [String] url
84
+ # @param [String] payload
85
+ # @param [Block] block
86
+ # @return [String] the payload of the response as string
87
+ #
88
+ def post(url, payload, options={}, &block)
89
+ http_options = options.merge(@basic_options)
90
+ if block_given?
91
+ RestClient.post(url, payload, http_options, &block)
92
+ else
93
+ RestClient.post(url, payload, http_options)
94
+ end
95
+ end
96
+
97
+
98
+ # Performs a delete request.
99
+ #
100
+ # @param [String] url
101
+ # @param [Hash] options
102
+ # @param [Block] block
103
+ # @return [String] the payload of the response as string
104
+ #
105
+ def delete(url, options={}, &block)
106
+ http_options = options.merge(@basic_options)
107
+ if block_given?
108
+ RestClient.delete(url, http_options, &block)
109
+ else
110
+ RestClient.delete(url, http_options)
111
+ end
112
+ end
113
+
114
+ end
115
+
116
+ end