unipass_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,32 @@
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
+
19
+ yardoc/
20
+ .rvmrc
21
+
22
+ # System/IDE files
23
+ Desktop.ini
24
+ Thumbs.db
25
+ nbproject
26
+ .DS_Store
27
+ .idea
28
+ .redcar
29
+ \#*#
30
+ *.swp
31
+ *.swo
32
+ *~
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unipass_api.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Unipass API #
2
+
3
+ [![Build Status](https://secure.travis-ci.org/tjeden/unipass_api.png)](http://travis-ci.org/tjeden/unipass_api)
4
+
5
+ This is wrapper for Unipass REST API. In order to use it, you will need to get unipas access token (see [omniauth-unipass](https://github.com/tjeden/omniauth-unipass))
6
+
7
+ # Installation #
8
+
9
+ Add to your `Gemfile` following line:
10
+
11
+ gem 'unipass_api'
12
+
13
+ Create `config/initializers/unipass_api.rb` with configuration options:
14
+
15
+ UnipassApi.configure do |config|
16
+ config.client_id = 'some_client_id'
17
+ config.client_secret = 'some_client_secret'
18
+ config.site = 'some_site'
19
+ config.api_site = 'some_api_site'
20
+ config.authorize_url = 'some_authorize_url'
21
+ config.token_url = 'some_token_url'
22
+ end
23
+
24
+ And you are ready to go.
25
+
26
+ # Usage #
27
+
28
+ Assuming you are using omniauth, and you are succesfull logged in into unipass, you can create client in your application controller:
29
+
30
+ def unipass_api(force_refresh_token = false)
31
+ @unipass_api ||= UnipassApi::Client.new(
32
+ :access_token => session[:access_token],
33
+ :expires_at => session[:access_token_expires_at],
34
+ :refresh_token => session[:refresh_token]
35
+ )
36
+ if @unipass_api.expired? || force_refresh_token
37
+ @unipass_api.refresh!
38
+ session[:access_token] = @unipass_api.access_token
39
+ session[:access_token_expires_at] = @unipass_api.expires_at
40
+ session[:refresh_token] = @unipass_api.refresh_token
41
+ end
42
+ @unipass_api
43
+ end
44
+
45
+ And then you can just use it as follows:
46
+
47
+ unipass_api.post('groups', :params => {:name => 'group_name'})
48
+
49
+ ## Adding property ##
50
+
51
+ @property = begin
52
+ unipass_api.post("me/properties/weight", :params => {:value => '57 kg'})
53
+ rescue UnipassApi::ResourceInvalid => error
54
+ @errors = error.response.parsed
55
+ render :show and return
56
+ end
57
+
58
+ ## Showing property ##
59
+
60
+ @property = begin
61
+ unipass_api.get("me/properties/#{key}")
62
+ rescue UnipassApi::ResourceNotFound
63
+ nil
64
+ end
65
+
66
+
67
+
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require "bundler/gem_tasks"
12
+
13
+ require 'rake/testtask'
14
+
15
+ desc 'Run test suite'
16
+ task 'test' do
17
+ Rake::TestTask.new do |t|
18
+ t.libs << "test"
19
+ t.test_files = FileList['test/*_test.rb']
20
+ t.verbose = true
21
+ end
22
+ end
23
+
24
+ task :default => :test
@@ -0,0 +1,65 @@
1
+ module UnipassApi
2
+
3
+ class Client
4
+
5
+ attr_accessor :access_token, :refresh_token, :expires_at
6
+
7
+ def initialize(options = {}, &block)
8
+ @client_id = options[:client_id] || UnipassApi.options[:client_id]
9
+ @client_secret = options[:client_secret] || UnipassApi.options[:client_secret]
10
+ @site = options[:site] || UnipassApi.options[:site]
11
+ @api_site = options[:api_site] || UnipassApi.options[:api_site]
12
+ @authorize_url = options[:authorize_url] || UnipassApi.options[:authorize_url]
13
+ @token_url = options[:token_url] || UnipassApi.options[:token_url]
14
+
15
+ self.access_token = options[:access_token]
16
+ self.refresh_token = options[:refresh_token]
17
+ self.expires_at = options[:expires_at]
18
+ end
19
+
20
+ def expired?
21
+ token.expired?
22
+ end
23
+
24
+ def refresh!
25
+ new_token = token.refresh!
26
+ self.access_token = new_token.token
27
+ self.refresh_token = new_token.refresh_token
28
+ self.expires_at = new_token.expires_at
29
+ new_token
30
+ end
31
+
32
+ def request(verb, url, options = {})
33
+ token.request(verb, url, options).parsed
34
+ rescue OAuth2::Error => error
35
+ case error.response.status
36
+ when 404 then raise ResourceNotFound.new(error.response)
37
+ when 422 then raise ResourceInvalid.new(error.response)
38
+ else raise
39
+ end
40
+ end
41
+
42
+ def get(url, options = {})
43
+ request(:get, url, options)
44
+ end
45
+
46
+ def post(url, options = {})
47
+ request(:post, url, options)
48
+ end
49
+
50
+ def client
51
+ @client ||= ::OAuth2::Client.new(@client_id, @client_secret, {
52
+ :site => @api_site,
53
+ :authorize_url => @authorize_url,
54
+ :token_url => @token_url
55
+ })
56
+ end
57
+
58
+ private
59
+
60
+ def token
61
+ ::OAuth2::AccessToken.new(client, access_token, :refresh_token => refresh_token, :expires_at => expires_at, :mode => :query, :param_name => 'oauth_token')
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,30 @@
1
+ module UnipassApi
2
+ # Defines constants and methods related to configuration
3
+ module Config
4
+ # An array of valid keys in the options hash when configuring a {Twitter::Client}
5
+ VALID_OPTIONS_KEYS = [
6
+ :client_id,
7
+ :client_secret,
8
+ :site,
9
+ :api_site,
10
+ :authorize_url,
11
+ :token_url
12
+ ]
13
+
14
+ attr_accessor *VALID_OPTIONS_KEYS
15
+
16
+ # Convenience method to allow configuration options to be set in a block
17
+ def configure
18
+ yield self
19
+ self
20
+ end
21
+
22
+ # Create a hash of options and their values
23
+ def options
24
+ options = {}
25
+ VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
26
+ options
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,6 @@
1
+ module UnipassApi
2
+
3
+ class ResourceInvalid < OAuth2::Error
4
+ end
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ module UnipassApi
2
+
3
+ class ResourceNotFound < OAuth2::Error
4
+ end
5
+
6
+ end
@@ -0,0 +1,3 @@
1
+ module UnipassApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'oauth2'
2
+
3
+ require 'unipass_api/client'
4
+ require 'unipass_api/config'
5
+ require 'unipass_api/resource_invalid'
6
+ require 'unipass_api/resource_not_found'
7
+
8
+ module UnipassApi
9
+ extend Config
10
+ end
11
+
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class ClientTest < Test::Unit::TestCase
4
+
5
+ def test_expired
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ require 'test/unit'
2
+
3
+ require 'lib/unipass_api'
4
+
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../lib/unipass_api/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ['Aleksadner Dąbrowski', 'Karol Sarnacki']
5
+ gem.email = ['aleksander.dabrowski@connectmedica.com', 'karol.sarnacki@connectmedica.pl']
6
+ gem.description = 'Unipass client API'
7
+ gem.summary = 'In order to use unipass API you can use it.'
8
+ gem.homepage = 'https://github.com/tjeden/unipass-api'
9
+
10
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ gem.name = 'unipass_api'
14
+ gem.require_paths = ['lib']
15
+ gem.version = UnipassApi::VERSION
16
+
17
+ gem.add_dependency 'omniauth', '~> 0.3.2'
18
+ gem.add_dependency 'oauth2'
19
+ gem.add_dependency 'rake'
20
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unipass_api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - "Aleksadner D\xC4\x85browski"
14
+ - Karol Sarnacki
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-11-07 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 0
31
+ - 3
32
+ - 2
33
+ version: 0.3.2
34
+ prerelease: false
35
+ name: omniauth
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :runtime
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ prerelease: false
49
+ name: oauth2
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ type: :runtime
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ prerelease: false
63
+ name: rake
64
+ version_requirements: *id003
65
+ description: Unipass client API
66
+ email:
67
+ - aleksander.dabrowski@connectmedica.com
68
+ - karol.sarnacki@connectmedica.pl
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files: []
74
+
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - README.md
79
+ - Rakefile
80
+ - lib/unipass_api.rb
81
+ - lib/unipass_api/client.rb
82
+ - lib/unipass_api/config.rb
83
+ - lib/unipass_api/resource_invalid.rb
84
+ - lib/unipass_api/resource_not_found.rb
85
+ - lib/unipass_api/version.rb
86
+ - test/client_test.rb
87
+ - test/test_helper.rb
88
+ - unipass_api.gemspec
89
+ homepage: https://github.com/tjeden/unipass-api
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options: []
94
+
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ requirements: []
116
+
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.10
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: In order to use unipass API you can use it.
122
+ test_files: []
123
+