magis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +2 -0
  3. data/Rakefile +2 -0
  4. data/bin/magis +83 -0
  5. data/boilerplate/plugin/Gemfile +4 -0
  6. data/boilerplate/plugin/Gemfile.lock +17 -0
  7. data/boilerplate/plugin/LICENSE.txt +22 -0
  8. data/boilerplate/plugin/README.md +29 -0
  9. data/boilerplate/plugin/Rakefile +2 -0
  10. data/boilerplate/plugin/lib/your_spi_plugin.rb +8 -0
  11. data/boilerplate/plugin/lib/your_spi_plugin/version.rb +3 -0
  12. data/boilerplate/plugin/pkg/your_spi_plugin-0.0.1.gem +0 -0
  13. data/boilerplate/plugin/pkg/your_spi_plugin-1.0.1.gem +0 -0
  14. data/boilerplate/plugin/pkg/your_spi_plugin2-1.0.1.gem +0 -0
  15. data/boilerplate/plugin/your_spi_plugin.gemspec +23 -0
  16. data/boilerplate/project/Gemfile +1 -0
  17. data/boilerplate/project/Gemfile.lock +129 -0
  18. data/boilerplate/project/assets/javascripts/application.coffee +9 -0
  19. data/boilerplate/project/assets/stylesheets/application.css +9 -0
  20. data/boilerplate/project/collections/documentation.yml +2 -0
  21. data/boilerplate/project/config.ru +3 -0
  22. data/boilerplate/project/config/app.yml +1 -0
  23. data/boilerplate/project/config/database.yml +1 -0
  24. data/boilerplate/project/config/facebook.yml +3 -0
  25. data/boilerplate/project/config/google.yml +3 -0
  26. data/boilerplate/project/config/twitter.yml +3 -0
  27. data/boilerplate/project/data/json/documentation.json +6 -0
  28. data/boilerplate/project/public/index.html +40 -0
  29. data/boilerplate/project/public/pages/authentication.html +14 -0
  30. data/boilerplate/project/public/pages/documentation.html +8 -0
  31. data/lib/magis.rb +72 -0
  32. data/lib/magis/auths/fb.rb +71 -0
  33. data/lib/magis/auths/google.rb +56 -0
  34. data/lib/magis/auths/twitter.rb +52 -0
  35. data/lib/magis/base.rb +108 -0
  36. data/lib/magis/collection.rb +47 -0
  37. data/lib/magis/collections.rb +80 -0
  38. data/lib/magis/version.rb +3 -0
  39. data/lib/magis/web.rb +138 -0
  40. data/magis/html/setup.html +82 -0
  41. data/magis/js/current_user.js +10 -0
  42. data/magis/js/finch.min.js +14 -0
  43. data/magis/js/main.js +112 -0
  44. data/magis/js/setup.js +16 -0
  45. metadata +409 -0
