personal_api_client 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/README.md +65 -0
- data/lib/oauth2_client.rb +81 -0
- data/lib/personal_api_client.rb +97 -0
- data/lib/personal_api_client/version.rb +3 -0
- data/personal_api_client.gemspec +19 -0
- metadata +66 -0
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
personal_api_client.gem
|
2
|
+
=======================
|
3
|
+
|
4
|
+
#INSTRUCTIONS:
|
5
|
+
|
6
|
+
1. Build the gem with following command: gem build personal_api_client.gemspec
|
7
|
+
2. Put following line in Gemfile of your application: gem 'personal_api_client', :path => "path_to_the_folder_where_your_gem_is"
|
8
|
+
3. Run bundle command
|
9
|
+
4. Congrats! You've succesfully built and installed the gem.
|
10
|
+
|
11
|
+
|
12
|
+
5. Getting the access token:
|
13
|
+
5.1 Initialize your Personal Oauth2 Client: client = Oauth2Client.initialize_auth_code(parameters) where parameters are:
|
14
|
+
|
15
|
+
{:client_id => your_client_id,
|
16
|
+
:client_secret => your_client_secret,
|
17
|
+
:target_site => api_endpoint_i_e_https://api-sandbox.personal.com,
|
18
|
+
:callback_uri => path_to_your_method_where_authorization_code_will_be_sent_i_e_oauth2/my_callback_method,
|
19
|
+
:scope => scope_as_per_http://developer.personal.com/docs/read/authentication/Authorization_Code}
|
20
|
+
|
21
|
+
5.2 Redirect to authorize URL (recirect_to client.authorize_url)
|
22
|
+
|
23
|
+
5.3 Authorization code will arrive to the callback URL you specified in :callback_uri. Call get_access_token:
|
24
|
+
access_token = client.get_access_token(parameters) where parameters are:
|
25
|
+
|
26
|
+
{:code => params[:code],
|
27
|
+
:callback_uri => the_same_as_in_previous_request}
|
28
|
+
|
29
|
+
6. Get the data
|
30
|
+
6.1 Initialize Personal API Client:
|
31
|
+
personal_api_client = PersonalApiClient.new(access_token, your_client_id)
|
32
|
+
|
33
|
+
6.2 Use some of the methods of the client to manipulate data (consult http://developer.personal.com/docs if you need to assemble request body):
|
34
|
+
|
35
|
+
|
36
|
+
Getting List of Gems
|
37
|
+
get_list_of_gems
|
38
|
+
|
39
|
+
Read Existing Gem
|
40
|
+
get_gem(gem_instance_id)
|
41
|
+
|
42
|
+
Write to Existing Gem
|
43
|
+
write_existing_gem(gem_instance_id, body, client_password)
|
44
|
+
|
45
|
+
Create New Gem
|
46
|
+
create_gem(body, client_password)
|
47
|
+
|
48
|
+
Delete Existing Gem
|
49
|
+
delete_gem(gem_instance_id)
|
50
|
+
|
51
|
+
Remove Gem Access
|
52
|
+
remove_gem_access(gem_instance_id, owner_id=nil)
|
53
|
+
|
54
|
+
Get Gem Schema
|
55
|
+
get_gem_schema(gem_template_id=nil)
|
56
|
+
|
57
|
+
Get Gem Schema Version
|
58
|
+
get_gem_schema_version
|
59
|
+
|
60
|
+
Granting Access
|
61
|
+
grant_access(body, client_password)
|
62
|
+
|
63
|
+
Requesting Access
|
64
|
+
request_access(body)
|
65
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'oauth2'
|
2
|
+
|
3
|
+
class Oauth2Client
|
4
|
+
@@oauth_config = nil
|
5
|
+
@@callback_uri = nil
|
6
|
+
|
7
|
+
attr_accessor :authorize_url
|
8
|
+
attr_accessor :strategy
|
9
|
+
attr_accessor :client
|
10
|
+
|
11
|
+
def self.initialize_auth_code(options={})
|
12
|
+
c = Oauth2Client.new(options[:client_id], options[:client_secret], options[:target_site])
|
13
|
+
c.authorize_url = c.client.auth_code.authorize_url(:redirect_uri => options[:callback_uri], :scope => options[:scope])
|
14
|
+
c.strategy = :auth_code
|
15
|
+
@@callback_uri = Oauth2Client.callback_uri
|
16
|
+
c
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.initialize_client_auth(options={})
|
20
|
+
c = Oauth2Client.new(options[:client_id], options[:client_secret], options[:target_site])
|
21
|
+
c.strategy = :client_credentials
|
22
|
+
c
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.initialize_from_configuration(path)
|
26
|
+
@@oauth_config = YAML.load_file(File.new(path)) unless @@oauth_config
|
27
|
+
Oauth2Client.initialize_auth_code({:client_id => Oauth2Client.client_id,
|
28
|
+
:client_secret => Oauth2Client.client_secret,
|
29
|
+
:target_site => Oauth2Client.target_site,
|
30
|
+
:callback_uri => Oauth2Client.callback_uri,
|
31
|
+
:scope => Oauth2Client.scope})
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_access_token(options={})
|
35
|
+
params = prepare_params options
|
36
|
+
@client.send(strategy).send(:get_token, *params)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.callback_uri
|
40
|
+
return @@callback_uri if @@callback_uri
|
41
|
+
@@oauth_config[site]["my_site"] + @@oauth_config[site]["callback_uri"]
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def initialize(client_id, client_secret, target_site)
|
46
|
+
@client = OAuth2::Client.new(client_id, client_secret, :site => target_site)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.site
|
50
|
+
@@oauth_config["site"]
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.scope
|
54
|
+
@@oauth_config[site]["scope"]
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.client_id
|
58
|
+
@client_id = @@oauth_config[site]["client_id"]
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.client_secret
|
62
|
+
@@oauth_config[site]["client_secret"]
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.target_site
|
66
|
+
@@oauth_config[site]["target_site"]
|
67
|
+
end
|
68
|
+
|
69
|
+
def prepare_params(params)
|
70
|
+
if strategy==:auth_code
|
71
|
+
raise Oauth2ClientException("You must specify code and callback_uri") unless params[:code] && params[:callback_uri]
|
72
|
+
return_list = [params[:code], {:redirect_uri=>params[:callback_uri]}]
|
73
|
+
elsif strategy==:client_credentials
|
74
|
+
return_list = []
|
75
|
+
end
|
76
|
+
return_list
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class Oauth2ClientException < Exception
|
81
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'oauth2_client'
|
2
|
+
|
3
|
+
# TODO: create body builders for post request and make post requests parametrized
|
4
|
+
class PersonalApiClient
|
5
|
+
API_VERSION_1 = "v1"
|
6
|
+
API_PATH = "api"
|
7
|
+
|
8
|
+
attr_accessor :client_id
|
9
|
+
|
10
|
+
def api_path
|
11
|
+
"/#{API_PATH}/#{API_VERSION_1}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(at, cl_id)
|
15
|
+
@access_token = at
|
16
|
+
@client_id = cl_id
|
17
|
+
end
|
18
|
+
|
19
|
+
#Getting List of Gems
|
20
|
+
def get_list_of_gems
|
21
|
+
get(add_client_id_to "#{api_path}/gems/")
|
22
|
+
end
|
23
|
+
|
24
|
+
#Read Existing Gem
|
25
|
+
def get_gem(gem_instance_id)
|
26
|
+
get(add_client_id_to "#{api_path}/gems/#{gem_instance_id}")
|
27
|
+
end
|
28
|
+
|
29
|
+
#Write to Existing Gem
|
30
|
+
def write_existing_gem(gem_instance_id, body, client_password)
|
31
|
+
put(add_client_id_to "#{api_path}/gems/#{gem_instance_id}", {:body=>body, :headers=>{"Secure-Password"=>client_password }})
|
32
|
+
end
|
33
|
+
|
34
|
+
#Create New Gem
|
35
|
+
def create_gem(body, client_password)
|
36
|
+
post(add_client_id_to "#{api_path}/gems/", {:body=>body, :headers=>{"Secure-Password"=>client_password}})
|
37
|
+
end
|
38
|
+
|
39
|
+
#Delete Existing Gem
|
40
|
+
def delete_gem(gem_instance_id)
|
41
|
+
delete(add_client_id_to "#{api_path}/gems/#{gem_instance_id}")
|
42
|
+
end
|
43
|
+
|
44
|
+
#Remove Gem Access
|
45
|
+
def remove_gem_access(gem_instance_id, owner_id=nil)
|
46
|
+
delete(add_client_id_to "#{api_path}/gems/#{gem_instance_id}/access/#{owner_id}")
|
47
|
+
end
|
48
|
+
|
49
|
+
#Get Gem Schema
|
50
|
+
def get_gem_schema(gem_template_id=nil)
|
51
|
+
path_to_schema = "#{api_path}/schema"
|
52
|
+
path_to_schema.concat("/#{gem_template_id}") if gem_template_id
|
53
|
+
get(add_client_id_to path_to_schema)
|
54
|
+
end
|
55
|
+
|
56
|
+
#Get Gem Schema Version
|
57
|
+
def get_gem_schema_version
|
58
|
+
get(add_client_id_to "#{api_path}/schema/version")
|
59
|
+
end
|
60
|
+
|
61
|
+
#Granting Access
|
62
|
+
def grant_access(body, client_password)
|
63
|
+
post(add_client_id_to "#{api_path}/access/grant", {:body=>body, :headers=>{"Secure-Password"=>client_password}})
|
64
|
+
end
|
65
|
+
|
66
|
+
#Requesting Access
|
67
|
+
def request_access(body)
|
68
|
+
post(add_client_id_to "#{api_path}/access/request", :body=>body)
|
69
|
+
end
|
70
|
+
|
71
|
+
protected
|
72
|
+
def get(resource, params = {})
|
73
|
+
fire(__method__, resource, :params => params)
|
74
|
+
end
|
75
|
+
|
76
|
+
def post(resource, params = {})
|
77
|
+
fire(__method__, resource, :params => params)
|
78
|
+
end
|
79
|
+
|
80
|
+
def put(resource, params = {})
|
81
|
+
fire(__method__, resource, :params => params)
|
82
|
+
end
|
83
|
+
|
84
|
+
def delete(resource, params = {})
|
85
|
+
fire(__method__, resource, :params => params)
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
def fire(method_name, resource, params)
|
90
|
+
response = @access_token.method(method_name).call(resource, :params => params)
|
91
|
+
return response.status, response.parsed
|
92
|
+
end
|
93
|
+
|
94
|
+
def add_client_id_to(url)
|
95
|
+
"#{url}?client_id=#{client_id}"
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "personal_api_client/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'personal_api_client'
|
6
|
+
s.version = PersonalApi::VERSION
|
7
|
+
s.date = '2012-10-31'
|
8
|
+
s.summary = "Personal API Client"
|
9
|
+
s.description = "Personal library for easier interaction with Personal API"
|
10
|
+
s.authors = ["Adnan Karac"]
|
11
|
+
s.add_runtime_dependency "oauth2", ["= 0.8.0"]
|
12
|
+
s.email = 'adnan@personal.com'
|
13
|
+
s.files = `git ls-files`.split($\)
|
14
|
+
s.require_paths = ["lib"]
|
15
|
+
s.rubyforge_project = 'personal_api_client'
|
16
|
+
s.homepage = ""
|
17
|
+
#["lib/oauth2_client.rb", "lib/personal_api_client.rb"]
|
18
|
+
end
|
19
|
+
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: personal_api_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adnan Karac
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: oauth2
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.8.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.8.0
|
30
|
+
description: Personal library for easier interaction with Personal API
|
31
|
+
email: adnan@personal.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- .gitignore
|
37
|
+
- README.md
|
38
|
+
- lib/oauth2_client.rb
|
39
|
+
- lib/personal_api_client.rb
|
40
|
+
- lib/personal_api_client/version.rb
|
41
|
+
- personal_api_client.gemspec
|
42
|
+
homepage: ''
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project: personal_api_client
|
62
|
+
rubygems_version: 1.8.23
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Personal API Client
|
66
|
+
test_files: []
|