fortytwo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b878dfe76a61b2f6eb865f7d56985adf47daeec9
4
+ data.tar.gz: 2559a245fc7928b2c2f613137c425e571a2161b4
5
+ SHA512:
6
+ metadata.gz: 5d54bc966a09181071cfc799ca1c063d8ae6ad9615d7222709cb7ba1e0bf108b05286a542f552ddeda209d51e7f77153f6f52422d6f92cfe879e8e4d9cd12fee
7
+ data.tar.gz: 77d2daf6388d644d1e64953cc5d48de0e9de4b1188e9e528bd36605ddcda8fcf6c189942c4df571254a7dd053f2486ad585b0d1fe402d9ae8bc58c4158209921
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ ._DS_Store
2
+ .DS_Store
3
+ *.gem
4
+ *.rbc
5
+ .bundle
6
+ .config
7
+ .idea
8
+ .yardoc
9
+ Gemfile.lock
10
+ InstalledFiles
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ .ruby-env
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir["tasks/**/*.rake"].each do |file|
4
+ load(file)
5
+ end
data/fortytwo.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fortytwo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'fortytwo'
8
+ spec.version = FortyTwo::VERSION
9
+ spec.authors = ['Matias Fernandez']
10
+ spec.email = ['matiasfmolinari@gmail.com']
11
+ spec.summary = 'Ruby client library for the 42 API'
12
+ spec.description = 'Provides an easy way to interact with the 42 API in any kind of application'
13
+ spec.homepage = 'https://github.com/MatiasFMolinari/fortytwo'
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.required_ruby_version = '2.0.0'
22
+
23
+ spec.add_development_dependency 'rack', '1.6.4'
24
+ spec.add_development_dependency 'bundler', '~> 1.5'
25
+ spec.add_development_dependency 'rake', '~> 10.0', '>= 10.0.0'
26
+ spec.add_development_dependency 'pry', '~> 0.9', '>= 0.9.0'
27
+ spec.add_development_dependency 'yard', '~> 0.8'
28
+
29
+ spec.add_runtime_dependency 'oauth2', '~> 1.2'
30
+ end
data/lib/fortytwo.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'fortytwo/version'
2
+ require 'fortytwo/client'
3
+
4
+ module FortyTwo
5
+ def self.client
6
+ @client ||= FortyTwo::Client.new
7
+ end
8
+ end
@@ -0,0 +1,72 @@
1
+ require 'oauth2'
2
+
3
+ require 'fortytwo/configuration'
4
+ # require 'fortytwo/error'
5
+ require 'fortytwo/endpoints/user'
6
+
7
+ module FortyTwo
8
+ class Client
9
+ API_HOST = 'https://api.intra.42.fr'
10
+ REQUEST_CLASSES = [FortyTwo::Endpoint::User]
11
+
12
+ attr_reader :configuration
13
+
14
+ def initialize(options = nil)
15
+ @configuration = nil
16
+ define_request_methods
17
+
18
+ unless options.nil?
19
+ @configuration = Configuration.new(options)
20
+ check_api_keys
21
+ end
22
+ end
23
+
24
+ def configure
25
+ # raise Error::AlreadyConfigured unless @configuration.nil?
26
+
27
+ @configuration = Configuration.new
28
+ yield(@configuration)
29
+ check_api_keys
30
+ end
31
+
32
+ def check_api_keys
33
+ if configuration.nil? || configuration.invalid?
34
+ @configuration = nil
35
+ # raise Error::MissingAPIKeys
36
+ else
37
+ @configuration.freeze
38
+ end
39
+ end
40
+
41
+ def token
42
+ return @token if instance_variable_defined?(:@token)
43
+
44
+ check_api_keys
45
+ api_key = @configuration.api_key
46
+ api_secret = @configuration.api_secret
47
+ client = OAuth2::Client.new(api_key, api_secret, site: API_HOST)
48
+ client.client_credentials.get_token
49
+ end
50
+
51
+ private
52
+
53
+ def define_request_methods
54
+ REQUEST_CLASSES.each do |request_class|
55
+ endpoint_instance = request_class.new(self)
56
+ create_methods_from_instance(endpoint_instance)
57
+ end
58
+ end
59
+
60
+ def create_methods_from_instance(instance)
61
+ instance.public_methods(false).each do |method_name|
62
+ add_method(instance, method_name)
63
+ end
64
+ end
65
+
66
+ def add_method(instance, method_name)
67
+ define_singleton_method(method_name) do |*args|
68
+ instance.public_send(method_name, *args)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,19 @@
1
+ module FortyTwo
2
+ class Configuration
3
+ AUTH_KEYS = [:api_key, :api_secret]
4
+
5
+ attr_accessor *AUTH_KEYS
6
+
7
+ def initialize(config_hash = nil)
8
+ if config_hash.is_a?(Hash)
9
+ config_hash.each do |config_name, config_value|
10
+ self.send("#{config_name}=", config_value)
11
+ end
12
+ end
13
+ end
14
+
15
+ def invalid?
16
+ AUTH_KEYS.any? { |key| send(key).nil? || send(key).empty? }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ require 'fortytwo/responses/user'
2
+
3
+ module FortyTwo
4
+ module Endpoint
5
+ class User
6
+ PATH = '/v2/users'
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ def user(username, params = {})
13
+ Response::User.new(user_request(username, params))
14
+ end
15
+
16
+ private
17
+
18
+ def user_request(username, params = {})
19
+ @client.token.get("#{PATH}/#{username}", params: params).parsed
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,67 @@
1
+ # module FortyTwo
2
+ # module Error
3
+
4
+ # # Not sure if this will work...
5
+ # class ResponseValidator
6
+ # def validate(response)
7
+ # return if successful_response?(response)
8
+ # raise error_from_response(response)
9
+ # end
10
+
11
+ # def successful_response?(response)
12
+ # (200..399).include?(response.status)
13
+ # end
14
+
15
+ # def error_from_response(response)
16
+ # body = JSON.parse(response.body)
17
+ # klass = error_classes[body['error']['id']]
18
+ # klass.new(body['error']['text'], body['error'])
19
+ # end
20
+
21
+ # def error_classes
22
+ # @@error_classes ||= Hash.new do |hash, key|
23
+ # class_name = key.split('_').map(&:capitalize).join('').gsub('Oauth', 'OAuth')
24
+ # hash[key] = FortyTwo::Error.const_get(class_name)
25
+ # end
26
+ # end
27
+ # end
28
+
29
+ # def self.check_for_error(response)
30
+ # @response_validator ||= ResponseValidator.new
31
+ # @response_validator.validate(response)
32
+ # end
33
+
34
+ # class Base < StandardError
35
+ # def initialize(msg, error = nil)
36
+ # super(msg)
37
+ # end
38
+ # end
39
+
40
+ # class AlreadyConfigured < Base
41
+ # def initialize(msg = 'Gem cannot be reconfigured. Initialize a new ' +
42
+ # 'instance of FortyTwo::Client', error = nil)
43
+ # super
44
+ # end
45
+ # end
46
+
47
+ # class MissingAPIKeys < Base
48
+ # def initialize(msg = "You're missing an API key", error = nil)
49
+ # super
50
+ # end
51
+ # end
52
+
53
+ # # Not sure this will work.
54
+ # class InvalidParameter < Base
55
+ # attr_reader :text, :field
56
+
57
+ # def initialize(msg = 'One or more parameters were invalid', error = nil)
58
+ # unless error.nil?
59
+ # @text = error['text']
60
+ # @field = error['field']
61
+ # msg = msg + ': ' + @field
62
+ # end
63
+ # super(msg, error)
64
+ # end
65
+ # end
66
+ # end
67
+ # end
@@ -0,0 +1,22 @@
1
+ module FortyTwo
2
+ module Response
3
+ class Base
4
+ def initialize(json)
5
+ return if json.nil?
6
+
7
+ json.each do |key, value|
8
+ key = key.chomp('?') if key.end_with?('?')
9
+ instance_variable_set("@#{key}", value)
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def parse(json, klass)
16
+ return json.map { |j| klass.new(j) } if json.is_a?(Array)
17
+ return klass.new(json) if json
18
+ nil
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ require 'fortytwo/responses/base'
2
+ require 'fortytwo/responses/models/language'
3
+
4
+ module FortyTwo
5
+ module Response
6
+ module Model
7
+ class Campus < Response::Base
8
+ attr_reader :id, :name, :time_zone, :language, :user_count, :vogsphere_id
9
+
10
+ def initialize(json)
11
+ super(json)
12
+
13
+ @language = parse(@language, Language)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ require 'fortytwo/responses/base'
2
+
3
+ module FortyTwo
4
+ module Response
5
+ module Model
6
+ class Cursus < Response::Base
7
+ attr_reader :id, :created_at, :name, :slug
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ require 'fortytwo/responses/base'
2
+ require 'fortytwo/responses/models/skill'
3
+ require 'fortytwo/responses/models/cursus'
4
+
5
+
6
+ module FortyTwo
7
+ module Response
8
+ module Model
9
+ class CursusUser < Response::Base
10
+ attr_reader :id, :begin_at, :end_at, :grade, :level, :skills,
11
+ :cursus_id, :cursus
12
+
13
+ def initialize(json)
14
+ super(json)
15
+
16
+ @skills = parse(@cursus_users, Skill)
17
+ @cursus = parse(@cursus, Cursus)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ require 'fortytwo/responses/base'
2
+
3
+ module FortyTwo
4
+ module Response
5
+ module Model
6
+ class ExpertiseUser < Response::Base
7
+ attr_reader :id, :expertise_id, :interested, :value, :contact_me, :created_at, :user_id
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'fortytwo/responses/base'
2
+
3
+ module FortyTwo
4
+ module Response
5
+ module Model
6
+ class Language < Response::Base
7
+ attr_reader :id, :name, :identifier, :created_at, :updated_at
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'fortytwo/responses/base'
2
+
3
+ module FortyTwo
4
+ module Response
5
+ module Model
6
+ class Project < Response::Base
7
+ attr_reader :id, :name, :slug
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ require 'fortytwo/responses/base'
2
+ require 'fortytwo/responses/models/project'
3
+
4
+ module FortyTwo
5
+ module Response
6
+ module Model
7
+ class ProjectUser < Response::Base
8
+ attr_reader :id, :occurence, :final_mark, :status,
9
+ :current_team_id, :project, :cursus_ids
10
+
11
+ def initialize(json)
12
+ super(json)
13
+
14
+ @project = parse(@project, Project)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ require 'fortytwo/responses/base'
2
+
3
+ module FortyTwo
4
+ module Response
5
+ module Model
6
+ class Skill < Response::Base
7
+ attr_reader :id, :name, :level
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ require 'fortytwo/responses/base'
2
+ require 'fortytwo/responses/models/cursus_user'
3
+ require 'fortytwo/responses/models/project_user'
4
+ require 'fortytwo/responses/models/expertise_user'
5
+ require 'fortytwo/responses/models/campus'
6
+
7
+ module FortyTwo
8
+ module Response
9
+ module Model
10
+ class User < Response::Base
11
+ attr_reader :id, :email, :login, :first_name, :last_name, :url, :phone,
12
+ :displayname, :image_url, :correction_point, :staff,
13
+ :pool_month, :pool_year, :location, :wallet, :cursus_users,
14
+ :projects_users, :expertises_users, :campus
15
+
16
+ def initialize(json)
17
+ super(json)
18
+
19
+ @cursus_users = parse(@cursus_users, CursusUser)
20
+ @projects_users = parse(@projects_users, ProjectUser)
21
+ @expertises_users = parse(@expertises_users, ExpertiseUser)
22
+ @campus = parse(@campus, Campus)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ require 'fortytwo/responses/base'
2
+ require 'fortytwo/responses/models/user'
3
+
4
+ module FortyTwo
5
+ module Response
6
+ class User < Base
7
+ attr_reader :user
8
+
9
+ def initialize(json)
10
+ @user = parse(json, Model::User)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module FortyTwo
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,4 @@
1
+ desc "Open a pry session preloaded with this library"
2
+ task :console do
3
+ sh "pry --gem"
4
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fortytwo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matias Fernandez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.4
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.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 10.0.0
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '10.0'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 10.0.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: pry
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.9'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 0.9.0
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '0.9'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 0.9.0
81
+ - !ruby/object:Gem::Dependency
82
+ name: yard
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '0.8'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '0.8'
95
+ - !ruby/object:Gem::Dependency
96
+ name: oauth2
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '1.2'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '1.2'
109
+ description: Provides an easy way to interact with the 42 API in any kind of application
110
+ email:
111
+ - matiasfmolinari@gmail.com
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files: []
115
+ files:
116
+ - ".gitignore"
117
+ - ".ruby-version"
118
+ - Gemfile
119
+ - README.md
120
+ - Rakefile
121
+ - fortytwo.gemspec
122
+ - lib/fortytwo.rb
123
+ - lib/fortytwo/client.rb
124
+ - lib/fortytwo/configuration.rb
125
+ - lib/fortytwo/endpoints/user.rb
126
+ - lib/fortytwo/error.rb
127
+ - lib/fortytwo/responses/base.rb
128
+ - lib/fortytwo/responses/models/campus.rb
129
+ - lib/fortytwo/responses/models/cursus.rb
130
+ - lib/fortytwo/responses/models/cursus_user.rb
131
+ - lib/fortytwo/responses/models/expertise_user.rb
132
+ - lib/fortytwo/responses/models/language.rb
133
+ - lib/fortytwo/responses/models/project.rb
134
+ - lib/fortytwo/responses/models/project_user.rb
135
+ - lib/fortytwo/responses/models/skill.rb
136
+ - lib/fortytwo/responses/models/user.rb
137
+ - lib/fortytwo/responses/user.rb
138
+ - lib/fortytwo/version.rb
139
+ - tasks/console.rake
140
+ homepage: https://github.com/MatiasFMolinari/fortytwo
141
+ licenses:
142
+ - MIT
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '='
151
+ - !ruby/object:Gem::Version
152
+ version: 2.0.0
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.0.14.1
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Ruby client library for the 42 API
164
+ test_files: []