calx_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b63479923ef59460370bd9e4e3aecccf0a01103
4
+ data.tar.gz: 349ee83773fd8b207229c94555c151e952c2a166
5
+ SHA512:
6
+ metadata.gz: 9a3ff2304d4cc0c04a45eaccc4c90eb58093f6483a49937f05cfa1cc7f30725cf784d71df9e05f07ea95366aa7624947c3d8a2866af5f47c02220a1134d0aa58
7
+ data.tar.gz: a5407fa15f2b713e092ab8a19aa7103118b3b34821cf52d820e27c851af57e90a35bbc9eae72e0a7f539237d6adbceb232962b73e449cfde974d0fe5335549cb
@@ -0,0 +1 @@
1
+ coverage
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Metrics/AbcSize:
5
+ Max: 30
6
+
7
+ Metrics/LineLength:
8
+ Max: 100
9
+
10
+ Metrics/MethodLength:
11
+ Max: 15
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Style/DotPosition:
17
+ EnforcedStyle: trailing
18
+
19
+ Style/FrozenStringLiteralComment:
20
+ Enabled: false
21
+
22
+ Style/IfInsideElse:
23
+ Enabled: false
24
+
25
+ Style/RaiseArgs:
26
+ EnforcedStyle: compact
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.3.3
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
@@ -0,0 +1,81 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ calx_client (0.0.1)
5
+ api-auth
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ addressable (2.5.0)
11
+ public_suffix (~> 2.0, >= 2.0.2)
12
+ api-auth (2.1.0)
13
+ ast (2.3.0)
14
+ coveralls (0.8.17)
15
+ json (>= 1.8, < 3)
16
+ simplecov (~> 0.12.0)
17
+ term-ansicolor (~> 1.3)
18
+ thor (~> 0.19.1)
19
+ tins (~> 1.6)
20
+ crack (0.4.3)
21
+ safe_yaml (~> 1.0.0)
22
+ diff-lcs (1.2.5)
23
+ docile (1.1.5)
24
+ hashdiff (0.3.2)
25
+ json (2.0.2)
26
+ parser (2.3.3.1)
27
+ ast (~> 2.2)
28
+ powerpack (0.1.1)
29
+ public_suffix (2.0.5)
30
+ rainbow (2.2.1)
31
+ rake (12.0.0)
32
+ rspec (3.5.0)
33
+ rspec-core (~> 3.5.0)
34
+ rspec-expectations (~> 3.5.0)
35
+ rspec-mocks (~> 3.5.0)
36
+ rspec-core (3.5.4)
37
+ rspec-support (~> 3.5.0)
38
+ rspec-expectations (3.5.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.5.0)
41
+ rspec-mocks (3.5.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.5.0)
44
+ rspec-support (3.5.0)
45
+ rubocop (0.46.0)
46
+ parser (>= 2.3.1.1, < 3.0)
47
+ powerpack (~> 0.1)
48
+ rainbow (>= 1.99.1, < 3.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (~> 1.0, >= 1.0.1)
51
+ ruby-progressbar (1.8.1)
52
+ safe_yaml (1.0.4)
53
+ simplecov (0.12.0)
54
+ docile (~> 1.1.0)
55
+ json (>= 1.8, < 3)
56
+ simplecov-html (~> 0.10.0)
57
+ simplecov-html (0.10.0)
58
+ term-ansicolor (1.4.0)
59
+ tins (~> 1.0)
60
+ thor (0.19.4)
61
+ tins (1.13.0)
62
+ unicode-display_width (1.1.2)
63
+ webmock (2.3.2)
64
+ addressable (>= 2.3.6)
65
+ crack (>= 0.3.2)
66
+ hashdiff
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ calx_client!
73
+ coveralls
74
+ rake
75
+ rspec
76
+ rubocop
77
+ simplecov
78
+ webmock
79
+
80
+ BUNDLED WITH
81
+ 1.13.6
@@ -0,0 +1,22 @@
1
+ Copyright © 2017 Mert Guldur
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.
@@ -0,0 +1,8 @@
1
+ # CalX Client
2
+
3
+ [![Build
4
+ Status](https://travis-ci.org/mertguldur/calx_client.svg?branch=master)](https://travis-ci.org/mertguldur/calx_client)
5
+ [![Coverage
6
+ Status](https://coveralls.io/repos/github/mertguldur/calx_client/badge.svg?branch=master)](https://coveralls.io/github/mertguldur/calx_client?branch=master)
7
+
8
+ Ruby API client to access CalX events.
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RuboCop::RakeTask.new
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: [:rubocop, :spec]
@@ -0,0 +1,22 @@
1
+ require File.expand_path('lib/calx_client/version', File.dirname(__FILE__))
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'calx_client'
5
+ s.version = CalX::VERSION
6
+ s.date = '2016-12-09'
7
+ s.summary = 'CalX API Client'
8
+ s.description = 'API client to access CalX events'
9
+ s.authors = ['Mert Guldur']
10
+ s.email = 'mertguldur@gmail.com'
11
+ s.files = `git ls-files`.split("\n")
12
+ s.license = 'MIT'
13
+
14
+ s.add_dependency 'api-auth'
15
+
16
+ s.add_development_dependency 'rake'
17
+ s.add_development_dependency 'rspec'
18
+ s.add_development_dependency 'webmock'
19
+ s.add_development_dependency 'rubocop'
20
+ s.add_development_dependency 'simplecov'
21
+ s.add_development_dependency 'coveralls'
22
+ end
@@ -0,0 +1,123 @@
1
+ module CalX
2
+ class Error < StandardError; end
3
+ class ServerError < Error; end
4
+ class ClientError < Error; end
5
+ class AuthenticationError < ClientError; end
6
+
7
+ class Client
8
+ def initialize(access_id, secret_key, options = {})
9
+ @access_id = access_id
10
+ @secret_key = secret_key
11
+ @host = host(options)
12
+ end
13
+
14
+ def authorize(user_id)
15
+ post('/app_authorization_requests', user_id: user_id)
16
+ end
17
+
18
+ def events(user_id, params = {})
19
+ get("/users/#{user_id}/events", params)
20
+ end
21
+
22
+ def event(event_id)
23
+ get("/events/#{event_id}")
24
+ end
25
+
26
+ def create_event(user_id, params)
27
+ post("/users/#{user_id}/events", params)
28
+ end
29
+
30
+ def update_event(event_id, params)
31
+ put("/events/#{event_id}", params)
32
+ end
33
+
34
+ def delete_event(event_id)
35
+ delete("/events/#{event_id}")
36
+ end
37
+
38
+ private
39
+
40
+ def get(request_uri, params = {})
41
+ request_uri += "?#{encode_params(params)}" unless params.nil?
42
+ uri = URI(@host + request_uri)
43
+ request = Net::HTTP::Get.new(uri.request_uri)
44
+ request(uri, request)
45
+ end
46
+
47
+ def post(request_uri, params)
48
+ uri = URI(@host + request_uri)
49
+ request = Net::HTTP::Post.new(uri.request_uri)
50
+ request.form_data = params
51
+ request(uri, request)
52
+ end
53
+
54
+ def put(request_uri, params)
55
+ uri = URI(@host + request_uri)
56
+ request = Net::HTTP::Put.new(uri.request_uri)
57
+ request.form_data = params
58
+ request(uri, request)
59
+ end
60
+
61
+ def delete(request_uri)
62
+ uri = URI(@host + request_uri)
63
+ request = Net::HTTP::Delete.new(uri.request_uri)
64
+ request(uri, request)
65
+ end
66
+
67
+ def request(uri, request)
68
+ signed_request = ApiAuth.sign!(request, @access_id, @secret_key)
69
+ response = http(uri).request(signed_request)
70
+ parse_response(uri, response)
71
+ end
72
+
73
+ def parse_response(uri, response)
74
+ case response
75
+ when Net::HTTPNoContent
76
+ :no_content
77
+ when Net::HTTPSuccess
78
+ parse_success_response(response)
79
+ when Net::HTTPUnauthorized
80
+ raise AuthenticationError.new(error_response_message(response, uri))
81
+ when Net::HTTPClientError
82
+ raise ClientError.new(error_response_message(response, uri))
83
+ when Net::HTTPServerError
84
+ raise ServerError.new(error_response_message(response, uri))
85
+ else
86
+ raise Error.new(error_response_message(response, uri))
87
+ end
88
+ end
89
+
90
+ def parse_success_response(response)
91
+ if response['Content-Type'].split(';').first == 'application/json'
92
+ JSON.parse(response.body)
93
+ else
94
+ response.body
95
+ end
96
+ end
97
+
98
+ def error_response_message(response, uri)
99
+ message = "#{response.code} response from #{uri.host}"
100
+ message + " | Response body: #{JSON.parse(response.body)}" unless response.body.nil?
101
+ end
102
+
103
+ def http(uri)
104
+ http = Net::HTTP.new(uri.host, uri.port)
105
+ http.use_ssl = uri.is_a?(URI::HTTPS)
106
+ http
107
+ end
108
+
109
+ def host(options = {})
110
+ (options[:host] || 'http://localhost:3000') + '/api/v1'
111
+ end
112
+
113
+ def encode_params(params)
114
+ params.flat_map do |k, vs|
115
+ Array(vs).map { |v| "#{escape_component(k)}=#{escape_component(v)}" }
116
+ end.join('&')
117
+ end
118
+
119
+ def escape_component(component)
120
+ CGI.escape(component.to_s)
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,188 @@
1
+ require 'spec_helper'
2
+ require 'api-auth'
3
+ require 'webmock/rspec'
4
+
5
+ describe CalX::Client do
6
+ let(:host) { 'http://localhost:3000/api/v1' }
7
+ let(:headers) { { 'Content-Type' => 'application/json' } }
8
+
9
+ subject { described_class.new('test_tenant', '12345') }
10
+
11
+ describe '#authorize' do
12
+ let(:user_id) { '1' }
13
+ let(:tenant_id) { '1' }
14
+ let(:response_body) { { 'user_id' => user_id, 'tenant_id' => tenant_id } }
15
+
16
+ before do
17
+ stub_request(:post, "#{host}/app_authorization_requests").
18
+ with(body: { user_id: user_id }).
19
+ to_return(body: response_body.to_json, status: 200, headers: headers)
20
+ end
21
+
22
+ it 'returns the app authorization request' do
23
+ expect(subject.authorize(user_id)).to eq(response_body)
24
+ end
25
+ end
26
+
27
+ describe '#events' do
28
+ let(:user_id) { '1' }
29
+ let(:date) { Date.today }
30
+ let(:response_body) do
31
+ { 'user_id' => user_id, 'event_id' => '1' }
32
+ end
33
+
34
+ before do
35
+ stub_request(:get, "#{host}/users/#{user_id}/events").
36
+ with(query: hash_including('date' => date.to_s)).
37
+ to_return(body: response_body.to_json, status: 200, headers: headers)
38
+ end
39
+
40
+ it 'returns the events' do
41
+ expect(subject.events(user_id, date: date)).to eq(response_body)
42
+ end
43
+ end
44
+
45
+ describe '#event' do
46
+ let(:event_id) { '1' }
47
+ let(:response_body) do
48
+ { 'event_id' => event_id }
49
+ end
50
+
51
+ before do
52
+ stub_request(:get, "#{host}/events/#{event_id}").
53
+ to_return(body: response_body.to_json, status: 200, headers: headers)
54
+ end
55
+
56
+ it 'returns the event' do
57
+ expect(subject.event(event_id)).to eq(response_body)
58
+ end
59
+ end
60
+
61
+ describe '#create_event' do
62
+ let(:user_id) { '1' }
63
+ let(:event_id) { '1' }
64
+ let(:event_params) do
65
+ { 'title' => 'test' }
66
+ end
67
+ let(:response_body) do
68
+ { 'event_id' => event_id, 'title' => 'test' }
69
+ end
70
+
71
+ before do
72
+ stub_request(:post, "#{host}/users/#{user_id}/events").
73
+ with(body: event_params).
74
+ to_return(body: response_body.to_json, status: 200, headers: headers)
75
+ end
76
+
77
+ it 'returns the created event' do
78
+ expect(subject.create_event(user_id, event_params)).to eq(response_body)
79
+ end
80
+ end
81
+
82
+ describe '#update_event' do
83
+ let(:event_id) { '1' }
84
+ let(:event_params) do
85
+ { 'title' => 'test' }
86
+ end
87
+ let(:response_body) do
88
+ { 'event_id' => event_id, 'title' => 'test' }
89
+ end
90
+
91
+ before do
92
+ stub_request(:put, "#{host}/events/#{event_id}").
93
+ with(body: event_params).
94
+ to_return(body: response_body.to_json, status: 200, headers: headers)
95
+ end
96
+
97
+ it 'returns the updated event' do
98
+ expect(subject.update_event(event_id, event_params)).to eq(response_body)
99
+ end
100
+ end
101
+
102
+ describe '#delete_event' do
103
+ let(:event_id) { '1' }
104
+ let(:response_body) do
105
+ { 'event_id' => event_id, 'title' => 'test' }
106
+ end
107
+
108
+ before do
109
+ stub_request(:delete, "#{host}/events/#{event_id}").
110
+ to_return(body: response_body.to_json, status: 200, headers: headers)
111
+ end
112
+
113
+ it 'returns the deleted event' do
114
+ expect(subject.delete_event(event_id)).to eq(response_body)
115
+ end
116
+ end
117
+
118
+ context 'text body' do
119
+ let(:event_id) { 1 }
120
+ let(:headers) { { 'Content-Type' => 'application/text' } }
121
+
122
+ before do
123
+ stub_request(:get, "#{host}/events/#{event_id}").
124
+ to_return(body: 'test', status: 200, headers: headers)
125
+ end
126
+
127
+ it 'returns the text body' do
128
+ expect(subject.event(event_id)).to eq('test')
129
+ end
130
+ end
131
+
132
+ context 'error cases' do
133
+ let(:event_id) { 1 }
134
+
135
+ before do
136
+ stub_request(:get, "#{host}/events/#{event_id}").
137
+ to_return(body: '', status: status_code, headers: headers)
138
+ end
139
+
140
+ context 'HTTP no content' do
141
+ let(:status_code) { 204 }
142
+
143
+ it 'is no content' do
144
+ expect(subject.event(event_id)).to eq(:no_content)
145
+ end
146
+ end
147
+
148
+ context 'HTTP unauthorized' do
149
+ let(:status_code) { 401 }
150
+
151
+ it 'raises AuthenticationError' do
152
+ expect do
153
+ subject.event(event_id)
154
+ end.to raise_error(CalX::AuthenticationError)
155
+ end
156
+ end
157
+
158
+ context 'HTTP client error' do
159
+ let(:status_code) { 400 }
160
+
161
+ it 'raises ClientError' do
162
+ expect do
163
+ subject.event(event_id)
164
+ end.to raise_error(CalX::ClientError)
165
+ end
166
+ end
167
+
168
+ context 'HTTP server error' do
169
+ let(:status_code) { 500 }
170
+
171
+ it 'raises ServerError' do
172
+ expect do
173
+ subject.event(event_id)
174
+ end.to raise_error(CalX::ServerError)
175
+ end
176
+ end
177
+
178
+ context 'HTTP client error' do
179
+ let(:status_code) { 308 }
180
+
181
+ it 'raises Error' do
182
+ expect do
183
+ subject.event(event_id)
184
+ end.to raise_error(CalX::Error)
185
+ end
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,11 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ Coveralls::SimpleCov::Formatter,
6
+ SimpleCov::Formatter::HTMLFormatter,
7
+ ]
8
+
9
+ SimpleCov.start
10
+
11
+ require 'calx_client'
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: calx_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mert Guldur
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: api-auth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: API client to access CalX events
112
+ email: mertguldur@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - ".gitignore"
118
+ - ".rubocop.yml"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - calx_client.gemspec
126
+ - lib/calx_client.rb
127
+ - spec/calx_client_spec.rb
128
+ - spec/spec_helper.rb
129
+ homepage:
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.6.8
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: CalX API Client
153
+ test_files: []