sheep 0.0.1a
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +20 -0
- data/Rakefile +5 -0
- data/lib/sheep/error.rb +26 -0
- data/lib/sheep/user.rb +51 -0
- data/lib/sheep/version.rb +3 -0
- data/lib/sheep.rb +23 -0
- data/sheep.gemspec +28 -0
- data/spec/app_helper.rb +15 -0
- data/spec/quality_spec.rb +56 -0
- data/spec/sheep/error_spec.rb +50 -0
- data/spec/sheep/user_spec.rb +61 -0
- data/spec/sheep_spec.rb +19 -0
- data/spec/spec_helper.rb +13 -0
- metadata +141 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Usage
|
2
|
+
|
3
|
+
Sheep.client_id = 'your client_id'
|
4
|
+
Sheep.client_secret = 'your client_secret'
|
5
|
+
|
6
|
+
user = Sheep::User.find('krekoten')
|
7
|
+
p user.full_name
|
8
|
+
|
9
|
+
p user.friends # => ['dorialan', 'maxmax', ...]
|
10
|
+
user.add_friend(Sheep::User.find('tuppy'))
|
11
|
+
# OR
|
12
|
+
user.add_friend('tuppy')
|
13
|
+
|
14
|
+
# Authors
|
15
|
+
|
16
|
+
* Marjan Krekoten' (Мар'ян Крекотень)
|
17
|
+
|
18
|
+
# Copyright
|
19
|
+
|
20
|
+
Intensol (c) 2011
|
data/Rakefile
ADDED
data/lib/sheep/error.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Sheep
|
2
|
+
|
3
|
+
class Error < StandardError; end
|
4
|
+
class BadRequest < Error; alias errors message; end
|
5
|
+
class NotFound < Error; end
|
6
|
+
class UnknownError < Error; alias response message; end
|
7
|
+
|
8
|
+
class ErrorResponse
|
9
|
+
def initialize(app = nil)
|
10
|
+
@app = app
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(response)
|
14
|
+
case response.status
|
15
|
+
when 200, 201, 301, 302
|
16
|
+
@app.call(response) if @app
|
17
|
+
when 400
|
18
|
+
raise Sheep::BadRequest.new(response.body['error'] || response.body['errors'])
|
19
|
+
when 404
|
20
|
+
raise Sheep::NotFound
|
21
|
+
else
|
22
|
+
raise Sheep::UnknownError.new(response)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/sheep/user.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Sheep
|
2
|
+
class User
|
3
|
+
class << self
|
4
|
+
def find(id)
|
5
|
+
new(Sheep.connection.get("/v1/users/#{id}").body['user'])
|
6
|
+
end
|
7
|
+
|
8
|
+
def create(attributes)
|
9
|
+
response = Sheep.connection.post('/v1/users') do |request|
|
10
|
+
request.params = { :user => attributes }
|
11
|
+
request.params['client_id'] = Sheep.client_id
|
12
|
+
request.params['client_secret'] = Sheep.client_secret
|
13
|
+
end
|
14
|
+
# TODO: refactor this to use Authorized when it will be finished
|
15
|
+
Sheep.access_token = response.body['access_token']
|
16
|
+
new(Sheep.connection.get(response.headers['Location']).body['user'])
|
17
|
+
end
|
18
|
+
|
19
|
+
def authorize!(username, password)
|
20
|
+
response = Sheep.connection.post('/v1/oauth/access_token') do |request|
|
21
|
+
request.params['username'] = username
|
22
|
+
request.params['password'] = password
|
23
|
+
request.params['client_id'] = Sheep.client_id
|
24
|
+
request.params['client_secret'] = Sheep.client_secret
|
25
|
+
request.params['grant_type'] = 'password'
|
26
|
+
end
|
27
|
+
Sheep.access_token = response.body['access_token']
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(data)
|
32
|
+
@data = data
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_missing(attribute, value = nil)
|
36
|
+
if attribute =~/\=$/
|
37
|
+
self[attribute.to_s.gsub(/\=$/, '')] = value
|
38
|
+
else
|
39
|
+
self[attribute]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def [](attribute)
|
44
|
+
@data[attribute.to_s]
|
45
|
+
end
|
46
|
+
|
47
|
+
def []=(attribute, value)
|
48
|
+
@data[attribute.to_s] = value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/sheep.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'apis'
|
2
|
+
require 'yajl'
|
3
|
+
|
4
|
+
lib_dir = File.expand_path('../', __FILE__)
|
5
|
+
$: << lib_dir unless $:.include?(lib_dir)
|
6
|
+
|
7
|
+
require 'sheep/user'
|
8
|
+
require 'sheep/error'
|
9
|
+
|
10
|
+
module Sheep
|
11
|
+
class << self
|
12
|
+
attr_accessor :client_id, :client_secret, :access_token
|
13
|
+
|
14
|
+
def connection
|
15
|
+
@connection ||= Apis::Connection.new(:uri => 'http://api-tuppy-com.heroku.com/') do
|
16
|
+
response do
|
17
|
+
use Apis::Middleware::Response::Json
|
18
|
+
use Sheep::ErrorResponse
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/sheep.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "sheep/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "sheep"
|
7
|
+
s.version = Sheep::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Marjan Krekoten' (Мар'ян Крекотень)"]
|
10
|
+
s.email = ["krekoten@intensol.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Funny sheep Tuppy}
|
13
|
+
s.description = %q{Ruby wrapper for Tuppy's API}
|
14
|
+
|
15
|
+
s.rubyforge_project = "[none]"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_development_dependency 'rspec', '>= 2.5'
|
23
|
+
s.add_development_dependency 'rack-test'
|
24
|
+
s.add_development_dependency 'sinatra'
|
25
|
+
s.add_dependency 'apis', '>= 0.3'
|
26
|
+
s.add_dependency 'multi_json'
|
27
|
+
s.add_dependency 'yajl-ruby'
|
28
|
+
end
|
data/spec/app_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
class App < Sinatra::Base
|
4
|
+
get '/v1/users/krekoten' do
|
5
|
+
%|{"user":{"id":2,"login":"krekoten","full_name": "Marjan Krekoten'","email":"krekoten@intensol.com"}}|
|
6
|
+
end
|
7
|
+
|
8
|
+
post '/v1/users' do
|
9
|
+
[201, {"Location" => "/v1/users/#{params[:user][:login]}"}, %|{"access_token":"access_token"}|]
|
10
|
+
end
|
11
|
+
|
12
|
+
post '/v1/oauth/access_token' do
|
13
|
+
[200, {}, %|{"access_token":"access_token"}|]
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "The library itself" do
|
4
|
+
def check_for_tab_characters(filename)
|
5
|
+
failing_lines = []
|
6
|
+
File.readlines(filename).each_with_index do |line,number|
|
7
|
+
failing_lines << number + 1 if line =~ /\t/
|
8
|
+
end
|
9
|
+
|
10
|
+
unless failing_lines.empty?
|
11
|
+
"#{filename} has tab characters on lines #{failing_lines.join(', ')}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def check_for_extra_spaces(filename)
|
16
|
+
failing_lines = []
|
17
|
+
File.readlines(filename).each_with_index do |line,number|
|
18
|
+
next if line =~ /^\s+#.*\s+\n$/
|
19
|
+
failing_lines << number + 1 if line =~ /\s+\n$/
|
20
|
+
end
|
21
|
+
|
22
|
+
unless failing_lines.empty?
|
23
|
+
"#{filename} has spaces on the EOL on lines #{failing_lines.join(', ')}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
RSpec::Matchers.define :be_well_formed do
|
28
|
+
failure_message_for_should do |actual|
|
29
|
+
actual.join("\n")
|
30
|
+
end
|
31
|
+
|
32
|
+
match do |actual|
|
33
|
+
actual.empty?
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it "has no malformed whitespace" do
|
38
|
+
error_messages = []
|
39
|
+
Dir.chdir(File.expand_path("../..", __FILE__)) do
|
40
|
+
`git ls-files`.split("\n").each do |filename|
|
41
|
+
next if filename =~ /\.gitmodules|fixtures|\.md/
|
42
|
+
error_messages << check_for_tab_characters(filename)
|
43
|
+
error_messages << check_for_extra_spaces(filename)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
error_messages.compact.should be_well_formed
|
47
|
+
end
|
48
|
+
|
49
|
+
it "can still be built" do
|
50
|
+
Dir.chdir(root) do
|
51
|
+
`gem build sheep.gemspec`
|
52
|
+
$?.should == 0
|
53
|
+
`rm sheep-*.gem`
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Error Handling' do
|
4
|
+
context 'status 400' do
|
5
|
+
let(:response) { Apis::Response.new(400, {}, {'errors' => {'login' => ["cann't be blank"]}})}
|
6
|
+
it 'raises BadRequest' do
|
7
|
+
expect {
|
8
|
+
Sheep::ErrorResponse.new.call(response)
|
9
|
+
}.to raise_error(Sheep::BadRequest)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'exception contains errors' do
|
13
|
+
begin
|
14
|
+
Sheep::ErrorResponse.new.call(response)
|
15
|
+
rescue Sheep::BadRequest => e
|
16
|
+
e.errors.should == {'login' => ["cann't be blank"]}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'status 404' do
|
22
|
+
let(:response) { Apis::Response.new(404, {}, nil)}
|
23
|
+
it 'raises NotFound' do
|
24
|
+
expect {
|
25
|
+
Sheep::ErrorResponse.new.call(response)
|
26
|
+
}.to raise_error(Sheep::NotFound)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'unknown status' do
|
31
|
+
let(:response) { Apis::Response.new(510, {}, nil)}
|
32
|
+
it 'raises UnknownError' do
|
33
|
+
expect {
|
34
|
+
Sheep::ErrorResponse.new.call(response)
|
35
|
+
}.to raise_error(Sheep::UnknownError)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
[200, 201, 301, 302].each do |status|
|
40
|
+
context "status #{status}" do
|
41
|
+
let(:response) { Apis::Response.new(status, {}, nil)}
|
42
|
+
|
43
|
+
it 'doesn\'t raise anything' do
|
44
|
+
expect {
|
45
|
+
Sheep::ErrorResponse.new.call(response)
|
46
|
+
}.to_not raise_error
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sheep::User do
|
4
|
+
before do
|
5
|
+
Sheep.client_id = 'client'
|
6
|
+
Sheep.client_secret = 'secret'
|
7
|
+
Sheep.connection.adapter Apis::Adapter::RackTest
|
8
|
+
Sheep.connection.adapter.app = App
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'find' do
|
12
|
+
it 'returns itself initialized with user data' do
|
13
|
+
krekoten = Sheep::User.find('krekoten')
|
14
|
+
krekoten.full_name.should == 'Marjan Krekoten\''
|
15
|
+
krekoten.login.should == 'krekoten'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'create' do
|
20
|
+
before do
|
21
|
+
@user = Sheep::User.create(
|
22
|
+
:login => 'krekoten',
|
23
|
+
:full_name => 'Marjan Krekoten\'',
|
24
|
+
:password => '123456',
|
25
|
+
:email => 'krekoten@intensol.com'
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'returns itself initialized with user data' do
|
30
|
+
@user.login.should == 'krekoten'
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'sets token' do
|
34
|
+
Sheep.access_token.should == 'access_token'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'authorize!' do
|
39
|
+
before do
|
40
|
+
@token = Sheep::User.authorize!('login', 'password')
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'sends client credentials' do
|
44
|
+
Sheep.connection.adapter.last_params['client_id'].should == 'client'
|
45
|
+
Sheep.connection.adapter.last_params['client_secret'].should == 'secret'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'sends user credentials' do
|
49
|
+
Sheep.connection.adapter.last_params['username'].should == 'login'
|
50
|
+
Sheep.connection.adapter.last_params['password'].should == 'password'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'returns token' do
|
54
|
+
@token.should == 'access_token'
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'sets token' do
|
58
|
+
Sheep.access_token.should == 'access_token'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/sheep_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sheep do
|
4
|
+
context 'configuration' do
|
5
|
+
it 'sets client_id' do
|
6
|
+
Sheep.client_id = 'xxxx'
|
7
|
+
Sheep.client_id.should == 'xxxx'
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'sets client_secret' do
|
11
|
+
Sheep.client_secret = 'abcdef'
|
12
|
+
Sheep.client_secret.should == 'abcdef'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'adds ErrorResponse middleware to stack' do
|
16
|
+
Sheep.connection.response.to_a.should include(Sheep::ErrorResponse)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sheep
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: 5
|
5
|
+
version: 0.0.1a
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- "Marjan Krekoten' (\xD0\x9C\xD0\xB0\xD1\x80'\xD1\x8F\xD0\xBD \xD0\x9A\xD1\x80\xD0\xB5\xD0\xBA\xD0\xBE\xD1\x82\xD0\xB5\xD0\xBD\xD1\x8C)"
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-29 00:00:00 +03:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "2.5"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack-test
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: sinatra
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: apis
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0.3"
|
58
|
+
type: :runtime
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: multi_json
|
62
|
+
prerelease: false
|
63
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
type: :runtime
|
70
|
+
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: yajl-ruby
|
73
|
+
prerelease: false
|
74
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
type: :runtime
|
81
|
+
version_requirements: *id006
|
82
|
+
description: Ruby wrapper for Tuppy's API
|
83
|
+
email:
|
84
|
+
- krekoten@intensol.com
|
85
|
+
executables: []
|
86
|
+
|
87
|
+
extensions: []
|
88
|
+
|
89
|
+
extra_rdoc_files: []
|
90
|
+
|
91
|
+
files:
|
92
|
+
- .gitignore
|
93
|
+
- Gemfile
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/sheep.rb
|
97
|
+
- lib/sheep/error.rb
|
98
|
+
- lib/sheep/user.rb
|
99
|
+
- lib/sheep/version.rb
|
100
|
+
- sheep.gemspec
|
101
|
+
- spec/app_helper.rb
|
102
|
+
- spec/quality_spec.rb
|
103
|
+
- spec/sheep/error_spec.rb
|
104
|
+
- spec/sheep/user_spec.rb
|
105
|
+
- spec/sheep_spec.rb
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
has_rdoc: true
|
108
|
+
homepage: ""
|
109
|
+
licenses: []
|
110
|
+
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: "0"
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ">"
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: 1.3.1
|
128
|
+
requirements: []
|
129
|
+
|
130
|
+
rubyforge_project: "[none]"
|
131
|
+
rubygems_version: 1.6.2
|
132
|
+
signing_key:
|
133
|
+
specification_version: 3
|
134
|
+
summary: Funny sheep Tuppy
|
135
|
+
test_files:
|
136
|
+
- spec/app_helper.rb
|
137
|
+
- spec/quality_spec.rb
|
138
|
+
- spec/sheep/error_spec.rb
|
139
|
+
- spec/sheep/user_spec.rb
|
140
|
+
- spec/sheep_spec.rb
|
141
|
+
- spec/spec_helper.rb
|