bloc-atlas 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a90e96e0da93a2ea8c16b41af09f71696efc0eef
4
+ data.tar.gz: 537bb1d948c99d854d9f16be45edfe919a1d91ba
5
+ SHA512:
6
+ metadata.gz: c072e895ebea7e8f4243190a19a8eb6a3977597c2f55583a0dbf178557665e79a2e19ba31352c99f6fb635849d346c5bf6e47e834570de9bf511dc7cd48ca73f
7
+ data.tar.gz: 74f96246ea2aed949e072fb5aaf51e71354cc04558ebb6878ba7d4076a8efc279be4d610150020aa69c6d332ccc4a5008c49ab4a11b7a59c8e5f2c3a7632d0ab
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.rbc
2
+ capybara-*.html
3
+ .rspec
4
+ /log
5
+ /tmp
6
+ /db/*.sqlite3
7
+ /db/*.sqlite3-journal
8
+ /public/system
9
+ /coverage/
10
+ /spec/tmp
11
+ **.orig
12
+ rerun.txt
13
+ pickle-email-*.html
14
+
15
+ # TODO Comment out these rules if you are OK with secrets being uploaded to the repo
16
+ config/initializers/secret_token.rb
17
+ config/secrets.yml
18
+
19
+ ## Environment normalization:
20
+ /.bundle
21
+ /vendor/bundle
22
+
23
+ # these should all be checked in to normalize the environment:
24
+ # Gemfile.lock, .ruby-version, .ruby-gemset
25
+
26
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
27
+ .rvmrc
28
+
29
+ # if using bower-rails ignore default bower_components path bower.json files
30
+ /vendor/assets/bower_components
31
+ *.bowerrc
32
+ bower.json
33
+
34
+ # Ignore pow environment settings
35
+ .powenv
36
+
37
+ /.bundle/
38
+ /.yardoc
39
+ /Gemfile.lock
40
+ /_yardoc/
41
+ /coverage/
42
+ /doc/
43
+ /pkg/
44
+ /spec/reports/
45
+ /tmp/
46
+
47
+
48
+ # Ignore application configuration
49
+ /config/application.yml
50
+ atlas-*
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in atlas.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ #Atlas
2
+
3
+ Ruby toolkit for the Workshop API.
4
+
5
+ ![Atlas](http://bloc-global-assets.s3.amazonaws.com/atlas.jpg)
6
+
7
+ ##Quick start
8
+
9
+ Install via Rubygems
10
+
11
+ ```bash
12
+ gem install atlas
13
+ ```
14
+
15
+ ... or add to your Gemfile
16
+
17
+ ```ruby
18
+ gem "atlas", "~> 0.1.0"
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ Create a new `Atlas` client using your Bloc username and password:
24
+
25
+ ```ruby
26
+ atlas = Atlas.new("api_key")
27
+ ```
28
+
29
+ Then use the client to make requests:
30
+
31
+ ```ruby
32
+ atlas.update_exercise(129, {name: "New Exercise Name"})
33
+ ```
34
+
35
+ ## Acknowledgements
36
+
37
+ This gem is inspired by [octokit](https://github.com/octokit)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/atlas.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "bloc-atlas"
7
+ spec.version = '0.1.1'
8
+ spec.authors = ["Rob Madden"]
9
+ spec.email = ["rob.madden@bloc.io"]
10
+
11
+ spec.summary = %q{A Ruby API client for Workshop.}
12
+ spec.description = %q{A Ruby API client for Workshop.}
13
+ spec.homepage = "http://rubygems.org/gems/bloc-atlas"
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.11"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_runtime_dependency 'httparty', '~>0.13.7'
33
+ spec.add_runtime_dependency 'activesupport', '~>4.2'
34
+ spec.add_runtime_dependency 'json', '~>1.8'
35
+ spec.add_development_dependency 'vcr', '~>2.9'
36
+ spec.add_development_dependency 'webmock', '~>1.22'
37
+ end
data/lib/atlas.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ require_relative 'client/exercises'
5
+
6
+ Dir[File.dirname(__FILE__) + '/client/*.rb'].each {|file| require file }
7
+
8
+ class Atlas
9
+ include HTTParty
10
+ include Exercises
11
+
12
+ def initialize(api_key, api_base_path="localhost:3000/api/v1")
13
+ @api_base_path = api_base_path
14
+ @api_key = api_key
15
+ end
16
+ end
@@ -0,0 +1,47 @@
1
+ require_relative 'class_factory'
2
+
3
+ module BaseClient
4
+ # Handle API timeouts
5
+ def handle_timeouts
6
+ begin
7
+ yield
8
+ rescue Net::OpenTimeout, Net::ReadTimeout
9
+ {}
10
+ end
11
+ end
12
+
13
+ # This needs a better name
14
+ def convert_response(response, name)
15
+ if success?(response.code)
16
+ body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body
17
+
18
+ if body.is_a?(Array)
19
+ body.map {|hash| ClassFactory.build_response_object(hash, name)}
20
+ else
21
+ ClassFactory.build_response_object(body, name)
22
+ end
23
+ else
24
+ return response
25
+ end
26
+ end
27
+
28
+ def success?(code)
29
+ code.between?(200, 299)
30
+ end
31
+
32
+ # Set the API Authorization Header
33
+ #
34
+ # @return [Hash] The authorization header
35
+ def auth_header
36
+ { "authorization" => 'Token token=' + @api_key }
37
+ end
38
+
39
+ def convert_keys(options)
40
+ options.keys.each {|k| options[k.to_s] = options.delete(k) if k.kind_of?(Symbol)}
41
+ options
42
+ end
43
+
44
+ def whitelist_params(options, whitelist)
45
+ options.select {|k, v| whitelist.include?(k)}
46
+ end
47
+ end
@@ -0,0 +1,30 @@
1
+ require 'json'
2
+ require 'active_support/inflector'
3
+
4
+ module ClassFactory
5
+ def ClassFactory.build_response_object(response_hash, name)
6
+ if Object.const_defined?(name.camelize)
7
+ name.camelize.constantize.new(response_hash)
8
+ else
9
+ generic_class = ClassFactory::GenericClass.new
10
+ generic_class.create_class name
11
+ name.camelize.constantize.new(response_hash)
12
+ end
13
+ end
14
+
15
+ class GenericClass
16
+ def create_class name
17
+ Object.const_set(name.classify,
18
+ Class.new do
19
+ def initialize(hash)
20
+ hash.each do |k,v|
21
+ self.instance_variable_set("@#{k}", v)
22
+ self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
23
+ self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
24
+ end
25
+ end
26
+ end
27
+ )
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,62 @@
1
+ require_relative 'base_client'
2
+
3
+ module Exercises
4
+ include BaseClient
5
+
6
+ # Get all exercises
7
+ #
8
+ # @return [HTTParty::Response] All exercises.
9
+ # @example Get all exercises
10
+ # Atlas#get_exercises
11
+ def get_exercises
12
+ url = "#{@api_base_path}/exercises"
13
+ handle_timeouts do
14
+ response = self.class.get(url, headers: auth_header)
15
+ convert_response(response, "exercises")
16
+ end
17
+ end
18
+
19
+ # Get an exercise
20
+ #
21
+ # @return [HTTParty::Response] One exercise.
22
+ # @example Get exercise by id
23
+ # Atlas#get_exercise(id)
24
+ def get_exercise(id)
25
+ url = "#{@api_base_path}/exercises/" + id.to_s
26
+ handle_timeouts do
27
+ response = self.class.get(url, headers: auth_header)
28
+ convert_response(response, "exercise")
29
+ end
30
+ end
31
+
32
+ # Update an exercise
33
+ #
34
+ # @param id [Integer] An exercise id.
35
+ # @param options [Hash] A customizable set of options.
36
+ # @option options [String] :name Exercise name.
37
+ # @option options [String] :body Exercise body.
38
+ # @option options [Integer] :status Exercise status.
39
+ # @option options [String] :slug Exercise slug.
40
+ # @option options [String] :specs Exercise specs.
41
+ # @option options [String] :starting_code Exercise starting code.
42
+ # @option options [String] :canonical_solution Exercise solution.
43
+ # @option options [String] :language Exercise language.
44
+ # @return [Exercise] The updated checkpoint
45
+ # @example Update a checkpoint
46
+ # Atlas#update_checkpoint(1, {name: 'Real Cool Exercise'})
47
+ def update_exercise(id, options={})
48
+ whitelist = ['name', 'body', 'status', 'slug','specs', 'starting_code', 'canonical_solution', 'language']
49
+
50
+ options = convert_keys(options)
51
+ params = whitelist_params(options, whitelist)
52
+ url = "#{@api_base_path}/exercises/#{id}"
53
+
54
+ handle_timeouts do
55
+ response = self.class.put(url,
56
+ headers: auth_header,
57
+ body: { exercise: params })
58
+ convert_response(response, "exercise")
59
+ end
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bloc-atlas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Rob Madden
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.13.7
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.13.7
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.8'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.9'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.22'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.22'
125
+ description: A Ruby API client for Workshop.
126
+ email:
127
+ - rob.madden@bloc.io
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - README.md
137
+ - Rakefile
138
+ - atlas.gemspec
139
+ - config/application.yml
140
+ - lib/atlas.rb
141
+ - lib/client/base_client.rb
142
+ - lib/client/class_factory.rb
143
+ - lib/client/exercises.rb
144
+ homepage: http://rubygems.org/gems/bloc-atlas
145
+ licenses:
146
+ - MIT
147
+ metadata:
148
+ allowed_push_host: https://rubygems.org
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.5.1
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: A Ruby API client for Workshop.
169
+ test_files: []