centro-client 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
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ Centro Ruby Client
2
+ ==================
3
+
4
+ The Centro Ruby Client is used to interact with the Centro API from
5
+ Ruby.
6
+
7
+ For more about the Centro API see <http://api-docs.centro.net>.
8
+
9
+ Usage
10
+ -----
11
+
12
+ Start by creating a connection to Centro with your credentials:
13
+
14
+ require 'centro-centro'
15
+
16
+ centro = Centro::API.new(:api_key => API_KEY)
17
+
18
+ NOTE: You can leave out the `:api_key` if `ENV['CENTRO_API_KEY']` is set
19
+ instead.
20
+
21
+ Now you can make requests to the api.
22
+
23
+ Requests
24
+ --------
25
+
26
+ What follows is an overview of commands you can run for the client.
27
+
28
+ For additional details about any of the commands, see the [API
29
+ docs](http://api-docs.centro.net).
30
+
31
+ Mock
32
+ ----
33
+
34
+ For practice or testing you can also use a simulated Centro:
35
+
36
+ require 'centro-api'
37
+
38
+ centro = Centro::API.new(:api_key => API_KEY, :mock => true)
39
+
40
+ After that commands should still behave the same, but they will only
41
+ modify some local data instead of updating the state of things on
42
+ Centro.
43
+
44
+ Tests
45
+ -----
46
+
47
+ To run tests, first set `ENV['CENTRO_API_KEY']` to your api key. Then
48
+ use `bundle exec rake` to run mock tests or `MOCK=false bundle exec
49
+ rake` to run integration tests.
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'centro/client/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "centro-client"
8
+ gem.version = Centro::Client::VERSION
9
+ gem.authors = ["Tim Galeckas"]
10
+ gem.email = ["tim@galeckas.com"]
11
+ gem.description = %q{Ruby Client for the Centro api}
12
+ gem.summary = %q{Ruby Client for the Centro api}
13
+ gem.homepage = "http://github.com/centro/centro.rb"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rspec'
21
+ gem.add_development_dependency 'webmock'
22
+ gem.add_development_dependency 'guard-rspec'
23
+
24
+ gem.add_dependency 'omniauth'
25
+ gem.add_dependency 'omniauth-oauth2'
26
+ gem.add_dependency 'faraday'
27
+ gem.add_dependency 'faraday_middleware'
28
+ end
@@ -0,0 +1 @@
1
+ require(File.join(File.dirname(__FILE__), "centro", "client"))
@@ -0,0 +1,132 @@
1
+ require 'omniauth'
2
+ require 'omniauth-oauth2'
3
+ require 'faraday_middleware'
4
+
5
+ module Centro
6
+ class Client
7
+ class << self
8
+ def api_version
9
+ return @api_version if defined?(@api_version)
10
+ @api_version = 'v1'
11
+ end
12
+ def auth_host
13
+ return @auth_host if defined?(@auth_host)
14
+ @auth_host = 'localhost:3001'
15
+ end
16
+ def mms_host
17
+ return @mms_host if defined?(@mms_host)
18
+ @mms_host = 'localhost:3000'
19
+ end
20
+ def ssl_enabled?
21
+ return @ssl_enabled if defined?(@ssl_enabled)
22
+ @ssl_enaabled = false
23
+ end
24
+ def auth_url
25
+ url_from_host(auth_host)
26
+ end
27
+ def mms_url
28
+ url_from_host(mms_host)
29
+ end
30
+ def url_from_host(host)
31
+ (ssl_enabled? ? 'https://' : 'http://') +
32
+ host
33
+ end
34
+ end
35
+
36
+ attr_accessor :client_id, :client_secret, :access_token
37
+
38
+ def initialize(opts={})
39
+ @client_id = opts.delete(:client_id) if opts[:client_id]
40
+ @client_secret = opts.delete(:client_secret) if opts[:client_secret]
41
+ @access_token = opts.delete(:access_token) if opts[:access_token]
42
+ end
43
+
44
+ def retrieve_access_token(username, password)
45
+ raise 'client_id and client_secret required' unless @client_id && @client_secret
46
+ connection = Faraday.new self.class.auth_url do |conn|
47
+ set_default_connection_options(conn)
48
+ end
49
+ response = connection.post('/oauth/token',
50
+ :grant_type => 'password',
51
+ :client_id => @client_id,
52
+ :client_secret => @client_secret,
53
+ :username => username,
54
+ :password => password)
55
+ raise 'Failed to get an access token' unless response.success? && response.body['access_token']
56
+ self.access_token = response.body['access_token']
57
+ end
58
+
59
+
60
+ def get_credentials
61
+ auth_connection.get("/api/#{self.class.api_version}/me.json").body
62
+ end
63
+
64
+ def get_organizations(user_id=nil)
65
+ options = {}
66
+ options[:user_id]=user_id if user_id
67
+ auth_connection.get("/api/#{self.class.api_version}/organizations.json", options).body
68
+ end
69
+
70
+ def get_members_of_organization(organization_id)
71
+ auth_connection.get("/api/#{self.class.api_version}/organizations/#{organization_id}/members.json").body
72
+ end
73
+
74
+ ##
75
+ # Creates a user in an organization. If the user already exists in AuthTransis they will
76
+ # simply be added to the org (assuming the resource owner has rights), if they don't exist
77
+ # they will be created and added to the organizations.
78
+ #
79
+ # Returns a list of the organizations that they were sucessfully added to.
80
+ def create_user_in_organization(email, organization_id)
81
+ org_ids = organization_id.respond_to?(:each) ? organization_id : [organization_id]
82
+ successfuls = []
83
+ org_ids.each do |org_id|
84
+ response = auth_connection.post("/api/#{self.class.api_version}/organizations/#{organization_id}/members.json", {:email_address => email})
85
+ response.success? && successfuls << response.body
86
+ end
87
+ successfuls
88
+ end
89
+
90
+ def create_user(email)
91
+ response = auth_connection.post("/api/#{self.class.api_version}/members.json", {:email_address => email})
92
+ response.success? && response.body
93
+ end
94
+
95
+ def create_organization(org_name)
96
+ response = auth_connection.post("/api/#{self.class.api_version}/organizations", {:name => org_name})
97
+ response.success? && response.body
98
+ end
99
+
100
+ private
101
+
102
+ def set_default_connection_options(conn)
103
+ conn.request :json
104
+
105
+ conn.response :dates
106
+ conn.response :mashify
107
+ conn.response :json, :content_type => /\bjson$/
108
+ conn.response :raise_error
109
+
110
+ conn.adapter Faraday.default_adapter
111
+ end
112
+
113
+ def auth_connection
114
+ raise 'access_token required' unless @access_token
115
+ @auth_connection ||= begin
116
+ Faraday.new self.class.auth_url do |conn|
117
+ conn.request :oauth2, @access_token
118
+ set_default_connection_options(conn)
119
+ end
120
+ end
121
+ end
122
+ def mms_connection
123
+ raise 'access_token required' unless @access_token
124
+ @mms_connection ||= begin
125
+ Faraday.new self.class.mms_url do |conn|
126
+ conn.request :oauth2, @access_token
127
+ set_default_connection_options(conn)
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,5 @@
1
+ module Centro
2
+ class Client
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: centro-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tim Galeckas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
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: webmock
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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'
46
+ - !ruby/object:Gem::Dependency
47
+ name: guard-rspec
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: omniauth
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
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
+ - !ruby/object:Gem::Dependency
79
+ name: omniauth-oauth2
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: faraday
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: faraday_middleware
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Ruby Client for the Centro api
127
+ email:
128
+ - tim@galeckas.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - README.md
135
+ - centro-client.gemspec
136
+ - lib/centro-client.rb
137
+ - lib/centro/client.rb
138
+ - lib/centro/client/version.rb
139
+ homepage: http://github.com/centro/centro.rb
140
+ licenses: []
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 1.8.23
160
+ signing_key:
161
+ specification_version: 3
162
+ summary: Ruby Client for the Centro api
163
+ test_files: []
164
+ has_rdoc: