empresaula-api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +14 -0
- data/Guardfile +5 -0
- data/Rakefile +10 -0
- data/empresaula-api.gemspec +22 -0
- data/lib/empresaula-api/config.rb +27 -0
- data/lib/empresaula-api/controller/authentication.rb +30 -0
- data/lib/empresaula-api/controller.rb +1 -0
- data/lib/empresaula-api/resource/base.rb +11 -0
- data/lib/empresaula-api/resource/user.rb +13 -0
- data/lib/empresaula-api/resource.rb +1 -0
- data/lib/empresaula-api/test_helpers/user.rb +27 -0
- data/lib/empresaula-api/test_helpers.rb +1 -0
- data/lib/empresaula-api/version.rb +5 -0
- data/lib/empresaula-api.rb +9 -0
- data/test/empresaula-api/config_test.rb +25 -0
- data/test/empresaula-api/controller/authentication_test.rb +34 -0
- data/test/empresaula-api/resource/user_test.rb +26 -0
- data/test/empresaula-api/test_helpers/user_test.rb +22 -0
- data/test/test_helper.rb +3 -0
- metadata +115 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use 1.9.3@empresaula-api
|
data/Gemfile
ADDED
data/Guardfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/empresaula-api/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Codegram"]
|
6
|
+
gem.email = ["info@codegram.com"]
|
7
|
+
gem.description = %q{Ruby wrapper for interacting with Empresaula's API}
|
8
|
+
gem.summary = %q{Ruby wrapper for interacting with Empresaula's API}
|
9
|
+
|
10
|
+
gem.add_runtime_dependency 'activeresource', '>= 3.0'
|
11
|
+
|
12
|
+
gem.add_development_dependency 'turn'
|
13
|
+
gem.add_development_dependency 'minitest'
|
14
|
+
gem.add_development_dependency 'mocha'
|
15
|
+
|
16
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
gem.files = `git ls-files`.split("\n")
|
18
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
gem.name = "empresaula-api"
|
20
|
+
gem.require_paths = ['lib']
|
21
|
+
gem.version = Empresaula::API::VERSION
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'resource'
|
2
|
+
|
3
|
+
module Empresaula
|
4
|
+
module API
|
5
|
+
def self.config
|
6
|
+
@config ||= Config.new
|
7
|
+
yield(@config) if block_given?
|
8
|
+
@config
|
9
|
+
end
|
10
|
+
|
11
|
+
class Config
|
12
|
+
attr_reader :host
|
13
|
+
|
14
|
+
def host=(host)
|
15
|
+
@host = host
|
16
|
+
Resource::Base.site = "http://#{@host}/api"
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_reader :secret_token
|
20
|
+
|
21
|
+
def secret_token=(token)
|
22
|
+
@secret_token = token
|
23
|
+
Resource::Base.headers['X-SecretToken'] = token
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative '../resource/user'
|
2
|
+
|
3
|
+
module Empresaula
|
4
|
+
module API
|
5
|
+
module Controller
|
6
|
+
module Authentication
|
7
|
+
def self.included(base)
|
8
|
+
base.class_eval do
|
9
|
+
helper_method :current_user
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Sets the current user
|
14
|
+
#
|
15
|
+
def current_user
|
16
|
+
@current_user ||= find_from_cookies
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def find_from_cookies
|
22
|
+
if cookies[:empresaula_session].present?
|
23
|
+
Empresaula::API::Resource::User.find_by_session_id(
|
24
|
+
cookies[:empresaula_session])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'controller/authentication'
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'resource/user'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'empresaula-api/resource/user'
|
2
|
+
require 'active_resource'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Empresaula
|
6
|
+
module API
|
7
|
+
module TestHelpers
|
8
|
+
module User
|
9
|
+
def mock_current_user(user)
|
10
|
+
cookies = Capybara.current_session.driver.browser.current_session.instance_variable_get(:@rack_mock_session).cookie_jar
|
11
|
+
cookies[:empresaula_session] = 'qwertyuiop123456789'
|
12
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
13
|
+
mock.get "/api/users/qwertyuiop123456789.json", {},
|
14
|
+
user.to_json
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def mock_user(user)
|
19
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
20
|
+
mock.get "/api/users/#{user[:id]}.json", {},
|
21
|
+
user.to_json
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'test_helpers/user'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
require 'empresaula-api/config'
|
3
|
+
|
4
|
+
module Empresaula
|
5
|
+
describe "config" do
|
6
|
+
describe "#host=" do
|
7
|
+
it "sets the resource default site" do
|
8
|
+
API.config do |config|
|
9
|
+
config.host = 'localhost:3000'
|
10
|
+
end
|
11
|
+
API.config.host.must_equal 'localhost:3000'
|
12
|
+
API::Resource::Base.site.to_s.must_equal 'http://localhost:3000/api'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
describe "#secret_token=" do
|
16
|
+
it "sets a secret token" do
|
17
|
+
API.config do |config|
|
18
|
+
config.secret_token = 'secretOMG'
|
19
|
+
end
|
20
|
+
API.config.secret_token.must_equal 'secretOMG'
|
21
|
+
API::Resource::Base.headers["X-SecretToken"].must_equal 'secretOMG'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
require 'empresaula-api/controller/authentication'
|
3
|
+
require 'mocha'
|
4
|
+
|
5
|
+
module Empresaula::API::Controller
|
6
|
+
describe Authentication do
|
7
|
+
subject do
|
8
|
+
Class.new do
|
9
|
+
def self.helper_method(*args); end;
|
10
|
+
include Authentication
|
11
|
+
def cookies
|
12
|
+
@cookies ||= {}
|
13
|
+
end
|
14
|
+
end.new
|
15
|
+
end
|
16
|
+
before do
|
17
|
+
subject.cookies[:empresaula_session] = empresaula_session
|
18
|
+
end
|
19
|
+
let(:empresaula_session) do
|
20
|
+
stub(present?: false)
|
21
|
+
end
|
22
|
+
describe "#current_user" do
|
23
|
+
it "returns nil if no cookie set" do
|
24
|
+
subject.current_user.must_equal nil
|
25
|
+
end
|
26
|
+
it "returns an user if a cookie is found" do
|
27
|
+
empresaula_session.stubs(present?: true)
|
28
|
+
Empresaula::API::Resource::User.expects(:find_by_session_id).
|
29
|
+
with(empresaula_session)
|
30
|
+
subject.current_user
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
require 'empresaula-api/resource/user'
|
3
|
+
|
4
|
+
module Empresaula::API::Resource
|
5
|
+
describe User do
|
6
|
+
before do
|
7
|
+
User.site = "http://fakehost/api"
|
8
|
+
end
|
9
|
+
describe ".find_by_session_id" do
|
10
|
+
let(:user) do
|
11
|
+
{user: {name: "James", id: 1}}
|
12
|
+
end
|
13
|
+
before do
|
14
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
15
|
+
mock.get '/api/users/session_key.json', {},
|
16
|
+
user.to_json
|
17
|
+
end
|
18
|
+
end
|
19
|
+
it "builds a new user given a session id" do
|
20
|
+
user = User.find_by_session_id("session_key")
|
21
|
+
user.id.must_equal 1
|
22
|
+
user.name.must_equal "James"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
require 'empresaula-api/test_helpers/user'
|
3
|
+
|
4
|
+
module Empresaula::API::TestHelpers
|
5
|
+
describe User do
|
6
|
+
subject do
|
7
|
+
Class.new do
|
8
|
+
include User
|
9
|
+
end.new
|
10
|
+
end
|
11
|
+
describe "#mock_user" do
|
12
|
+
before do
|
13
|
+
Empresaula::API::Resource::User.site = "http://fake/api"
|
14
|
+
end
|
15
|
+
it "mocks a particular user" do
|
16
|
+
subject.mock_user({id: 1, name: "James"})
|
17
|
+
user = Empresaula::API::Resource::User.find(1)
|
18
|
+
user.name.must_equal "James"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: empresaula-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Codegram
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activeresource
|
16
|
+
requirement: &70309093422200 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70309093422200
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: turn
|
27
|
+
requirement: &70309093421720 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70309093421720
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: minitest
|
38
|
+
requirement: &70309093421160 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70309093421160
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: mocha
|
49
|
+
requirement: &70309093420700 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70309093420700
|
58
|
+
description: Ruby wrapper for interacting with Empresaula's API
|
59
|
+
email:
|
60
|
+
- info@codegram.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- .rvmrc
|
67
|
+
- Gemfile
|
68
|
+
- Guardfile
|
69
|
+
- Rakefile
|
70
|
+
- empresaula-api.gemspec
|
71
|
+
- lib/empresaula-api.rb
|
72
|
+
- lib/empresaula-api/config.rb
|
73
|
+
- lib/empresaula-api/controller.rb
|
74
|
+
- lib/empresaula-api/controller/authentication.rb
|
75
|
+
- lib/empresaula-api/resource.rb
|
76
|
+
- lib/empresaula-api/resource/base.rb
|
77
|
+
- lib/empresaula-api/resource/user.rb
|
78
|
+
- lib/empresaula-api/test_helpers.rb
|
79
|
+
- lib/empresaula-api/test_helpers/user.rb
|
80
|
+
- lib/empresaula-api/version.rb
|
81
|
+
- test/empresaula-api/config_test.rb
|
82
|
+
- test/empresaula-api/controller/authentication_test.rb
|
83
|
+
- test/empresaula-api/resource/user_test.rb
|
84
|
+
- test/empresaula-api/test_helpers/user_test.rb
|
85
|
+
- test/test_helper.rb
|
86
|
+
homepage:
|
87
|
+
licenses: []
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.8.10
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Ruby wrapper for interacting with Empresaula's API
|
110
|
+
test_files:
|
111
|
+
- test/empresaula-api/config_test.rb
|
112
|
+
- test/empresaula-api/controller/authentication_test.rb
|
113
|
+
- test/empresaula-api/resource/user_test.rb
|
114
|
+
- test/empresaula-api/test_helpers/user_test.rb
|
115
|
+
- test/test_helper.rb
|