motion-oauth2 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,18 @@
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
18
+ build
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in motion-oauth2.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 nov matake
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,29 @@
1
+ # Motion::Oauth2
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'motion-oauth2'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install motion-oauth2
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ $:.unshift("/Library/RubyMotion/lib")
3
+ require 'motion/project'
4
+ Bundler.setup
5
+ Bundler.require
6
+
7
+ namespace :spec do
8
+ desc "Run Motion::OAuth2 specs without compile"
9
+ task :lib do
10
+ sh "bacon #{Dir.glob("spec/**/*_spec.rb").join(' ')}"
11
+ end
12
+ task :motion => 'spec'
13
+ task :all => [:lib, :motion]
14
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,22 @@
1
+ # NOTE:
2
+ # FB doesn't support "Bearer" schema.
3
+ # You need to specify "OAuth" schema as the optional 2nd argument here.
4
+ oauth2 = Motion::OAuth2.new(ACCESS_TOKEN, :OAuth)
5
+
6
+ oauth2.get 'https://graph.facebook.com/me' do |res|
7
+ @res = res
8
+ case
9
+ when res.success?
10
+ if res.json?
11
+ puts "Hello #{res.body['name']}!" # => If response is JSON, it's automatically parsed.
12
+ else
13
+ puts res.body
14
+ end
15
+ when res.redirect?
16
+ # ignore
17
+ when res.error?
18
+ puts res.body
19
+ puts res.error.code # => OAuth2 error code as Symbol, or :unknown
20
+ puts res.error.description # => OAuth2 error description, or nil
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "This file must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ require 'bubble-wrap/core'
6
+ require 'bubble-wrap/http'
7
+
8
+ Motion::Project::App.setup do |app|
9
+ Dir.glob(
10
+ File.join(File.dirname(__FILE__), 'motion/**/*.rb')
11
+ ).each do |file|
12
+ app.files.unshift file
13
+ end
14
+ end
@@ -0,0 +1,58 @@
1
+ module Motion
2
+ class OAuth2
3
+ attr_accessor :access_token, :schema
4
+
5
+ def initialize(access_token, schema = :Bearer)
6
+ self.access_token = access_token
7
+ self.schema = schema
8
+ end
9
+
10
+ def get(url, options = {}, &block)
11
+ request :get, url, options, &block
12
+ end
13
+
14
+ def post(url, options = {}, &block)
15
+ request :post, url, options, &block
16
+ end
17
+
18
+ def put(url, options = {}, &block)
19
+ request :put, url, options, &block
20
+ end
21
+
22
+ def delete(url, options = {}, &block)
23
+ request :delete, url, options, &block
24
+ end
25
+
26
+ def head(url, options = {}, &block)
27
+ request :head, url, options, &block
28
+ end
29
+
30
+ def patch(url, options = {}, &block)
31
+ request :patch, url, options, &block
32
+ end
33
+
34
+ private
35
+
36
+ def request(method, url, options = {}, &block)
37
+ BubbleWrap::HTTP.send method, url, with_token(options) do |raw_response|
38
+ response = Response.new raw_response
39
+ if block.is_a? Proc
40
+ block.call response
41
+ else
42
+ # TODO:
43
+ # Warn here that developers should handle OAuth2 error here.
44
+ # But how to *warn* using NSLog??
45
+ NSLog "You don't care about OAuth2 errors? otherwise specify your own callback as block."
46
+ end
47
+ end
48
+ end
49
+
50
+ def with_token(options = {})
51
+ options[:headers] ||= {}
52
+ options[:headers].merge!(
53
+ Authorization: "#{schema} #{access_token}"
54
+ )
55
+ options
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,17 @@
1
+ module Motion
2
+ class OAuth2
3
+ class << self
4
+ def debug=(bool)
5
+ BubbleWrap.debug = bool
6
+ end
7
+
8
+ def debug!
9
+ BubbleWrap.debug = true
10
+ end
11
+
12
+ def debug?
13
+ BubbleWrap.debug?
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ module Motion
2
+ class OAuth2
3
+ class Error
4
+ attr_accessor :raw, :code, :description, :url, :www_authenticate
5
+
6
+ def initialize(body, www_authenticate_header = nil)
7
+ self.raw = body
8
+ self.www_authenticate = www_authenticate_header
9
+ if oauth2_error?
10
+ self.code = body['error'].to_sym
11
+ self.description = body['description']
12
+ self.url = body['error_url']
13
+ else
14
+ self.code = :unknown
15
+ end
16
+ end
17
+
18
+ def oauth2_error?
19
+ @body.is_a?(Hash) &&
20
+ [
21
+ 'invalid_request',
22
+ 'invalid_token',
23
+ 'insufficient_scope'
24
+ ].include?(body[:error])
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,82 @@
1
+ module Motion
2
+ class OAuth2
3
+ class Response
4
+ attr_accessor :raw_response
5
+
6
+ def initialize(raw_response)
7
+ self.raw_response = raw_response
8
+ end
9
+
10
+ def success?
11
+ (200..299).include? status_code
12
+ end
13
+
14
+ def redirect?
15
+ (300..399).include? status_code
16
+ end
17
+
18
+ def client_error?
19
+ (400..499).include? status_code
20
+ end
21
+
22
+ def server_error?
23
+ (500..599).include? status_code
24
+ end
25
+
26
+ def error?
27
+ client_error? || server_error?
28
+ end
29
+
30
+ def bad_request?
31
+ status_code = 400
32
+ end
33
+
34
+ def unauthorized?
35
+ status_code == 401
36
+ end
37
+
38
+ def forbidden?
39
+ status_code == 402
40
+ end
41
+
42
+ def not_found?
43
+ status_code == 400
44
+ end
45
+
46
+ def json?
47
+ !!json
48
+ end
49
+
50
+ def status_code
51
+ raw_response.status_code
52
+ end
53
+
54
+ def body
55
+ raw_response.body.to_str
56
+ end
57
+
58
+ def json
59
+ @json ||= case raw_response.headers['Content-Type']
60
+ when /^application\/json/
61
+ BubbleWrap::JSON.parse body
62
+ when /^text\/javascript/
63
+ # NOTE: FB Graph API uses text/javasctipt Content-Type for JSON response
64
+ case body
65
+ when /^(\[|\{)/
66
+ BubbleWrap::JSON.parse body
67
+ else
68
+ body
69
+ end
70
+ else
71
+ nil
72
+ end
73
+ end
74
+
75
+ def error
76
+ if error?
77
+ Error.new body, raw_response.headers['WWW-Authenticate']
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["nov matake"]
5
+ gem.email = ["nov@matake.jp"]
6
+ gem.description = %q{OAuth2 client library for RubyMotion apps, you need APIs right?}
7
+ gem.summary = %q{OAuth2 client library for RubyMotion apps}
8
+ gem.homepage = "https://github.com/nov/motion-oauth2"
9
+ gem.files = `git ls-files`.split($\)
10
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
11
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
+ gem.name = "motion-oauth2"
13
+ gem.require_paths = ["lib"]
14
+ gem.version = File.read("VERSION").delete("\n\r")
15
+ gem.add_runtime_dependency "bubble-wrap"
16
+ gem.add_development_dependency 'mocha', '0.11.4'
17
+ gem.add_development_dependency 'mocha-on-bacon'
18
+ gem.add_development_dependency 'bacon'
19
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../../spec_helper.rb', __FILE__)
2
+
3
+ describe Motion::OAuth2 do
4
+ before do
5
+ @client = Motion::OAuth2.new('access_token')
6
+ end
7
+
8
+ describe '.new' do
9
+ it 'should setup access token' do
10
+ client = Motion::OAuth2.new('access_token')
11
+ client.access_token.should == 'access_token'
12
+ end
13
+
14
+ it 'should use Bearer schema as default' do
15
+ client = Motion::OAuth2.new('access_token')
16
+ client.schema.should == :Bearer
17
+ end
18
+
19
+ it 'should allow overwrite schema' do
20
+ client = Motion::OAuth2.new('access_token', :OAuth)
21
+ client.schema.should == :OAuth
22
+ end
23
+ end
24
+
25
+ [:get, :post, :put, :delete, :head, :patch].each do |method|
26
+ describe "##{method}" do
27
+ # TODO
28
+ # it 'should set OAuth2 Authorization header'
29
+ # others?
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ require 'mocha-on-bacon'
2
+ require File.expand_path('../support/motion_stub.rb', __FILE__)
3
+ require 'motion/oauth2'
4
+
5
+ style = :focused
@@ -0,0 +1,14 @@
1
+ # Create a fake Motion class hierarchy for testing.
2
+ module Motion
3
+ module Project
4
+ class App
5
+ def self.setup
6
+ end
7
+ end
8
+
9
+ class Config
10
+ def files_dependencies
11
+ end
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - nov matake
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bubble-wrap
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.11.4
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.11.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: mocha-on-bacon
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bacon
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: OAuth2 client library for RubyMotion apps, you need APIs right?
79
+ email:
80
+ - nov@matake.jp
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - VERSION
91
+ - example/fb_graph.rb
92
+ - lib/motion-oauth2.rb
93
+ - lib/motion/oauth2.rb
94
+ - lib/motion/oauth2/debugger.rb
95
+ - lib/motion/oauth2/error.rb
96
+ - lib/motion/oauth2/response.rb
97
+ - motion-oauth2.gemspec
98
+ - spec/motion/oauth2_spec.rb
99
+ - spec/spec_helper.rb
100
+ - spec/support/motion_stub.rb
101
+ homepage: https://github.com/nov/motion-oauth2
102
+ licenses: []
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ segments:
114
+ - 0
115
+ hash: 1143494254103297252
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ segments:
123
+ - 0
124
+ hash: 1143494254103297252
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.24
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: OAuth2 client library for RubyMotion apps
131
+ test_files:
132
+ - spec/motion/oauth2_spec.rb
133
+ - spec/spec_helper.rb
134
+ - spec/support/motion_stub.rb