educandus_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in educandus-api.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alberto Lagos Toro
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,29 @@
1
+ # Educandus::Api
2
+
3
+ Gem that provides simple access (like an __API__) to the __LMS__ of the _Universidad de Talca_. Please remember that the __LMS__ do not offer any kind of __API__. This gem only _parse_ the html and it shows to you like a __API__ would do.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'educandus-api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install educandus-api
18
+
19
+ ## Usage
20
+
21
+ You only need run this in a _Rack Application_ or Middleware.
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/educandus_api/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Alberto Lagos Toro"]
6
+ gem.email = ["alberto.lagos@gmail.com"]
7
+ gem.description = %q{Library allow connect to lms EDUCANDUS.}
8
+ gem.summary = %q{Library allow connect to lms EDUCANDUS.}
9
+ gem.homepage = "http://github.com/nexon/educandus-api"
10
+
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "educandus_api"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Educandus::Api::VERSION
17
+
18
+ gem.add_dependency 'grape'
19
+ gem.add_dependency 'nokogiri'
20
+ gem.add_development_dependency 'rspec', '~> 2.9'
21
+ end
@@ -0,0 +1,23 @@
1
+ require "educandus_api/version"
2
+
3
+ module Educandus
4
+ class API < Grape::API
5
+ version 'v1', :using => :header, :vendor => 'educandus'
6
+
7
+
8
+ desc "Authenticate User in the LMS Platform"
9
+ post "authenticate" do
10
+ @user = Educandus::Estab.new("lms.educandus.cl", "/login/index.php", params[:username], params[:password])
11
+ @user.connect
12
+ end
13
+ resources :courses do
14
+ get "list" do
15
+ if @user
16
+ @courses = @user.sendRequest :path => "/", :method => "get"
17
+ else
18
+
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Educandus
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,95 @@
1
+ require 'open-uri'
2
+ require "net/http"
3
+ require 'net/https'
4
+ require_relative 'errors'
5
+ module Educandus
6
+ class Estab
7
+ def initialize(url, path,user,pass)
8
+ @url = url
9
+ @user = user
10
+ @pass = pass
11
+ @path = path
12
+ @response = Hash.new
13
+ end
14
+ def connect
15
+ self.setupConnection
16
+ begin
17
+ self.doConnect
18
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
19
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
20
+ raise Educandus::Error, "The Host seems to be down (like always)."
21
+ end
22
+ end
23
+
24
+ def sendRequest(options = {})
25
+ raise Educandus::Error, "The response must be valid!." unless (!@response.empty? || !@cookie.nil?)
26
+ if options.has_key?("url")
27
+ url = options[:url]
28
+ else
29
+ url = @url
30
+ end
31
+ if options.has_key?("headers") && options[:headers].empty?
32
+ headers = {"Cookies" => @response["set-cookies"]}
33
+ else
34
+ if !@response.empty?
35
+ headers = {"Cookie" => @response["set-cookie"]}
36
+ else
37
+ headers = {}
38
+ end
39
+ end
40
+ if options.has_key?("path")
41
+ path = options[:path]
42
+ else
43
+ path = "/"
44
+ end
45
+ if options.has_key?("post")
46
+ if !options.has_key?("method") || !options[:method] == "post"
47
+ raise Educandus::Error, "If you pass POST parameters, you must set the METHOD to POST"
48
+ else
49
+ post = options[:post]
50
+ end
51
+ end
52
+ @connection = Net::HTTP.new(url,443)
53
+ @connection.use_ssl = true
54
+ if options.has_key?(:method)
55
+ method = options[:method].downcase
56
+ else
57
+ method = "get"
58
+ end
59
+ begin
60
+ method == "post" ? (rsp, data = @connection.post(path,post,headers)) : ''
61
+ method == "get" ? (rsp, data = @connection.get(path,headers)) : ''
62
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
63
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
64
+ raise Educandus::Error, "The Host seems to be down (like always)."
65
+ end
66
+ puts rsp.body
67
+ end
68
+
69
+ private
70
+ def setupConnection
71
+ @connection = Net::HTTP.new(@url,443)
72
+ @connection.use_ssl = true
73
+ self.preLoadCookies
74
+ self.preparePost
75
+ self.prepareHeaders
76
+ end
77
+ def preLoadCookies
78
+ resp, data = @connection.get(@path, {})
79
+ @cookie = resp.response['set-cookie']
80
+ end
81
+ def preparePost
82
+ @post = "username=#{@user}&password=#{@pass}"
83
+ end
84
+ def prepareHeaders
85
+ @requestHeaders = {'Cookie' => @cookie, 'Content-Type' => 'application/x-www-form-urlencoded'}
86
+ end
87
+ def doConnect
88
+ rsp, data = @connection.post(@path, @post, @requestHeaders)
89
+ self.generateReadableResponse(rsp)
90
+ end
91
+ def generateReadableResponse(rsp)
92
+ rsp.each {|key, val| @response[key] = val}
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ require 'nokogiri'
2
+ module Educandus
3
+ class Parser
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Educandus
2
+ module Api
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Educandus::Api do
4
+ it 'requires additional testing'
5
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ $:.unshift File.dirname(__FILE__) + '/../lib'
8
+ require 'educandus-api'
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: educandus_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alberto Lagos Toro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: grape
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: nokogiri
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.9'
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: '2.9'
62
+ description: Library allow connect to lms EDUCANDUS.
63
+ email:
64
+ - alberto.lagos@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - educandus-api.gemspec
76
+ - lib/educandus-api.rb
77
+ - lib/educandus_api/errors.rb
78
+ - lib/educandus_api/estab.rb
79
+ - lib/educandus_api/parser.rb
80
+ - lib/educandus_api/version.rb
81
+ - spec/educandus_api_spec.rb
82
+ - spec/spec_helper.rb
83
+ homepage: http://github.com/nexon/educandus-api
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.24
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Library allow connect to lms EDUCANDUS.
107
+ test_files:
108
+ - spec/educandus_api_spec.rb
109
+ - spec/spec_helper.rb