mconnect 0.1.2

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: 948eb6c67121e0e1ea374fea00c6308df6a93772
4
+ data.tar.gz: 0c48a76c2959530fec7df1e6dd896304ca963035
5
+ SHA512:
6
+ metadata.gz: 50ee996726e6f3ba3eb96b8c2df735e2b871cfddbd79cbbea3dd60e8e6d322f54f506b47095b0f38e27e402b554abc0ce27a616d27b960c38eca629043b04784
7
+ data.tar.gz: cd9baaa2313597de01c8c29bd033cfc5b5a7a9855746380c80ffa0e833e60d19fb52b7ef90682133368424656d6cf427a2a1417693acc1cda8ce721547b8640f
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Silas Sao
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,50 @@
1
+ # Mconnect
2
+
3
+ CLI to export endpoints from the MasteryConnect API to CSV
4
+
5
+ ## Important
6
+
7
+ This gem was specifically created for Ingenium Schools and has custom decorators
8
+ to format the CSV's how they need them. I'm open sourcing this code just in case
9
+ someone that runs across it, can benefit from it.
10
+
11
+ ## Installation
12
+
13
+ $ gem install mconnect
14
+
15
+ ## Usage
16
+
17
+ Running the gem without any arguments will display a list of commands that can
18
+ be ran.
19
+
20
+ $ mconnect
21
+
22
+ First things first, you need to setup a configuration file.
23
+
24
+ $ mconnect config
25
+
26
+ It will ask you some questions and generate a configuration yaml. After this is
27
+ complete, you will need to authorize your client.
28
+
29
+ $ mconnect auth
30
+
31
+ This will give you a URL to paste into your browser that will send you to a
32
+ login, then immediately after, an authorization page. It will be awaiting input
33
+ for your oauth verifier. This can be found as a parameter on the end of the URL
34
+ after you've been logged in and authorized. After you paste your oauth verifier
35
+ into the input and the program completes, your authorization file has been generated.
36
+
37
+ You are now free to run the get method in order to get a specific API endpoint
38
+ and export it to a CSV file. Use the '-e' to tell the program which endpoint you
39
+ want to export and '-o' to tell it where you want the data to go. You can see an
40
+ example below:
41
+
42
+ $ mconnect get -e teachers -o ~/Desktop/teachers.csv
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it ( https://github.com/sao/mconnect/fork )
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
data/bin/mconnect ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'mconnect'
3
+
4
+ Mconnect::CLI.start
data/lib/mconnect.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'mconnect/version'
2
+ require 'thor'
3
+ require 'oauth'
4
+ require 'yaml'
5
+ require 'json'
6
+ require 'csv'
7
+
8
+ require 'mconnect/worker'
9
+ require 'mconnect/decorator'
10
+ require 'mconnect/generator'
11
+ require 'mconnect/authorizer'
12
+
13
+ module Mconnect
14
+ class CLI < Thor
15
+ include Thor::Actions
16
+
17
+ desc "config", "create a new configuration yaml"
18
+ def config options = {}
19
+ say "Let's setup a configuration file.."
20
+
21
+ options['consumer_key'] = ask('What is the consumer key?').to_s
22
+ options['consumer_secret'] = ask('What is the consumer secret?').to_s
23
+
24
+ create_file '/tmp/mconnect/config.yml' do
25
+ options.to_yaml
26
+ end
27
+ end
28
+
29
+ desc "auth", "authorize client and create authorization yaml"
30
+ def auth
31
+ authorizer = Mconnect::Authorizer.new
32
+
33
+ say 'Copy and paste the following URL in your browser:'
34
+ say "\t#{authorizer.authorize_url}"
35
+
36
+ authorizer.verifier = ask('When you sign in, copy and paste the oauth verifier here:').to_s
37
+
38
+ create_file '/tmp/mconnect/authorization.yml' do
39
+ authorizer.authorization.to_yaml
40
+ end
41
+ end
42
+
43
+ desc "get", "gets endpoint and saves to CSV"
44
+ option :e, required: true, desc: "which endpoint to export"
45
+ option :o, required: true, desc: "destination to put CSV"
46
+ def get
47
+ directory = options[:o].to_s
48
+ endpoint = options[:e].to_s
49
+ authorizer = Mconnect::Authorizer.new
50
+ worker = Mconnect::Worker.new authorizer.access_token, endpoint
51
+ generator = Mconnect::Generator.new(worker.content, directory, endpoint)
52
+
53
+ generator.save_csv
54
+ end
55
+ end
56
+ end
57
+
@@ -0,0 +1,41 @@
1
+ require 'mconnect/helpers'
2
+
3
+ module Mconnect
4
+ class Authorizer
5
+ include Helpers
6
+
7
+ attr_writer :verifier
8
+ attr_reader :client
9
+
10
+ def initialize
11
+ oauth_options = load_yaml '/tmp/mconnect/config.yml'
12
+ client_options = { :site => 'https://api.masteryconnect.com',
13
+ :authorize_path => '/oauth/authorize',
14
+ :request_token_path => '/oauth/request_token',
15
+ :access_token_path => '/oauth/access_token' }
16
+
17
+ @client = OAuth::Consumer.new(
18
+ oauth_options['consumer_key'],
19
+ oauth_options['consumer_secret'],
20
+ client_options
21
+ )
22
+ end
23
+
24
+ def access_token
25
+ access_token = load_yaml '/tmp/mconnect/authorization.yml'
26
+ OAuth::AccessToken.new(client, access_token.token, access_token.secret)
27
+ end
28
+
29
+ def request_token
30
+ @request_token ||= client.get_request_token
31
+ end
32
+
33
+ def authorize_url
34
+ request_token.authorize_url
35
+ end
36
+
37
+ def authorization
38
+ request_token.get_access_token(oauth_verifier: @verifier)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,58 @@
1
+ module Mconnect
2
+ class Decorator
3
+ attr_accessor :content
4
+
5
+ def initialize content
6
+ @content = content
7
+ end
8
+
9
+ def remove_columns columns = []
10
+ @content.each do |hash|
11
+ hash.reject! do |k,v|
12
+ columns.include? k
13
+ end
14
+ end
15
+ end
16
+
17
+ def flatten_column column
18
+ orig_content = @content.dup
19
+ @content = []
20
+
21
+ orig_content.each do |hash|
22
+ @content << hash[column]
23
+ end
24
+
25
+ @content.flatten!
26
+ end
27
+
28
+ def sections_hash
29
+ orig_content = @content.dup
30
+ @content = []
31
+
32
+ orig_content.each do |hash|
33
+ hash['teachers'].each do |t|
34
+ hash['students'].each do |s|
35
+ @content << {
36
+ "id" => hash['id'],
37
+ "school_id" => hash['school_id'],
38
+ "name" => hash['name'],
39
+ "teacher_id" => t['id'],
40
+ "teacher_school_id" => t['school_id'],
41
+ "teacher_first_name" => t['first_name'],
42
+ "teacher_last_name" => t['last_name'],
43
+ "teacher_custom" => t['custom'],
44
+ "student_id" => s['id'],
45
+ "student_first_name" => s['first_name'],
46
+ "student_last_name" => s['last_name'],
47
+ "student_number" => s['student_number'],
48
+ "student_school_id" => s['school_id'],
49
+ "student_custom" => s['custom']
50
+ }
51
+ end
52
+ end
53
+ end
54
+
55
+ return @content
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,34 @@
1
+ require 'pry'
2
+ module Mconnect
3
+ class Generator
4
+ attr_accessor :content
5
+ attr_reader :directory, :endpoint
6
+
7
+ def initialize content, directory, endpoint
8
+ @content = content
9
+ @directory = directory
10
+ @endpoint = endpoint
11
+ end
12
+
13
+ def save_csv
14
+ decorator = Mconnect::Decorator.new @content
15
+
16
+ case endpoint
17
+ when "teachers"
18
+ content = decorator.remove_columns ['custom', 'saml_name']
19
+ when "students"
20
+ content = decorator.remove_columns ['sections']
21
+ when "standards"
22
+ content = decorator.flatten_column 'standards'
23
+ when "sections"
24
+ content = decorator.sections_hash
25
+ end
26
+
27
+ CSV.open("#{@directory}/#{@endpoint}.csv", "w", write_headers: true, headers: @content.first.keys) do |csv|
28
+ @content.each do |hash|
29
+ csv << hash.values
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ module Mconnect
2
+ module Helpers
3
+ def load_yaml data
4
+ if data.kind_of? Hash
5
+ YAML.load(data.to_yaml)
6
+ else
7
+ YAML.load_file(data)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Mconnect
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,26 @@
1
+ module Mconnect
2
+ class Worker
3
+ attr_accessor :content
4
+
5
+ def initialize access_token, endpoint
6
+ @access_token = access_token
7
+ @endpoint = endpoint
8
+
9
+ get_content
10
+ end
11
+
12
+ def get_content page_number = 1
13
+ @content ||= []
14
+
15
+ url = "/api/#{@endpoint}?page=#{page_number}"
16
+
17
+ @content << JSON.parse(@access_token.get(url, 'x-li-format' => 'json').body)
18
+ @content.flatten!
19
+
20
+ unless @content.count < page_number * 1000
21
+ puts "Getting page #{page_number}.."
22
+ get_content (page_number + 1)
23
+ end
24
+ end
25
+ end
26
+ end
data/mconnect.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mconnect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mconnect"
8
+ spec.version = Mconnect::VERSION
9
+ spec.authors = ["Silas Sao"]
10
+ spec.email = ["silassao@gmail.com"]
11
+ spec.summary = "Gem for the MasteryConnect API"
12
+ spec.description = "Gem created to convert MasteryConnect API endpoints to CSV"
13
+ spec.homepage = "http://silassao.com"
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_dependency "thor", "~> 0.19"
22
+ spec.add_dependency "oauth", "~> 0.4"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "pry"
28
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mconnect::Helpers do
4
+ include Mconnect::Helpers
5
+
6
+ describe "#load_yaml" do
7
+ let(:data) { { token: "XXXXXX", secret: "YYYYYY" } }
8
+
9
+ it "takes a hash as a parameter" do
10
+ expect(load_yaml(data)).to be_kind_of Hash
11
+ end
12
+
13
+ it "takes a file as a parameter" do
14
+ allow(YAML).to receive(:load_file).with('config.yml').and_return(data)
15
+ expect(load_yaml("config.yml")).to be_kind_of Hash
16
+ end
17
+ end
18
+ end
@@ -0,0 +1 @@
1
+ require 'mconnect'
data/tasks/rspec.rake ADDED
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mconnect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Silas Sao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.19'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.19'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oauth
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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: rspec
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: pry
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
+ description: Gem created to convert MasteryConnect API endpoints to CSV
98
+ email:
99
+ - silassao@gmail.com
100
+ executables:
101
+ - mconnect
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/mconnect
111
+ - lib/mconnect.rb
112
+ - lib/mconnect/authorizer.rb
113
+ - lib/mconnect/decorator.rb
114
+ - lib/mconnect/generator.rb
115
+ - lib/mconnect/helpers.rb
116
+ - lib/mconnect/version.rb
117
+ - lib/mconnect/worker.rb
118
+ - mconnect.gemspec
119
+ - spec/helpers_spec.rb
120
+ - spec/spec_helper.rb
121
+ - tasks/rspec.rake
122
+ homepage: http://silassao.com
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.2.2
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Gem for the MasteryConnect API
146
+ test_files:
147
+ - spec/helpers_spec.rb
148
+ - spec/spec_helper.rb
149
+ has_rdoc: