social_snippet-registry_core 0.0.1

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.
Files changed (39) hide show
  1. checksums.yaml +15 -0
  2. data/.components +9 -0
  3. data/.gitignore +18 -0
  4. data/.travis.yml +26 -0
  5. data/Gemfile +17 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +31 -0
  8. data/Rakefile +15 -0
  9. data/config.ru +9 -0
  10. data/config/apps.rb +26 -0
  11. data/config/boot.rb +50 -0
  12. data/config/database.rb +45 -0
  13. data/lib/social_snippet/registry/webapi.rb +5 -0
  14. data/lib/social_snippet/registry/webapi/controllers/v0/repositories_controller.rb +37 -0
  15. data/lib/social_snippet/registry/webapi/controllers/v0/token_controller.rb +13 -0
  16. data/lib/social_snippet/registry/webapi/controllers/v0/user_controller.rb +49 -0
  17. data/lib/social_snippet/registry/webapi/helpers/repository_helper.rb +18 -0
  18. data/lib/social_snippet/registry/webapi/helpers/url_helper.rb +29 -0
  19. data/lib/social_snippet/registry/webapi/models/repository.rb +75 -0
  20. data/lib/social_snippet/registry/webapi/models/user_account.rb +30 -0
  21. data/lib/social_snippet/registry/webapi/webapi_base.rb +12 -0
  22. data/lib/social_snippet/registry/webapi/webapi_v0.rb +3 -0
  23. data/lib/social_snippet/registry_core.rb +8 -0
  24. data/lib/social_snippet/registry_core/common_helpers.rb +11 -0
  25. data/lib/social_snippet/registry_core/config_helpers.rb +95 -0
  26. data/lib/social_snippet/registry_core/fetcher.rb +3 -0
  27. data/lib/social_snippet/registry_core/fetcher/fetcher_base.rb +19 -0
  28. data/lib/social_snippet/registry_core/fetcher/github_fetcher.rb +69 -0
  29. data/lib/social_snippet/registry_core/version.rb +5 -0
  30. data/lib/social_snippet/registry_core/version_helpers.rb +21 -0
  31. data/social_snippet-registry_core.gemspec +36 -0
  32. data/spec/spec_helper.rb +9 -0
  33. data/spec/spec_helpers/database_cleaner_helper.rb +18 -0
  34. data/spec/spec_helpers/factory_girl_helper.rb +9 -0
  35. data/spec/spec_helpers/rack_test_helper.rb +18 -0
  36. data/spec/webapi/controllers/v0/repositories_controller_spec.rb +232 -0
  37. data/spec/webapi/helpers/url_helper_spec.rb +49 -0
  38. data/spec/webapi/models/repository_spec.rb +72 -0
  39. metadata +256 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODgwZTA3NTU5NjY5NjIyNTRlMGJlNTVhOTI1MGVhOWE2OTBkY2Q5NQ==
5
+ data.tar.gz: !binary |-
6
+ NGU0ZjVkYTcwODU4NDViNWYwOThkMWFkMGJjZmVjZTliY2U3NzNjOA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OWFjNDhiOWIxYjY0ZGU1ZDJhNTI4ZThlN2UzNDk2NDc1N2QzMDA3NDdiMDhk
10
+ OGZkN2Q3NDA2ZGQ3ZjdlYTYwYzBmNzU3N2MzMjNjOTQ3MTUwNzg0MDA2NDg5
11
+ YjcxZDgxMDgzNTZjMjliNTY4OGFkZTRkNDgzN2RkNTlmZTcyZjQ=
12
+ data.tar.gz: !binary |-
13
+ ZjAyZjA1NzUxN2JhMjU5NzQzMTkyMGNiNDYwZjE3Mjc3N2ZkZjU3MWNjNjdl
14
+ MmZjODhmZjA1YzQ0YTVjOTE2OWM3NTA5Y2FhNTQyOWFjOThjNzA1MmQxZmQx
15
+ YTExZDliMjk4ZWM5NTc2MmZkNzY2Y2ExYTViNzgyOGY0ZGM1YjQ=
data/.components ADDED
@@ -0,0 +1,9 @@
1
+ ---
2
+ :orm: mongoid
3
+ :test: rspec
4
+ :mock: none
5
+ :script: none
6
+ :renderer: slim
7
+ :stylesheet: none
8
+ :namespace: WebAPI
9
+ :migration_format: number
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /log/
11
+ /public/
12
+ *.bundle
13
+ *.so
14
+ *.o
15
+ *.a
16
+ mkmf.log
17
+ /vendor/bundle
18
+ *.bak
data/.travis.yml ADDED
@@ -0,0 +1,26 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - "2.1"
5
+ - "2.0"
6
+ - "1.9"
7
+
8
+ services:
9
+ - mongodb
10
+ - redis-server
11
+ - memcached
12
+
13
+ bundler_args: "--without production:debug --jobs 4 --retry 3"
14
+
15
+ sudo: false
16
+
17
+ script:
18
+ - bundle exec rake spec
19
+
20
+ deploy:
21
+ provider: rubygems
22
+ api_key:
23
+ master:
24
+ secure: "IJ++p1ozWwXqK83UZR8xAdzZR5XHVAr5uU2xKCIRsHYe+wi7c0SgOqA+j5VM9US+YTB3zT7gXdvTAajNMR5sPp6prbzFe4BD2G8RfrFAoPkN+GNzgSVbgXk8DQnmJVKDPleSaqo68AA244dGVPqHyBVi+rFFxc7jMtQPlttBJYE="
25
+ gem:
26
+ master: social_snippet-registry_core
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Distribute your app as a gem
4
+ gemspec
5
+
6
+ group :test do
7
+ gem "rspec"
8
+ gem "database_cleaner"
9
+ gem "factory_girl"
10
+ gem "rack-test"
11
+ end
12
+
13
+ group :debug do
14
+ gem "shotgun"
15
+ gem "pry-byebug"
16
+ end
17
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Hiroyuki Sano
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,31 @@
1
+ # SocialSnippet::RegistryCore
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'social_snippet-registry_core'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install social_snippet-registry_core
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/social_snippet-registry_core/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'padrino-core/cli/rake'
4
+ PadrinoTasks.use(:database)
5
+ PadrinoTasks.use(:mongomapper)
6
+ PadrinoTasks.init
7
+
8
+ require "rspec/core/rake_task"
9
+ RSpec::Core::RakeTask.new(:spec) do |t|
10
+ t.rspec_opts = [
11
+ "--format doc",
12
+ "--color",
13
+ ]
14
+ end
15
+
data/config.ru ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rackup
2
+ # encoding: utf-8
3
+
4
+ # This file can be used to start Padrino,
5
+ # just execute it from the command line.
6
+
7
+ require_relative "config/boot"
8
+
9
+ run Padrino.application
data/config/apps.rb ADDED
@@ -0,0 +1,26 @@
1
+ ##
2
+ # This file mounts each app in the Padrino project to a specified sub-uri.
3
+ # You can mount additional applications using any of these commands below:
4
+ #
5
+ # Padrino.mount('blog').to('/blog')
6
+ # Padrino.mount('blog', :app_class => 'BlogApp').to('/blog')
7
+ # Padrino.mount('blog', :app_file => 'path/to/blog/app.rb').to('/blog')
8
+ #
9
+ # You can also map apps to a specified host:
10
+ #
11
+ # Padrino.mount('Admin').host('admin.example.org')
12
+ # Padrino.mount('WebSite').host(/.*\.?example.org/)
13
+ # Padrino.mount('Foo').to('/foo').host('bar.example.org')
14
+ #
15
+ # Note 1: Mounted apps (by default) should be placed into the project root at '/app_name'.
16
+ # Note 2: If you use the host matching remember to respect the order of the rules.
17
+ #
18
+ # By default, this file mounts the primary app which was generated with this project.
19
+ # However, the mounted app can be modified as needed:
20
+ #
21
+ # Padrino.mount('AppName', :app_file => 'path/to/file', :app_class => 'BlogApp').to('/')
22
+ #
23
+
24
+ # Mounts the core application for this project
25
+ Padrino.mount("SocialSnippet::Registry::WebAPI::WebAPIv0").to("/v0")
26
+
data/config/boot.rb ADDED
@@ -0,0 +1,50 @@
1
+ # Defines our constants
2
+ RACK_ENV = ENV['RACK_ENV'] ||= 'development' unless defined?(RACK_ENV)
3
+ PADRINO_ROOT = File.expand_path('../..', __FILE__) unless defined?(PADRINO_ROOT)
4
+
5
+ require "bundler/setup"
6
+ require "social_snippet/registry_core"
7
+
8
+ ##
9
+ # ## Enable devel logging
10
+ #
11
+ # Padrino::Logger::Config[:development][:log_level] = :devel
12
+ # Padrino::Logger::Config[:development][:log_static] = true
13
+ #
14
+ # ## Configure your I18n
15
+ #
16
+ # I18n.default_locale = :en
17
+ # I18n.enforce_available_locales = false
18
+ #
19
+ # ## Configure your HTML5 data helpers
20
+ #
21
+ # Padrino::Helpers::TagHelpers::DATA_ATTRIBUTES.push(:dialog)
22
+ # text_field :foo, :dialog => true
23
+ # Generates: <input type="text" data-dialog="true" name="foo" />
24
+ #
25
+ # ## Add helpers to mailer
26
+ #
27
+ # Mail::Message.class_eval do
28
+ # include Padrino::Helpers::NumberHelpers
29
+ # include Padrino::Helpers::TranslationHelpers
30
+ # end
31
+
32
+ Padrino.configure_apps do
33
+ extend ::SocialSnippet::RegistryCore::ConfigHelpers
34
+ set :sspm_session, true
35
+ end
36
+
37
+ ##
38
+ # Add your before (RE)load hooks here
39
+ #
40
+ Padrino.before_load do
41
+ require "social_snippet/registry/webapi"
42
+ end
43
+
44
+ ##
45
+ # Add your after (RE)load hooks here
46
+ #
47
+ Padrino.after_load do
48
+ end
49
+
50
+ Padrino.load!
@@ -0,0 +1,45 @@
1
+ # Connection.new takes host and port.
2
+
3
+ host = 'localhost'
4
+ port = 27017
5
+
6
+ database_name = case Padrino.env
7
+ when :development then 'social_snippet_development'
8
+ when :production then 'social_snippet_production'
9
+ when :test then 'social_snippet_test'
10
+ end
11
+
12
+ # Use MONGO_URI if it's set as an environmental variable.
13
+ Mongoid::Config.sessions =
14
+ if ENV['MONGO_URI']
15
+ {default: {uri: ENV['MONGO_URI'] }}
16
+ else
17
+ {default: {hosts: ["#{host}:#{port}"], database: database_name}}
18
+ end
19
+
20
+ # If you want to use a YML file for config, use this instead:
21
+ #
22
+ # Mongoid.load!(File.join(Padrino.root, 'config', 'database.yml'), Padrino.env)
23
+ #
24
+ # And add a config/database.yml file like this:
25
+ # development:
26
+ # sessions:
27
+ # default:
28
+ # database: hoge2_development
29
+ # hosts:
30
+ # - localhost:27017
31
+ # production:
32
+ # sessions:
33
+ # default:
34
+ # database: hoge2_production
35
+ # hosts:
36
+ # - localhost:27017
37
+ # test:
38
+ # sessions:
39
+ # default:
40
+ # database: hoge2_test
41
+ # hosts:
42
+ # - localhost:27017
43
+ #
44
+ #
45
+ # More installation and setup notes are on http://mongoid.org/en/mongoid/docs/installation.html#configuration
@@ -0,0 +1,5 @@
1
+ module SocialSnippet::Registry
2
+ module WebAPI; end
3
+ end
4
+ require_relative "webapi/webapi_base"
5
+ require_relative "webapi/webapi_v0"
@@ -0,0 +1,37 @@
1
+ module SocialSnippet::Registry::WebAPI
2
+
3
+ WebAPIv0.controllers :repositories do
4
+
5
+ # GET /repositories
6
+ get :index do
7
+ if params[:q].nil?
8
+ Repository.all_repos.to_json
9
+ else
10
+ Repository.query(params[:q]).to_json
11
+ end
12
+ end
13
+
14
+ # GET /repositories/{repo-name}
15
+ get :index, :with => [:id] do
16
+ repo_model = Repository.find_by(:name => params[:id])
17
+ repo_model.to_object.to_json
18
+ end
19
+
20
+ # POST /repositories
21
+ #
22
+ # @param url [String]
23
+ post :index do
24
+ repo_url = normalize_url(params[:url])
25
+ fetcher = create_fetcher(repo_url)
26
+
27
+ info = fetcher.snippet_json(repo_url)
28
+ repo = Repository.create_by_snippet_json(info)
29
+ repo.url = repo_url
30
+ repo.save
31
+
32
+ "ok"
33
+ end
34
+
35
+ end # controllers
36
+
37
+ end # WebAPI
@@ -0,0 +1,13 @@
1
+ module SocialSnippet::Registry::WebAPI
2
+
3
+ WebAPIv0.controllers :token do
4
+
5
+ # GET /token
6
+ # returns csrf token
7
+ get :index do
8
+ csrf_token
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,49 @@
1
+ module SocialSnippet::Registry::WebAPI
2
+
3
+ WebAPIv0.controllers :user do
4
+
5
+ sspm_enable_session
6
+ sspm_enable_user_access_control
7
+
8
+ get "/repositories", :provides => :json do
9
+ halt 403 unless logged_in?
10
+ if current_account.github_repos.nil?
11
+ return {}.to_json
12
+ end
13
+ current_account.github_repos.map do |repo_name|
14
+ {
15
+ :name => repo_name,
16
+ }
17
+ end.to_json
18
+ end
19
+
20
+ patch "/repositories" do
21
+ client = ::Octokit::Client.new(:access_token => current_account.github_access_token)
22
+ client.auto_paginate = true
23
+ repos = SortedSet.new
24
+
25
+ repos.merge(
26
+ client.repos.select do |repo|
27
+ repo["permissions"]["admin"]
28
+ end.map do |repo|
29
+ repo["full_name"]
30
+ end
31
+ )
32
+ repos.merge(
33
+ client.orgs.map do |org|
34
+ client.org_repos(org.login, {:type => "all"}).select do |repo|
35
+ repo["permissions"]["admin"]
36
+ end.map do |repo|
37
+ repo["full_name"]
38
+ end
39
+ end.flatten
40
+ )
41
+
42
+ current_account.update_attributes :github_repos => repos.to_a
43
+
44
+ "ok"
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,18 @@
1
+ module SocialSnippet::Registry::WebAPI::Helpers
2
+
3
+ module RepositoryHelper
4
+
5
+ def create_fetcher(url)
6
+ uri = URI.parse(url)
7
+ if is_github?(uri)
8
+ ::SocialSnippet::RegistryCore::Fetcher::GitHubFetcher.new
9
+ else
10
+ raise "not supported"
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ ::SocialSnippet::Registry::WebAPI::WebAPIBase.helpers RepositoryHelper
17
+
18
+ end
@@ -0,0 +1,29 @@
1
+ module SocialSnippet::Registry::WebAPI::Helpers
2
+
3
+ module UrlHelper
4
+
5
+ def normalize_url(url)
6
+ uri = URI.parse(url)
7
+ if is_github?(uri)
8
+ uri.scheme = "git"
9
+ end
10
+ if is_github?(uri) && has_no_dot_git?(uri)
11
+ uri.path += ".git"
12
+ end
13
+ uri.to_s
14
+ end
15
+
16
+ def is_github?(uri)
17
+ uri.host === "github.com" && /^\// === uri.path
18
+ end
19
+
20
+ def has_no_dot_git?(uri)
21
+ not /\.git$/ === uri.path
22
+ end
23
+
24
+ end
25
+
26
+ ::SocialSnippet::Registry::WebAPI::WebAPIBase.helpers UrlHelper
27
+
28
+ end
29
+