empresaula-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create use 1.9.3@empresaula-api
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in empresaula-api.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'guard'
8
+ gem 'guard-minitest'
9
+ end
10
+
11
+ group :darwin do
12
+ gem 'rb-fsevent'
13
+ gem 'growl'
14
+ end
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'minitest' do
2
+ watch(%r|^test/(.*)_test\.rb|)
3
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
4
+ watch(%r|^test/test_helper\.rb|) { "test" }
5
+ end
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['./test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -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,11 @@
1
+ require 'active_resource'
2
+
3
+ module Empresaula
4
+ module API
5
+ module Resource
6
+ class Base < ActiveResource::Base
7
+ self.format = :json
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'base'
2
+
3
+ module Empresaula
4
+ module API
5
+ module Resource
6
+ class User < Base
7
+ def self.find_by_session_id(session_id)
8
+ User.new(get(session_id))
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -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,5 @@
1
+ module Empresaula
2
+ module API
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require "empresaula-api/version"
2
+ require "empresaula-api/resource"
3
+
4
+ require "empresaula-api/config"
5
+
6
+ module Empresaula
7
+ module API
8
+ end
9
+ end
@@ -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
@@ -0,0 +1,3 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'turn'
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