epitech-api 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 269374dfa8ca04aafb52fe4365b804e097e59bc6
4
+ data.tar.gz: cb60f0bdb4938fc17ed74fc59ecaf6fe7f4c0a6e
5
+ SHA512:
6
+ metadata.gz: 24f688fdfc5b8dfd365fa8bd589d38a95adeca13869090e8ade2fb964afc2ef5f368f6fba1d7145eeb527e17a537c9c24f9aa27d65880c41bb81c55b509e3516
7
+ data.tar.gz: 513d08f5f63e318216db2bf1e25ee76bac6c6bd1eedec8882b69880e0b6f470a1c716bb59c6ff30204fa3e03f3558ede5a78c5629fb32e63877b20675351080b
@@ -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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in epitech-api.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 antoin_m
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,29 @@
1
+ # Epitech::Api
2
+
3
+ This gem can be used to call paysdu42.fr's API through Ruby.
4
+ You can get informations about all Ionis users.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'epitech-api'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install epitech-api
19
+
20
+ ## Usage
21
+
22
+ require 'epitech-api'
23
+
24
+ c = Epitech::Api::Connection.new('exampl_e', 'z9VH52jb')
25
+ if c.is_login? 'antoin_m'
26
+ puts "Hello, I'm #{c.get_name('antoin_m')}"
27
+ end
28
+
29
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require_relative 'lib/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "epitech-api"
8
+ spec.version = Epitech::Api::VERSION
9
+ spec.authors = ["antoin_m"]
10
+ spec.email = ["michel1.antoine@epitech.eu"]
11
+ spec.summary = %q{A simple gem to use Ionis' API.}
12
+ spec.description = %q{This gem can be used to call paysdu42.fr's API through Ruby. You can get informations about all Ionis users.}
13
+ spec.homepage = "https://github.com/antoin-m/epitech-api"
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_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,19 @@
1
+ # API error's namespace
2
+ module Epitech::Api::Errors
3
+
4
+ # Top of the error tree
5
+ class Epitech::Api::Errors::Base < RuntimeError
6
+ end
7
+
8
+ # Error thrown when a parameter is missing in the request
9
+ class Epitech::Api::Errors::MissingParameters < Epitech::Api::Errors::Base
10
+ end
11
+
12
+ # Error thrown when the request ask for an unknown action
13
+ class Epitech::Api::Errors::UnknownAction < Epitech::Api::Errors::Base
14
+ end
15
+
16
+ # Error thrown when using a bad login/password combination
17
+ class Epitech::Api::Errors::AuthFail < Epitech::Api::Errors::Base
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ module Epitech::Api::CheckApi
2
+ # Check for a good login/password combination (those given to Epitech::Api::Connection.new)
3
+ # @return [Boolean] True (Raise a Epitech::Api::Errors::AuthFail otherwise)
4
+ def check_auth
5
+ call_api(action: 'login')
6
+ true
7
+ end
8
+
9
+ # Test if the parameter is an existing ionis login
10
+ # @param login [String] The login to test
11
+ def is_login?(login)
12
+ json = call_api(action: 'is_login', login: login)
13
+ return false if json['state'] != 'OK'
14
+ true
15
+ end
16
+
17
+ # Test if the parameters are an existing login / PPP combination
18
+ # @param login [String] Login to test
19
+ # @param pwd [String] Password to test
20
+ # @return [Boolean]
21
+ def check_password(login, pwd)
22
+ json = call_api(action: 'check_password', login: login, password: pwd)
23
+ return false if json['state'] != 'OK'
24
+ true
25
+ end
26
+ end
@@ -0,0 +1,41 @@
1
+ require_relative 'check_api'
2
+ require_relative 'info_api'
3
+ require_relative 'search_api'
4
+
5
+ # Class provided to maintain a connection to the API
6
+ class Epitech::Api::Connection
7
+ include Epitech::Api
8
+
9
+ include Epitech::Api::CheckApi
10
+ include Epitech::Api::InfoApi
11
+ include Epitech::Api::SearchApi
12
+
13
+ # Constructor
14
+ # @param login [String] Your ionis login
15
+ # @param pwd [String] Your ionis password
16
+ # @return [Boolean] True if the connection was successful (Raise a Epitech::Api::Errors::AuthFail otherwise)
17
+ def initialize(login, pwd)
18
+ @login = login
19
+ @pwd = pwd
20
+ connect!
21
+ end
22
+
23
+ # Overloading of Epitech::Api::call_api, will use instance variables
24
+ # for auth_login and auth_password
25
+ def call_api(opts={})
26
+ opts[:auth_login] ||= @login
27
+ opts[:auth_password] ||= @pwd
28
+ super(opts)
29
+ end
30
+
31
+ # Try to connect to the API with the instance variables
32
+ def connect!
33
+ @connected = check_auth
34
+ end
35
+
36
+ # @return True if the connection is established, false otherwise
37
+ def is_connected?
38
+ @connected
39
+ end
40
+
41
+ end
@@ -0,0 +1,26 @@
1
+ module Epitech::Api::InfoApi
2
+
3
+ # Get a Ionis login from a UID
4
+ # @param uid [String, Integer] The UID to look for
5
+ # @return [String] The corresponding login
6
+ def get_login_from_uid(uid)
7
+ call_api(action: 'get_login_from_uid', uid: uid)['login']
8
+ end
9
+
10
+ %w(uid name group school promo city plan phone).each do |request|
11
+ class_eval %Q(def get_#{request}(login)
12
+ call_api(action: "get_#{request}", login: login)['#{request}']
13
+ end)
14
+ end
15
+
16
+ # Get all the infos about a Ionis login
17
+ # Each information can be retrieved separately through the get_xxx method
18
+ # example:
19
+ # connection.get_name('exampl_e')
20
+ # connection.get_uid('exampl_e')
21
+ # @param login [String] The login to look for
22
+ # @return [Hash] The corresponding infos
23
+ def get_infos(login)
24
+ call_api(action: 'get_infos', login: login)
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ module Epitech::Api::SearchApi
2
+
3
+ # Used to search across the Ionis users
4
+ # @param query [String] First/Last Name or Login (or a part of it)
5
+ # of who you are looking for.
6
+ # @return [Array] An Array of logins
7
+ def search(query)
8
+ call_api(action: 'search', query: query)
9
+ end
10
+
11
+ # Used to search logins across the Ionis users through 3 criterias :
12
+ # * Schoolname
13
+ # * Promo
14
+ # * City
15
+ # @param opts [Hash] A Hash that should contain at least one of the 3 criterias
16
+ # @return [Array] An Array of logins
17
+ def get_logins(opts={})
18
+ call_api(action: 'get_logins', school: opts[:school], promo: opts[:promo], city: opts[:city])
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ module Epitech
2
+ module Api
3
+ # Gem version
4
+ VERSION = "1.0.0"
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ require 'version'
2
+ require 'open-uri'
3
+ require 'json'
4
+
5
+ # Gem's global namespace
6
+ module Epitech
7
+ module Api
8
+ # JSON API URL
9
+ BASE_URL = 'http://ws.paysdu42.fr/JSON/?'
10
+ end
11
+ end
12
+
13
+ require_relative 'utils'
14
+ require_relative 'api/connection'
@@ -0,0 +1,42 @@
1
+ require_relative './Exceptions/errors'
2
+
3
+ module Epitech::Api
4
+
5
+ # Used to call the webservice
6
+ # @param opts [Hash] Hash used as URL params, formatted by {#hash_to_req}
7
+ # @return What the Web API is supposed to return
8
+ def call_api(opts={})
9
+ tmp = open(URI(URI.escape(BASE_URL + hash_to_req(opts)))).read
10
+ tmp = tmp.split("\n").drop(1).join("\n") while tmp.start_with?(%Q(SSH connection failed.))
11
+ json = JSON.parse(tmp)
12
+ raise get_error_class(json['error']) if json['error'] != 'none'
13
+ json['result']
14
+ end
15
+
16
+ private
17
+
18
+ def get_error_class(str)
19
+ to_class "Epitech::Api::Errors::#{to_camelcase(str)}"
20
+ end
21
+
22
+ def to_camelcase(str)
23
+ str.split('_').collect(&:capitalize).join
24
+ end
25
+
26
+ def to_class(str)
27
+ Object.const_get str
28
+ end
29
+
30
+ # Transform a Hash to Web API URL params
31
+ # @param hash [Hash]
32
+ # @return [String] formatted from 'hash' as follow:
33
+ # { action: 'connect', login: 'exampl_e' } becomes
34
+ # "action=connect&login=exampl_e"
35
+ def hash_to_req(hash)
36
+ req = ''
37
+ hash.each_pair do |key, val|
38
+ req << key.to_s << '=' << val.to_s << '&' unless val.nil?
39
+ end
40
+ req.chop
41
+ end
42
+ end
@@ -0,0 +1,6 @@
1
+ module Epitech
2
+ module Api
3
+ # Gem version
4
+ VERSION = "1.0.0"
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: epitech-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - antoin_m
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-10 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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
+ description: This gem can be used to call paysdu42.fr's API through Ruby. You can
42
+ get informations about all Ionis users.
43
+ email:
44
+ - michel1.antoine@epitech.eu
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - epitech-api.gemspec
55
+ - lib/Exceptions/errors.rb
56
+ - lib/api/check_api.rb
57
+ - lib/api/connection.rb
58
+ - lib/api/info_api.rb
59
+ - lib/api/search_api.rb
60
+ - lib/api/version.rb
61
+ - lib/epitech-api.rb
62
+ - lib/utils.rb
63
+ - lib/version.rb
64
+ homepage: https://github.com/antoin-m/epitech-api
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.0.14
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: A simple gem to use Ionis' API.
88
+ test_files: []
89
+ has_rdoc: