dbc-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format documentation --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+
7
+ group :development, :test do
8
+ gem 'rspec-rails'
9
+ end
10
+
11
+
12
+ group :test do
13
+ gem 'database_cleaner'
14
+ end
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ #DBC Ruby
2
+
3
+ ## Installation
4
+
5
+ If you want to use the Dev Bootcamp (DBC) Ruby gem, run:
6
+
7
+ gem install --source https://someurl
8
+
9
+ ## Requirements
10
+
11
+ * Ruby 1.9.3
12
+ * rest-client
13
+
14
+ ## Development
15
+
16
+ Run unit test with:
17
+
18
+ bundle exec rake test
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new('spec')
4
+
5
+ task :default => :spec
data/dbc-ruby.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
3
+
4
+ require 'dbc-ruby/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.authors = ["Kevin Buchanan"]
8
+ gem.email = ["kevaustinbuch@gmail.com"]
9
+ gem.description = %q{REST API client for http://api.devbootcamp.com}
10
+ gem.summary = %q{REST API client for http://api.devbootcamp.com}
11
+ gem.homepage = "https://github.com/socrates-api/dbc-ruby"
12
+
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ gem.name = "dbc-ruby"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = DBC::VERSION
19
+
20
+ gem.add_dependency 'rest-client'
21
+ gem.add_development_dependency 'rspec', '~> 2.7'
22
+ gem.add_development_dependency 'webmock'
23
+ end
@@ -0,0 +1,12 @@
1
+ module DBC
2
+ class ApiKey < DbcObject
3
+ def self.endpoint(id)
4
+ "/api_keys/" + id.to_s
5
+ end
6
+
7
+ def self.find(id)
8
+ api_response = DBC.request(self.endpoint(id))
9
+ self.create_dbc_object(api_response)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module DBC
2
+ class Challenge < DbcObject
3
+ def self.endpoint(id = '')
4
+ "/challenges/" + id.to_s
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module DBC
2
+ class ChallengeAttempt < DbcObject
3
+ def self.endpoint(user_id, id = '')
4
+ "/users/#{user_id}/challenge_attempts/" + id
5
+ end
6
+
7
+ def self.find(user_id, id)
8
+ api_response = DBC.request(endpoint(user_id, id))
9
+ self.create_dbc_object(api_response)
10
+ end
11
+
12
+ def self.all(user_id, options = {})
13
+ api_response = DBC.request(endpoint(user_id), options)
14
+ self.create_dbc_objects(api_response)
15
+ end
16
+
17
+ def challenge
18
+ Challenge.find(@challenge_id)
19
+ end
20
+ end
21
+ end
data/lib/dbc/cohort.rb ADDED
@@ -0,0 +1,12 @@
1
+ module DBC
2
+ class Cohort < DbcObject
3
+ def initialize(attributes)
4
+ super
5
+ @students.map! { |user| DBC::User.new(user) }
6
+ end
7
+
8
+ def self.endpoint(id = '')
9
+ "/cohorts/" + id.to_s
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,36 @@
1
+ module DBC
2
+ class DbcObject
3
+ def initialize(attributes)
4
+ attributes.each do |key, value|
5
+ instance_variable_set("@#{key}", value)
6
+ end
7
+ end
8
+
9
+ def self.endpoint(id = '')
10
+ '/' + id.to_s
11
+ end
12
+
13
+ def self.all(options = {})
14
+ api_response = DBC.request(self.endpoint, options)
15
+ self.create_dbc_objects(api_response)
16
+ end
17
+
18
+ def self.find(id)
19
+ api_response = DBC.request(self.endpoint(id))
20
+ self.create_dbc_object(api_response)
21
+ end
22
+
23
+ private
24
+
25
+ def self.create_dbc_objects(api_response)
26
+ api_response.values.first.map! do |object|
27
+ self.new(object)
28
+ end
29
+ api_response
30
+ end
31
+
32
+ def self.create_dbc_object(api_response)
33
+ { api_response.keys.first => self.new(api_response.values.first) }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ module DBC
2
+ class Exercise < DbcObject
3
+ def self.endpoint(id = '')
4
+ "/exercises/" + id.to_s
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module DBC
2
+ class ExerciseAttempt < DbcObject
3
+ def self.endpoint(user_id, id = '')
4
+ "/users/#{user_id}/exercise_attempts/" + id
5
+ end
6
+
7
+ def self.find(user_id, id)
8
+ api_response = DBC.request(endpoint(user_id, id))
9
+ self.create_dbc_object(api_response)
10
+ end
11
+
12
+ def self.all(user_id, options = {})
13
+ api_response = DBC.request(endpoint(user_id), options)
14
+ self.create_dbc_objects(api_response)
15
+ end
16
+
17
+ def exercise
18
+ Exercise.find(@exercise_id)
19
+ end
20
+ end
21
+ end
data/lib/dbc/user.rb ADDED
@@ -0,0 +1,19 @@
1
+ module DBC
2
+ class User < DbcObject
3
+ def self.endpoint(id = '')
4
+ "/users/" + id.to_s
5
+ end
6
+
7
+ def challenge_attempts
8
+ ChallengeAttempt.all(@id)
9
+ end
10
+
11
+ def exercise_attempts
12
+ ExerciseAttempt.all(@id)
13
+ end
14
+
15
+ def cohort
16
+ Cohort.find(@cohort_id)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module DBC
2
+ VERSION = '1.0.0'
3
+ end
data/lib/dbc-ruby.rb ADDED
@@ -0,0 +1,50 @@
1
+ require_relative 'dbc/dbc_object'
2
+ require_relative 'dbc/user'
3
+ require_relative 'dbc/cohort'
4
+ require_relative 'dbc/challenge'
5
+ require_relative 'dbc/exercise'
6
+ require_relative 'dbc/api_key'
7
+ require_relative 'dbc/challenge_attempt'
8
+ require_relative 'dbc/exercise_attempt'
9
+
10
+ require 'json'
11
+ require 'rest_client'
12
+
13
+ module DBC
14
+ @api_url = 'https://api.devbootcamp.com'
15
+ @token = nil
16
+ @shared_token = nil
17
+
18
+ def self.token=(token)
19
+ @token = token
20
+ end
21
+
22
+ def self.token
23
+ @token ||= ENV['DBC_API']
24
+ end
25
+
26
+ def self.shared_token=(token)
27
+ @shared_token = token
28
+ end
29
+
30
+ def self.shared_token
31
+ @shared_token ||= ENV['DBC_SHARED']
32
+ end
33
+
34
+ def self.endpoint
35
+ @api_url
36
+ end
37
+
38
+ def self.request(path, options = {})
39
+ raise ArgumentError.new "You must set ENV['DBC_API'] to your api token" unless self.token
40
+ response = RestClient.get(@api_url + path, {accept: :json, authorization: 'DBC-API' + ' ' + @token, params: options})
41
+ JSON.parse(response.body, symbolize_names: true)
42
+ end
43
+
44
+ def self.request_with_shared_token(path, options = {})
45
+ raise ArgumentError.new "You must set ENV['DBC_SHARED'] to your shared token" unless self.shared_token
46
+ response = RestClient.get(@api_url + path, {accept: :json, authorization: 'DBC-SHARED' + ' ' + @shared_token , params: options})
47
+ JSON.parse(response.body, symbolize_names: true)
48
+ end
49
+ end
50
+
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe DBC::DbcObject do
4
+ describe '.all' do
5
+ before do
6
+ ENV.stub(:[]).with('DBC_API').and_return('test123')
7
+ DBC.stub(:request).and_return({objects: [{attribute: 'test'}]})
8
+ end
9
+
10
+ it 'should make a request with the correct endpoint' do
11
+ expect(DBC).to receive(:request).with('/', {})
12
+ DBC::DbcObject.all()
13
+ end
14
+
15
+ it 'should return an array of objects' do
16
+ objects = DBC::DbcObject.all()[:objects]
17
+ expect(objects).to be_a(Array)
18
+ expect(objects.first).to be_a(DBC::DbcObject)
19
+ end
20
+ end
21
+
22
+ describe '.find' do
23
+ before do
24
+ ENV.stub(:[]).with('DBC_API').and_return('test123')
25
+ DBC.stub(:request).and_return({object: {attribute: 'test'}})
26
+ end
27
+
28
+ it 'should make a request with the correct endpoint' do
29
+ expect(DBC).to receive(:request).with('/1')
30
+ DBC::DbcObject.find(1)
31
+ end
32
+
33
+ it 'should return one object' do
34
+ expect(DBC::DbcObject.find(1)[:object]).to be_a(DBC::DbcObject)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe DBC do
4
+ describe '.request' do
5
+ context 'with an api token' do
6
+ before do
7
+ ENV.stub(:[]).with('DBC_API').and_return('test123')
8
+ end
9
+
10
+ it 'should make an api request' do
11
+ stub_request(:get, DBC.endpoint).to_return(body: JSON.generate({test: 'test'}))
12
+ expect{ DBC.request('') }.to_not raise_error
13
+ end
14
+ end
15
+
16
+ context 'without an api token' do
17
+ it 'should throw an argument error' do
18
+ expect{ DBC.request() }.to raise_error(ArgumentError)
19
+ end
20
+ end
21
+ end
22
+
23
+ describe '.request_with_shared_token' do
24
+ context 'with a shared token' do
25
+ before do
26
+ ENV.stub(:[]).with('DBC_SHARED').and_return('test123')
27
+ end
28
+
29
+ it 'should make an api request' do
30
+ stub_request(:get, DBC.endpoint).to_return(body: JSON.generate({test: 'test'}))
31
+ expect{ DBC.request_with_shared_token('') }.to_not raise_error
32
+ end
33
+ end
34
+
35
+ context 'without a shared token' do
36
+ it 'should throw an argument error' do
37
+ expect{ DBC.request_with_shared_token() }.to raise_error(ArgumentError)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+ require 'rspec'
7
+ require 'webmock/rspec'
8
+ require 'dbc-ruby'
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dbc-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kevin Buchanan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.7'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.7'
46
+ - !ruby/object:Gem::Dependency
47
+ name: webmock
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: REST API client for http://api.devbootcamp.com
63
+ email:
64
+ - kevaustinbuch@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - README.md
73
+ - Rakefile
74
+ - dbc-ruby.gemspec
75
+ - lib/dbc-ruby.rb
76
+ - lib/dbc-ruby/version.rb
77
+ - lib/dbc/api_key.rb
78
+ - lib/dbc/challenge.rb
79
+ - lib/dbc/challenge_attempt.rb
80
+ - lib/dbc/cohort.rb
81
+ - lib/dbc/dbc_object.rb
82
+ - lib/dbc/exercise.rb
83
+ - lib/dbc/exercise_attempt.rb
84
+ - lib/dbc/user.rb
85
+ - spec/lib/dbc-ruby_spec.rb
86
+ - spec/lib/dbc/dbc_object_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/socrates-api/dbc-ruby
89
+ licenses: []
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.24
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: REST API client for http://api.devbootcamp.com
112
+ test_files:
113
+ - spec/lib/dbc-ruby_spec.rb
114
+ - spec/lib/dbc/dbc_object_spec.rb
115
+ - spec/spec_helper.rb