@@ -0,0 +1,14 @@
1
+ <template name="authentication">
2
+ <div class="container">
3
+ <h1>The Magis Project</h1>
4
+ <h3>Development A Better Way</h3>
5
+ </div>
6
+ <div class="col-sm-3">
7
+ <a href='/auth/google' class="btn btn-primary col-sm-12">Login with google</a>
8
+ <br />
9
+ <br />
10
+ <a href='/auth/facebook' class="btn btn-primary col-sm-12">Login with facebook</a>
11
+ <br>
12
+ <br />
13
+ <a href='/auth/twitter' class="btn btn-primary col-sm-12">Login with twitter</a></div>
14
+ </template>
@@ -0,0 +1,8 @@
1
+ <template name="documentation">
2
+ <div class="documentation">
3
+ {{#documentation }}
4
+ <div class="title"><h2>{{title}}</h2></div>
5
+ <div class="description">{{description}}</div>
6
+ {{/documentation }}
7
+ </div>
8
+ </template>
data/lib/magis.rb ADDED
@@ -0,0 +1,72 @@
1
+ require "magis/version"
2
+ require 'bundler'
3
+ Bundler.require(:default)
4
+
5
+ #######################
6
+ # FAYE SERVER
7
+ #######################
8
+ require 'faye'
9
+
10
+ #######################
11
+ # REQUIRE SINATRA
12
+ #######################
13
+ require 'sinatra'
14
+ require 'sinatra/asset_pipeline'
15
+
16
+ #######################
17
+ # REQUIRE SOCIAL AUTH
18
+ #######################
19
+ require 'omniauth'
20
+ require 'omniauth-facebook'
21
+ require 'omniauth-twitter'
22
+ require 'omniauth-google-oauth2'
23
+
24
+ #######################
25
+ # SOCIAL INTERACTIONS
26
+ #######################
27
+ require 'koala'
28
+
29
+ #######################
30
+ # SUPPORTING FILES
31
+ #######################
32
+ require 'yaml'
33
+
34
+ ##################################
35
+ # REQUIRE MONGO JSON INFLECTOR
36
+ ##################################
37
+ require 'mongo'
38
+ require 'json'
39
+ require 'active_support/inflector'
40
+
41
+ #################################
42
+ # REQUIRE BASE AND COLLECTIONS
43
+ #################################
44
+ require_relative 'magis/collections'
45
+
46
+ ###############################
47
+ # REQUIRE MAIN LIB
48
+ ###############################
49
+ require_relative 'magis/base'
50
+
51
+ ###############################
52
+ # REQUIRE MAIN LIB
53
+ ###############################
54
+ require_relative 'magis/auths/fb'
55
+ require_relative 'magis/auths/twitter'
56
+ require_relative 'magis/auths/google'
57
+
58
+ ###############################
59
+ # REQUIRE MAIN LIB
60
+ ###############################
61
+ require_relative 'magis/web'
62
+
63
+ require_relative 'magis/collection'
64
+
65
+ ###############################
66
+ # REQUIRE API FILES
67
+ ###############################
68
+ Dir[File.dirname(Magis.home_folder) + '/api/*.rb'].each do |file|
69
+ require file.gsub(".rb", "")
70
+ end
71
+
72
+ Magis.start
@@ -0,0 +1,71 @@
1
+ class FBTether
2
+ def self.store(user_params)
3
+ user_raw = user_params["extra"]["raw_info"]
4
+ user = Magis.db["users"].find(email: user_raw["email"]).first
5
+
6
+ user_info = Hash.new
7
+
8
+ if user_raw["email"]
9
+ user_info[:email] = user_raw["email"]
10
+ end
11
+
12
+ if user_raw["first_name"]
13
+ user_info[:firstName] = user_raw["first_name"]
14
+ end
15
+
16
+ if user_raw["last_name"]
17
+ user_info[:lastName] = user_raw["last_name"]
18
+ end
19
+
20
+ if user_raw["gender"]
21
+ user_info[:gender] = user_raw["gender"]
22
+ end
23
+
24
+
25
+ if user_params["info"]["image"]
26
+ user_info[:image] = user_params["info"]["image"]
27
+ end
28
+
29
+ if user_raw["id"]
30
+ user_info[:uid] = user_raw["id"]
31
+ end
32
+
33
+ if user_raw["locale"]
34
+ user_info[:locale] = user_raw["locale"]
35
+ end
36
+
37
+ if user_raw["timezone"]
38
+ user_info[:timezone] = user_raw["timezone"]
39
+ end
40
+
41
+ if user_params["credentials"]["token"]
42
+ user_info[:accessToken] = user_params["credentials"]["token"]
43
+ end
44
+
45
+ user_info[:provider] = :facebook
46
+
47
+ if user
48
+ Magis.db["users"].update({"_id" => user["_id"]}, {"$set" => user_info})
49
+ else
50
+ Magis.db["users"].insert(user_info)
51
+ user = Magis.db["users"].find(email: user_raw["email"]).first
52
+ end
53
+
54
+ user
55
+ end
56
+
57
+ def self.friends(user)
58
+ graph = Koala::Facebook::API.new(user["accessToken"])
59
+
60
+ friends = Magis.db["friends"].find(userId: user["_id"]).first
61
+ fb_friends = graph.get_connections("me", "friends")
62
+
63
+ if friends
64
+ friends_list = Magis.db["friends"].update({userId: user["_id"]}, {"$set" => {friends: fb_friends}})
65
+ else
66
+ Magis.db["friends"].insert({userId: user["_id"], friends: fb_friends})
67
+ end
68
+
69
+ Magis.db["friends"].find(userId: user["_id"]).first["friends"]
70
+ end
71
+ end
@@ -0,0 +1,56 @@
1
+ class GoogleTether
2
+ def self.store(user_params)
3
+ user_raw = user_params["extra"]["raw_info"]
4
+ user = Magis.db["users"].find(email: user_raw["email"], provider: user_params["provider"]).first
5
+
6
+ user_info = Hash.new
7
+
8
+ if user_raw["email"]
9
+ user_info[:email] = user_raw["email"]
10
+ end
11
+
12
+ if user_raw["first_name"]
13
+ user_info[:firstName] = user_raw["first_name"]
14
+ end
15
+
16
+ if user_raw["last_name"]
17
+ user_info[:lastName] = user_raw["last_name"]
18
+ end
19
+
20
+ if user_raw["gender"]
21
+ user_info[:gender] = user_raw["gender"]
22
+ end
23
+
24
+
25
+ if user_params["info"]["image"]
26
+ user_info[:image] = user_params["info"]["image"]
27
+ end
28
+
29
+ if user_raw["id"]
30
+ user_info[:uid] = user_raw["id"]
31
+ end
32
+
33
+ if user_raw["locale"]
34
+ user_info[:locale] = user_raw["locale"]
35
+ end
36
+
37
+ if user_raw["timezone"]
38
+ user_info[:timezone] = user_raw["timezone"]
39
+ end
40
+
41
+ if user_params["credentials"]["token"]
42
+ user_info[:accessToken] = user_params["credentials"]["token"]
43
+ end
44
+
45
+ user_info[:provider] = user_params["provider"]
46
+
47
+ if user
48
+ Magis.db["users"].update({"_id" => user["_id"]}, {"$set" => user_info})
49
+ else
50
+ Magis.db["users"].insert(user_info)
51
+ user = Magis.db["users"].find(email: user_raw["email"]).first
52
+ end
53
+
54
+ user
55
+ end
56
+ end
@@ -0,0 +1,52 @@
1
+ class TwitterTether
2
+ def self.store(user_params)
3
+ user_raw = user_params["extra"]["raw_info"]
4
+
5
+ user = Magis.db["users"].find(screenName: user_raw["screen_name"]).first
6
+
7
+ user_info = Hash.new
8
+
9
+ if user_raw["screen_name"]
10
+ user_info[:screenName] = user_raw["screen_name"]
11
+ end
12
+
13
+ if user_raw["name"]
14
+ user_info[:firstName] = user_raw["name"].split(" ").first
15
+ user_info[:lastName] = user_raw["name"].split(" ").last
16
+ end
17
+
18
+ if user_raw["gender"]
19
+ user_info[:gender] = user_raw["gender"]
20
+ end
21
+
22
+ if user_raw["id"]
23
+ user_info[:uid] = user_raw["id"]
24
+ end
25
+
26
+ if user_raw["profile_image_url_https"]
27
+ user_info[:image] = user_raw["profile_image_url_https"]
28
+ end
29
+
30
+ if user_raw["timezone"]
31
+ user_info[:timezone] = user_raw["timezone"]
32
+ end
33
+
34
+ if user_params["credentials"]["token"]
35
+ user_info[:token] = user_params["credentials"]["token"]
36
+ end
37
+
38
+ if user_params["credentials"]["secret"]
39
+ user_info[:token_secret] = user_params["credentials"]["secret"]
40
+ end
41
+
42
+ user_info[:provider] = :twitter
43
+
44
+ if user
45
+ Magis.db["users"].update({"_id" => user["_id"]}, {"$set" => user_info})
46
+ else
47
+ user = Magis.db["users"].insert(user_info)
48
+ end
49
+
50
+ user
51
+ end
52
+ end
data/lib/magis/base.rb ADDED
@@ -0,0 +1,108 @@
1
+ module Rack
2
+ class Request
3
+ def subdomains(tld_len=1) # we set tld_len to 1, use 2 for co.uk or similar
4
+ # cache the result so we only compute it once.
5
+ @env['rack.env.subdomains'] ||= lambda {
6
+ # check if the current host is an IP address, if so return an empty array
7
+ return [] if (host.nil? ||
8
+ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
9
+ host.split('.')[0...(1 - tld_len - 2)] # pull everything except the TLD
10
+ }.call
11
+ end
12
+ end
13
+ end
14
+
15
+ module Magis
16
+ include Mongo
17
+ @@home_folder = ""
18
+ def self.framework_path
19
+ @@local_path ||= File.expand_path("../../", File.dirname(__FILE__))
20
+ end
21
+ def self.file(filename)
22
+ local_path = self.framework_path
23
+ project_path = File.expand_path(self.home_folder)
24
+
25
+ file_contents = nil
26
+ full_file_name = project_path + filename
27
+
28
+ if File.file?(full_file_name)
29
+ file_contents = File.new(full_file_name).readlines
30
+ elsif File.file?(local_path + filename)
31
+ full_file_name = local_path + filename
32
+ file_contents = File.new(full_file_name).readlines
33
+ end
34
+
35
+ file_contents
36
+ end
37
+
38
+ def self.application
39
+ Api
40
+ end
41
+
42
+ def self.env
43
+ @@environment ||= ENV['RACK_ENV']
44
+ setup ||= @@environment == "setup"
45
+ production ||= @@environment == "production"
46
+ development ||= @@environment == "development"
47
+ test ||= @@environment == "test"
48
+ @environments ||= OpenStruct.new({
49
+ setup?: setup,
50
+ production?: production,
51
+ development?: development,
52
+ test?: test
53
+ })
54
+ @environments
55
+ end
56
+
57
+ def self.home_folder
58
+ Dir.pwd
59
+ end
60
+
61
+ def self.set_db
62
+ uri = ENV["DB_URI"] || Magis.load_configuration("database")["uri"]
63
+
64
+ if uri
65
+ client = MongoClient.from_uri(uri)
66
+ else
67
+ client = MongoClient.new
68
+ end
69
+
70
+ db_name = ENV["DB_NAME"] || Magis.load_configuration("database")["name"]
71
+
72
+ @@db = client[db_name]
73
+ end
74
+
75
+ def self.db
76
+ @@db
77
+ end
78
+
79
+ def self.load_collection(file)
80
+ if File.exist?(home_folder + "/collections/" + file + ".yml")
81
+ YAML.load_file(home_folder + "/collections/" + file + ".yml")
82
+ else
83
+ Hash.new
84
+ end
85
+ end
86
+
87
+ def self.load_configuration(file)
88
+ @@files ||= Hash.new
89
+ if File.exist?(home_folder + "/config/" + file + ".yml")
90
+ @@files[file] ||= YAML.load_file(home_folder + "/config/" + file + ".yml")
91
+ else
92
+ Hash.new
93
+ end
94
+ end
95
+
96
+ def self.omni_auth_config(type)
97
+ configuration = self.load_configuration(type)
98
+ configuration[:id] ||= nil
99
+ configuration[:secret] ||= nil
100
+
101
+ configuration
102
+ end
103
+
104
+ def self.start
105
+ Dir["/api/*.rb"].each {|file| require file }
106
+ self.set_db
107
+ end
108
+ end
@@ -0,0 +1,47 @@
1
+ class Collection
2
+ attr_accessor :resource, :request, :config, :current_user, :type
3
+
4
+ def initialize(current_user, params, request)
5
+ self.resource = params[:resource]
6
+ self.request = request
7
+ self.current_user = current_user
8
+
9
+ self.config = Magis.load_collection(resource)
10
+ self.type = self.config["type"]
11
+ end
12
+
13
+ def source
14
+ if self.config["type"] == "json"
15
+ JSON.parse( IO.read(self.config["source"]+".json") )
16
+ else
17
+ Magis.db[resource]
18
+ end
19
+ end
20
+
21
+ def is_public?
22
+ end
23
+
24
+ def is_accessable?
25
+ user_access = config["user_access"]
26
+ if user_access
27
+ id = user_access["id"]
28
+ type = user_access["type"]
29
+ end
30
+ puts "METHOD"
31
+ puts request.request_method
32
+ puts "METHOD END"
33
+ if config["public"][request.request_method] || [request.request_method]
34
+ true
35
+ else
36
+ false
37
+ end
38
+ end
39
+
40
+ def check_verb(method)
41
+ if (collection_config["public"] && method == "GET" ) || (collection_config["public"] && method == collection_config[method])
42
+ true
43
+ else
44
+ false
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,80 @@
1
+ class Api < Sinatra::Base
2
+
3
+ before "/api/collections/:resource" do
4
+ content_type 'application/json'
5
+ @collection = Collection.new(current_user, params, request)
6
+
7
+ unless @collection.is_public? || current_user
8
+ pass
9
+ end
10
+ end
11
+
12
+ post "/api/collections/:resource" do
13
+ resource = params[:data].to_hash
14
+ resource["userId"] = current_user["_id"]
15
+ resourceId = current_resource.insert(resource)
16
+ json = process_json current_resource.find("_id" => resourceId).to_a.first
17
+ json
18
+ end
19
+
20
+ get "/api/collections/:resource" do
21
+ resource_array = Array.new
22
+ if @collection.type == "json"
23
+ @collection.source.to_json
24
+ else
25
+ current_resource.find({userId: current_user["_id"]}).to_a.each do |resource|
26
+ resource_array.push(fix_id(resource))
27
+ end
28
+
29
+ resource_array.to_json
30
+ end
31
+ end
32
+
33
+ get "/api/collections/:resource/:id" do
34
+ oid = BSON::ObjectId.from_string(params[:id])
35
+ resource_array = Array.new
36
+
37
+ process_json current_resource.find({userId: current_user["_id"], "_id" => oid}).to_a.first
38
+ end
39
+
40
+ delete "/api/collections/:resource/:id" do
41
+ oid = BSON::ObjectId.from_string(params[:id])
42
+ json = process_json current_resource.remove({userId: current_user["_id"], "_id" => oid})
43
+ json
44
+ end
45
+
46
+ put "/api/collections/:resource/:id" do
47
+ oid = BSON::ObjectId.from_string(params[:id])
48
+ resource = params[:data].to_hash
49
+ json = process_json current_resource.update({"userId" => current_user["_id"], "_id" => oid}, {"$set" => resource})
50
+ json
51
+ end
52
+
53
+ def pass
54
+ halt [ 401, {error: "Not Found"}.to_json ]
55
+ end
56
+
57
+ def resource_name
58
+ params[:resource]
59
+ end
60
+
61
+ def current_resource
62
+ @collection.source
63
+ end
64
+
65
+ def fix_id(local_object)
66
+ local_object["_id"] = local_object["_id"].to_s
67
+ local_object
68
+ end
69
+ def process_json(local_object)
70
+ fix_id(local_object).to_json
71
+ end
72
+
73
+ def current_user
74
+ Magis.db["users"].find(_id: session[:user_id]).to_a.first
75
+ end
76
+
77
+ def faye_client
78
+ Faye::Client.new(request.base_url+'/faye')
79
+ end
80
+ end