kynetx_am_api 0.1.2
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/LICENSE +21 -0
- data/Rakefile +2 -0
- data/init.rb +1 -0
- data/lib/kynetx_am_api.rb +41 -0
- data/lib/kynetx_am_api/application.rb +228 -0
- data/lib/kynetx_am_api/direct_api.rb +210 -0
- data/lib/kynetx_am_api/oauth.rb +91 -0
- data/lib/kynetx_am_api/user.rb +125 -0
- data/rails/init.rb +1 -0
- metadata +117 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2010 Kynetx Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init.rb"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/kynetx_am_api/direct_api.rb'
|
2
|
+
require File.dirname(__FILE__) + '/kynetx_am_api/oauth.rb'
|
3
|
+
require File.dirname(__FILE__) + '/kynetx_am_api/user.rb'
|
4
|
+
require File.dirname(__FILE__) + '/kynetx_am_api/application.rb'
|
5
|
+
|
6
|
+
|
7
|
+
DEFAULT_META = <<-KRL
|
8
|
+
meta {
|
9
|
+
name "<<NAME>>"
|
10
|
+
description <<
|
11
|
+
<<DESCRIPTION>>
|
12
|
+
>>
|
13
|
+
author ""
|
14
|
+
// Uncomment this line to require Markeplace purchase to use this app.
|
15
|
+
// authz require user
|
16
|
+
logging off
|
17
|
+
}
|
18
|
+
KRL
|
19
|
+
|
20
|
+
DEFAULT_GLOBAL = <<-KRL
|
21
|
+
global {
|
22
|
+
|
23
|
+
}
|
24
|
+
KRL
|
25
|
+
|
26
|
+
DEFAULT_DISPATCH = <<-KRL
|
27
|
+
dispatch {
|
28
|
+
// Some example dispatch domains
|
29
|
+
// www.exmple.com
|
30
|
+
// other.example.com
|
31
|
+
}
|
32
|
+
KRL
|
33
|
+
|
34
|
+
DEFAULT_RULE = <<-KRL
|
35
|
+
rule <<NAME>> is active {
|
36
|
+
select using "" setting ()
|
37
|
+
// pre { }
|
38
|
+
// notify("Hello World", "This is a sample rule.");
|
39
|
+
noop();
|
40
|
+
}
|
41
|
+
KRL
|
@@ -0,0 +1,228 @@
|
|
1
|
+
module KynetxAmApi
|
2
|
+
class Application
|
3
|
+
|
4
|
+
attr_reader :name
|
5
|
+
attr_reader :user
|
6
|
+
attr_reader :application_id
|
7
|
+
attr_reader :api
|
8
|
+
|
9
|
+
def initialize(user, application_id, version=nil)
|
10
|
+
@api = user.api
|
11
|
+
@application_id = application_id
|
12
|
+
@user = user
|
13
|
+
load_base
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_param
|
17
|
+
return @application_id
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_initial_app(name, description)
|
21
|
+
@name = name
|
22
|
+
@description = description
|
23
|
+
@meta = DEFAULT_META.gsub("<<NAME>>", name).gsub("<<DESCRIPTION>>", description)
|
24
|
+
@global = DEFAULT_GLOBAL
|
25
|
+
@dispatch = DEFAULT_DISPATCH
|
26
|
+
@first_rule = DEFAULT_RULE.gsub("<<NAME>>", "first_rule")
|
27
|
+
set_krl(gen_default_krl)
|
28
|
+
load_base
|
29
|
+
return self
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete
|
33
|
+
@api.post_app_delete(@application_id)
|
34
|
+
end
|
35
|
+
|
36
|
+
def krl(v=nil)
|
37
|
+
@krl ||= @api.get_app_source(@application_id, v ? v : "development", :krl)
|
38
|
+
end
|
39
|
+
|
40
|
+
def image_url(size="normal")
|
41
|
+
# returns the image url of a given size
|
42
|
+
load_base unless @images
|
43
|
+
if @images.empty?
|
44
|
+
defaults = {
|
45
|
+
"thumb" => "http://appresource.s3.amazonaws.com/apiappimages/missing_thumb.png",
|
46
|
+
"normal" => "http://appresource.s3.amazonaws.com/apiappimages/missing.png",
|
47
|
+
"original" => "http://appresource.s3.amazonaws.com/apiappimages/missing.png",
|
48
|
+
"icard" => "http://appresource.s3.amazonaws.com/apiappimages/missing_icard.jpg"
|
49
|
+
}
|
50
|
+
return defaults[size.to_s]
|
51
|
+
end
|
52
|
+
return @images[size.to_s]
|
53
|
+
end
|
54
|
+
|
55
|
+
def set_image(filename, content_type, image_data)
|
56
|
+
return @api.post_app_updateappimage(@application_id, filename, content_type, image_data)
|
57
|
+
end
|
58
|
+
|
59
|
+
def krl=(krl)
|
60
|
+
set_krl(krl)
|
61
|
+
end
|
62
|
+
|
63
|
+
def users
|
64
|
+
load_base unless @users
|
65
|
+
return @users
|
66
|
+
end
|
67
|
+
|
68
|
+
def remove_user(userid)
|
69
|
+
return @api.post_remove_user(@application_id, userid)
|
70
|
+
end
|
71
|
+
|
72
|
+
def version
|
73
|
+
return development_version
|
74
|
+
end
|
75
|
+
|
76
|
+
def development_version
|
77
|
+
load_versions unless @development_version
|
78
|
+
return @development_version
|
79
|
+
end
|
80
|
+
|
81
|
+
def production_version
|
82
|
+
load_versions unless @production_version
|
83
|
+
return @production_version
|
84
|
+
end
|
85
|
+
|
86
|
+
def production_version=(version)
|
87
|
+
@api.post_app_setproductversion(@application_id, version)
|
88
|
+
@production_version = version
|
89
|
+
end
|
90
|
+
|
91
|
+
def versions
|
92
|
+
load_versions unless @versions
|
93
|
+
return @versions
|
94
|
+
end
|
95
|
+
|
96
|
+
def set_version_note(version, note)
|
97
|
+
@api.post_app_setversionnote(@application_id, version, note)
|
98
|
+
end
|
99
|
+
|
100
|
+
def owner
|
101
|
+
load_base unless @owner
|
102
|
+
return @owner
|
103
|
+
end
|
104
|
+
|
105
|
+
def transfer_request
|
106
|
+
load_base unless @transfer_request
|
107
|
+
return @transfer_request
|
108
|
+
end
|
109
|
+
|
110
|
+
def transfer_owner(user_id, message)
|
111
|
+
load_base unless @name
|
112
|
+
return @api.post_app_transferownershiprequest(@application_id, @name, user_id, message)
|
113
|
+
end
|
114
|
+
|
115
|
+
def cancel_transfer(request_id)
|
116
|
+
return @api.post_app_ownershiptransfercancel(@application_id, request_id)
|
117
|
+
end
|
118
|
+
|
119
|
+
def share(email, message)
|
120
|
+
load_base unless @name
|
121
|
+
return @api.post_app_inviteuser(@application_id, @name, email, message)
|
122
|
+
end
|
123
|
+
|
124
|
+
def invites
|
125
|
+
load_base unless @invites
|
126
|
+
return @invites
|
127
|
+
end
|
128
|
+
|
129
|
+
def cancel_invite(invite_id)
|
130
|
+
return @api.post_app_invitecancel(@application_id, invite_id)
|
131
|
+
end
|
132
|
+
|
133
|
+
def reload
|
134
|
+
load_base
|
135
|
+
load_versions
|
136
|
+
end
|
137
|
+
|
138
|
+
#----- Distrubution Methods
|
139
|
+
|
140
|
+
def bookmarklet(env="prod", runtime="init.kobj.net/js/shared/kobj-static.js")
|
141
|
+
return @api.post_app_generate(@application_id, "bookmarklet", {"env" => env, "runtime" => runtime})["data"]
|
142
|
+
end
|
143
|
+
|
144
|
+
def infocard(name, datasets, env="prod")
|
145
|
+
load_base unless @guid
|
146
|
+
options = {
|
147
|
+
"extname" => name.gsub(/[&'<]/, "_"),
|
148
|
+
"extdatasets" => datasets.to_s,
|
149
|
+
"extversion" => env
|
150
|
+
}
|
151
|
+
|
152
|
+
options["image_url"] = image_url("icard")
|
153
|
+
|
154
|
+
return @api.post_app_generate(@application_id, "info_card", options)
|
155
|
+
end
|
156
|
+
|
157
|
+
def extension(type, name, author, description)
|
158
|
+
options = {
|
159
|
+
"extname" => name,
|
160
|
+
"extdesc" => description,
|
161
|
+
"extauthor" => author.to_s.empty? ? @user.name : author
|
162
|
+
}
|
163
|
+
options["appguid"] = @guid if type == :ie
|
164
|
+
return @api.post_app_generate(@application_id, type.to_s, options)
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
private
|
169
|
+
|
170
|
+
def gen_default_krl
|
171
|
+
r = "ruleset #{@application_id} {\n#{@meta}\n#{@dispatch}\n#{@global}\n#{@first_rule}}"
|
172
|
+
return r
|
173
|
+
end
|
174
|
+
|
175
|
+
def load_base
|
176
|
+
app_details = @api.get_app_details(@application_id)
|
177
|
+
puts "APPDETAILS: #{app_details.inspect}" if $DEBUG
|
178
|
+
if app_details["error"]
|
179
|
+
raise app_details["error"]
|
180
|
+
end
|
181
|
+
@name = app_details["name"]
|
182
|
+
@application_id = app_details["appid"]
|
183
|
+
@guid = app_details["guid"]
|
184
|
+
@owner = nil
|
185
|
+
@users = app_details["users"]
|
186
|
+
@users.each do |user|
|
187
|
+
if user["role"] == "owner"
|
188
|
+
@owner = user
|
189
|
+
break
|
190
|
+
end
|
191
|
+
end
|
192
|
+
@transfer_request = app_details["transferrequest"]
|
193
|
+
@transfer_request = nil unless @transfer_request
|
194
|
+
@invites = app_details["invites"]
|
195
|
+
@invites = [] unless @invites
|
196
|
+
@images = app_details["images"]
|
197
|
+
@images = [] unless @images
|
198
|
+
end
|
199
|
+
|
200
|
+
|
201
|
+
def load_versions
|
202
|
+
app_info = @api.get_app_info(@application_id)
|
203
|
+
puts "APPINFO: #{app_info.inspect}" if $DEBUG
|
204
|
+
@production_version = app_info["production"]["version"] if app_info["production"]
|
205
|
+
@development_version = app_info["development"]["version"] if app_info["development"]
|
206
|
+
@application_id = app_info["appid"]
|
207
|
+
@versions = app_info["versions"]
|
208
|
+
end
|
209
|
+
|
210
|
+
def set_krl(krl)
|
211
|
+
# ensure that the ruleset_id is correct.
|
212
|
+
krl.gsub!(/ruleset.*?\{/m, "ruleset #{@application_id} {")
|
213
|
+
puts "NEW KRL: #{krl}" if $DEBUG
|
214
|
+
response = @api.post_app_source(@application_id, krl, "krl")
|
215
|
+
response = JSON.parse(response)
|
216
|
+
if response["valid"]
|
217
|
+
reload
|
218
|
+
return true
|
219
|
+
else
|
220
|
+
raise response["error"]
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
|
@@ -0,0 +1,210 @@
|
|
1
|
+
module KynetxAmApi
|
2
|
+
require 'rubygems'
|
3
|
+
require 'oauth'
|
4
|
+
require 'json'
|
5
|
+
require 'net/http/post/multipart'
|
6
|
+
|
7
|
+
class DirectApi
|
8
|
+
|
9
|
+
attr_accessor :oauth
|
10
|
+
|
11
|
+
#
|
12
|
+
# Create a new session to the KynetxApi Manager.
|
13
|
+
#
|
14
|
+
# request_token
|
15
|
+
# request_secret
|
16
|
+
# :request_token
|
17
|
+
# :request_secret
|
18
|
+
# :oauth_verifier
|
19
|
+
# :access_token
|
20
|
+
# :access_secret
|
21
|
+
#
|
22
|
+
def initialize(tokens_and_secrets = {})
|
23
|
+
@oauth = KynetxAmApi::Oauth.new(tokens_and_secrets)
|
24
|
+
end
|
25
|
+
|
26
|
+
#
|
27
|
+
def get_request_token
|
28
|
+
@oauth.get_request_token
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# Provides the url to direct user to if the application has not been authorized.
|
33
|
+
#
|
34
|
+
def get_authorize_url
|
35
|
+
return get_request_token.authorize_url
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
#
|
40
|
+
# API Call From Here Down.
|
41
|
+
#
|
42
|
+
|
43
|
+
#
|
44
|
+
#
|
45
|
+
def get_applist
|
46
|
+
return get_response("applist", :json)
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_appcreate
|
50
|
+
return get_response("appcreate", :json)
|
51
|
+
end
|
52
|
+
|
53
|
+
def post_app_delete(application_id)
|
54
|
+
return post_response("app/#{application_id}/delete", {})
|
55
|
+
end
|
56
|
+
|
57
|
+
def post_remove_user(application_id, user_id)
|
58
|
+
return post_response("app/#{application_id}/removeuser", {"userid" => user_id})
|
59
|
+
end
|
60
|
+
|
61
|
+
def post_app_inviteuser(application_id, application_name, email, message)
|
62
|
+
return post_response("app/#{application_id}/inviteuser", {"email" => email, "message" => message, "appname" => application_name.to_s})
|
63
|
+
end
|
64
|
+
|
65
|
+
def post_app_invitecancel(application_id, invite_id)
|
66
|
+
return post_response("app/#{application_id}/invitecancel",{"inviteid" => invite_id})
|
67
|
+
end
|
68
|
+
|
69
|
+
def post_app_transferownershiprequest(application_id,application_name,user_id,message)
|
70
|
+
return post_response("app/#{application_id}/ownershiptransferrequest", {"userid" => user_id, "message" => message, "appname" => application_name})
|
71
|
+
end
|
72
|
+
|
73
|
+
def post_app_ownershiptransfercancel(application_id,transfer_id)
|
74
|
+
return post_response("app/#{application_id}/ownershiptransfercancel", {"ownershiptransferrequestid" => transfer_id})
|
75
|
+
end
|
76
|
+
|
77
|
+
#
|
78
|
+
# type is one of ff = firefox, ie = internet explorer, cr = google chrome
|
79
|
+
#
|
80
|
+
def post_app_generate(application_id, type, opts={})
|
81
|
+
#
|
82
|
+
# default_options = {
|
83
|
+
# "name" => "",
|
84
|
+
# "author" => "",
|
85
|
+
# "description" => "",
|
86
|
+
# "guid" => "",
|
87
|
+
# "datasets" => "",
|
88
|
+
# "env" => "prod",
|
89
|
+
# "image_url" => "http://appresource.s3.amazonaws.com/apiappimages/missing_icard.jpg",
|
90
|
+
# "runtime" => ""
|
91
|
+
# }
|
92
|
+
# options = default_options.merge(opts)
|
93
|
+
return post_response("app/#{application_id}/generate/#{type}", opts,:json)
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
def get_app_source(application_id, version, format = :krl)
|
98
|
+
data = get_response("app/#{application_id}/source/#{version}/#{format}", format)
|
99
|
+
return data
|
100
|
+
end
|
101
|
+
|
102
|
+
def get_app_info(application_id)
|
103
|
+
return get_response("app/#{application_id}/info", :json)
|
104
|
+
end
|
105
|
+
|
106
|
+
def get_app_details(application_id)
|
107
|
+
return get_response("app/#{application_id}/details", :json)
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
def post_app_source(application_id, source, type = "krl")
|
112
|
+
data = ""
|
113
|
+
if type == "krl"
|
114
|
+
data = {:krl => source.to_s}
|
115
|
+
else
|
116
|
+
data = {:json => source.to_json}
|
117
|
+
end
|
118
|
+
return post_response("app/#{application_id}/source", data)
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
def post_app_setproductversion(application_id, version)
|
123
|
+
return post_response("app/#{application_id}/setproductionversion", {"version" => version})
|
124
|
+
end
|
125
|
+
|
126
|
+
def post_app_setversionnote(application_id, version, note)
|
127
|
+
return post_response("app/#{application_id}/setversionnote", {"version" => version, "note" => note}, :json)
|
128
|
+
end
|
129
|
+
|
130
|
+
def post_app_updateappimage(application_id, filename, content_type, image_data)
|
131
|
+
# TODO: Make this go through the APIß
|
132
|
+
response = ""
|
133
|
+
url = URI.parse('https://accounts.kynetx.com/api/0.1/updateAppInfo')
|
134
|
+
StringIO.open(image_data) do |i|
|
135
|
+
req = Net::HTTP::Post::Multipart.new url.path,
|
136
|
+
"image" => UploadIO.new(i, content_type, filename),
|
137
|
+
"appid" => application_id
|
138
|
+
http = Net::HTTP.new(url.host, url.port)
|
139
|
+
http.use_ssl = true
|
140
|
+
response = http.start { |h| h.request(req) }
|
141
|
+
end
|
142
|
+
|
143
|
+
return response
|
144
|
+
end
|
145
|
+
|
146
|
+
def ping
|
147
|
+
return get_response("ping")
|
148
|
+
end
|
149
|
+
|
150
|
+
def get_user_info
|
151
|
+
user = @oauth.user
|
152
|
+
if user.username.to_s.empty?
|
153
|
+
user_info = get_response("userinfo", :json)
|
154
|
+
user.username = user_info["username"]
|
155
|
+
user.userid = user_info["userid"]
|
156
|
+
user.name = user_info["name"]
|
157
|
+
end
|
158
|
+
return user
|
159
|
+
end
|
160
|
+
|
161
|
+
private
|
162
|
+
|
163
|
+
def get_response(api_method, format = nil)
|
164
|
+
if format == :json
|
165
|
+
headers = {'Accept'=>'application/json'}
|
166
|
+
end
|
167
|
+
api_call = "/0.1/#{api_method}"
|
168
|
+
puts "---------GET---------------" if $DEBUG
|
169
|
+
puts api_call if $DEBUG
|
170
|
+
puts "___________________________" if $DEBUG
|
171
|
+
response = @oauth.get_access_token.get(api_call, headers).body
|
172
|
+
puts response.inspect if $DEBUG
|
173
|
+
puts "___________________________" if $DEBUG
|
174
|
+
begin
|
175
|
+
response = JSON.parse(response) if format == :json
|
176
|
+
rescue
|
177
|
+
puts $! if $DEBUG
|
178
|
+
raise "Unexpected response from the api: (#{api_method}) :: #{response}"
|
179
|
+
end
|
180
|
+
return response
|
181
|
+
end
|
182
|
+
|
183
|
+
# Probably a better way to do this. Make it a little more DRY
|
184
|
+
def post_response(api_method, data, format=nil, additional_headers=nil)
|
185
|
+
if format == :json
|
186
|
+
headers = {'Accept'=>'application/json'}
|
187
|
+
end
|
188
|
+
if additional_headers
|
189
|
+
headers.merge!(additional_headers)
|
190
|
+
end
|
191
|
+
api_call = "/0.1/#{api_method}"
|
192
|
+
puts "---------POST--------------" if $DEBUG
|
193
|
+
puts api_call if $DEBUG
|
194
|
+
puts data.inspect if $DEBUG
|
195
|
+
puts "___________________________" if $DEBUG
|
196
|
+
response = @oauth.get_access_token.post(api_call, data, headers).body
|
197
|
+
puts response.inspect if $DEBUG
|
198
|
+
puts "---------------------------" if $DEBUG
|
199
|
+
begin
|
200
|
+
response = JSON.parse(response) if format == :json
|
201
|
+
rescue
|
202
|
+
puts $! if $DEBUG
|
203
|
+
raise "Unexpected response from the api: (#{api_method}) :: #{response}"
|
204
|
+
end
|
205
|
+
return response
|
206
|
+
end
|
207
|
+
|
208
|
+
|
209
|
+
end
|
210
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module KynetxAmApi
|
2
|
+
require 'oauth'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
class Oauth
|
6
|
+
|
7
|
+
attr_accessor :request_token
|
8
|
+
attr_accessor :account_consumer
|
9
|
+
attr_accessor :api_consumer
|
10
|
+
attr_accessor :user
|
11
|
+
attr_accessor :access_token
|
12
|
+
|
13
|
+
|
14
|
+
def initialize(user=nil)
|
15
|
+
@user = KynetxAmApi::User.new(user)
|
16
|
+
if @user.oauth_verifier
|
17
|
+
access_tokens
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_access_token
|
22
|
+
return @access_token if @access_token
|
23
|
+
return @access_token = OAuth::AccessToken.new(get_api_consumer, @user.access_token, @user.access_secret)
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_request_token
|
27
|
+
return @request_token if @request_token
|
28
|
+
return @request_token = get_account_consumer.get_request_token
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def self.accounts_server_url=(v)
|
33
|
+
@@accounts_server_url = v
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.api_server_url=(v)
|
37
|
+
@@api_server_url = v
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.consumer_key=(v)
|
41
|
+
@@consumer_key = v
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.consumer_secret=(v)
|
45
|
+
@@consumer_secret = v
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
|
51
|
+
def access_tokens
|
52
|
+
access_request_token = OAuth::RequestToken.new(get_account_consumer, @user.request_token, @user.request_secret)
|
53
|
+
access_token_data = access_request_token.get_access_token :oauth_verifier => @user.oauth_verifier
|
54
|
+
@user.access_token = access_token_data.token
|
55
|
+
@user.access_secret = access_token_data.secret
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_account_consumer
|
60
|
+
return @account_consumer if @account_consumer
|
61
|
+
return @account_consumer = OAuth::Consumer.new(@@consumer_key, @@consumer_secret, {
|
62
|
+
:site => @@accounts_server_url,
|
63
|
+
:scheme => :header,
|
64
|
+
:method => :get,
|
65
|
+
:request_token_path => "/oauth/request_token",
|
66
|
+
:access_token_path => "/oauth/access_token",
|
67
|
+
:authorize_path => "/oauth/authorize",
|
68
|
+
:oauth_version => "1.0a"
|
69
|
+
})
|
70
|
+
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
def get_api_consumer
|
76
|
+
return @api_consumer if @api_consumer
|
77
|
+
return @api_consumer = OAuth::Consumer.new(@@consumer_key, @@consumer_secret, {
|
78
|
+
:site => @@api_server_url,
|
79
|
+
:scheme => :header,
|
80
|
+
:method => :get,
|
81
|
+
:request_token_path => "/oauth/request_token",
|
82
|
+
:access_token_path => "/oauth/access_token",
|
83
|
+
:authorize_path => "/oauth/authorize",
|
84
|
+
:oauth_version => "1.0a"
|
85
|
+
})
|
86
|
+
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
module KynetxAmApi
|
2
|
+
#
|
3
|
+
# Simple wrapper to allow access to the OAuth user information. This also hold some basic user data like
|
4
|
+
# username, name and user id.
|
5
|
+
#
|
6
|
+
class User
|
7
|
+
# OAuth Request Token
|
8
|
+
attr_accessor :request_token
|
9
|
+
# OAuth Secret Token
|
10
|
+
attr_accessor :request_secret
|
11
|
+
# OAuth Verifieer
|
12
|
+
attr_accessor :oauth_verifier
|
13
|
+
# OAuth Access Token
|
14
|
+
attr_accessor :access_token
|
15
|
+
# OAuth Access Secret
|
16
|
+
attr_accessor :access_secret
|
17
|
+
# Kynetx User name
|
18
|
+
attr_accessor :username
|
19
|
+
# Kynetx User ID
|
20
|
+
attr_accessor :userid
|
21
|
+
# Full name of user
|
22
|
+
attr_accessor :name
|
23
|
+
# Current Application context.
|
24
|
+
attr_reader :current_application
|
25
|
+
|
26
|
+
|
27
|
+
#
|
28
|
+
# Accepts a hash that has the following entries.
|
29
|
+
# - :request_token
|
30
|
+
# - :request_secret
|
31
|
+
# - :oauth_verifier
|
32
|
+
# - :access_token
|
33
|
+
# - :access_secret
|
34
|
+
# - :username
|
35
|
+
# - :userid
|
36
|
+
# - :name
|
37
|
+
#
|
38
|
+
|
39
|
+
def initialize(attributes)
|
40
|
+
@request_token = attributes[:request_token]
|
41
|
+
@request_secret = attributes[:request_secret]
|
42
|
+
@oauth_verifier = attributes[:oauth_verifier]
|
43
|
+
@access_token = attributes[:access_token]
|
44
|
+
@access_secret = attributes[:access_secret]
|
45
|
+
@username = attributes[:username]
|
46
|
+
@userid = attributes[:userid]
|
47
|
+
@name = attributes[:name]
|
48
|
+
@current_applicaion = nil
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Returns the direct api to the Kynetx Application Manager.
|
53
|
+
#
|
54
|
+
def api
|
55
|
+
@api ||= KynetxAmApi::DirectApi.new({:access_token => @access_token, :access_secret => @access_secret})
|
56
|
+
return @api
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# Read applications list
|
61
|
+
#
|
62
|
+
# - :offset => Start in list (not implemented)
|
63
|
+
# - :size => Number of application to list (not implemented)
|
64
|
+
#
|
65
|
+
# Returns a has with two keys
|
66
|
+
# - "apps" => Array Off Hashes with :appid , :role, :name, :created
|
67
|
+
# - "valid" => true
|
68
|
+
#
|
69
|
+
def applications(options = {})
|
70
|
+
@applications = api.get_applist if !@applications
|
71
|
+
@applications
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# - :application_id => application_id
|
76
|
+
# - :version => Version of application to obtain
|
77
|
+
#
|
78
|
+
def find_application(options = {})
|
79
|
+
options[:version] ||= "development"
|
80
|
+
raise "Expecting :application_id" unless options[:application_id]
|
81
|
+
|
82
|
+
if @current_application && @current_application.application_id != options[:application_id]
|
83
|
+
@current_application = KynetxAmApi::Application.new(self, options[:application_id], options[:version])
|
84
|
+
else
|
85
|
+
@current_application ||= KynetxAmApi::Application.new(self, options[:application_id], options[:version])
|
86
|
+
end
|
87
|
+
return @current_application
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
def create_application(name, description="")
|
92
|
+
appid = api.get_appcreate["appid"]
|
93
|
+
@current_application = KynetxAmApi::Application.new(self, appid).create_initial_app(name, description)
|
94
|
+
|
95
|
+
return @current_application
|
96
|
+
end
|
97
|
+
|
98
|
+
def duplicate_application(application_id)
|
99
|
+
old_app = KynetxAmApi::Application.new(self, application_id)
|
100
|
+
new_app = create_application(old_app.name || "", "")
|
101
|
+
new_app.krl = old_app.krl
|
102
|
+
return new_app
|
103
|
+
end
|
104
|
+
|
105
|
+
def owns_current?
|
106
|
+
return false unless @current_application
|
107
|
+
return @current_application.owner["kynetxuserid"].to_i == self.userid.to_i
|
108
|
+
end
|
109
|
+
|
110
|
+
def to_h
|
111
|
+
return {
|
112
|
+
:access_secret => @access_secret,
|
113
|
+
:access_token => @access_token,
|
114
|
+
:request_token => @request_token,
|
115
|
+
:request_secret => @request_secret,
|
116
|
+
:oauth_verifier => @oauth_verifier,
|
117
|
+
:name => @name,
|
118
|
+
:userid => @userid,
|
119
|
+
:username => @username
|
120
|
+
}
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
end
|
125
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../lib/kynetx_am_api.rb"
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kynetx_am_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Michael Farmer, Cid Dennis
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-03 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: json
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: oauth
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :runtime
|
43
|
+
version_requirements: *id002
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: multipart-post
|
46
|
+
prerelease: false
|
47
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
type: :runtime
|
55
|
+
version_requirements: *id003
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec
|
58
|
+
prerelease: false
|
59
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
type: :development
|
67
|
+
version_requirements: *id004
|
68
|
+
description: " Using this gem you can access the Kynetx Application API. This api allows developers a way\n to create,update, delete rulesets. This gem also helps with the required OAuth communcations with\n the api server.\n"
|
69
|
+
email: cid@kynetx.com
|
70
|
+
executables: []
|
71
|
+
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
extra_rdoc_files:
|
75
|
+
- LICENSE
|
76
|
+
files:
|
77
|
+
- lib/kynetx_am_api/application.rb
|
78
|
+
- lib/kynetx_am_api/direct_api.rb
|
79
|
+
- lib/kynetx_am_api/oauth.rb
|
80
|
+
- lib/kynetx_am_api/user.rb
|
81
|
+
- lib/kynetx_am_api.rb
|
82
|
+
- rails/init.rb
|
83
|
+
- init.rb
|
84
|
+
- Rakefile
|
85
|
+
- LICENSE
|
86
|
+
has_rdoc: true
|
87
|
+
homepage: http://www.kynetx.com
|
88
|
+
licenses: []
|
89
|
+
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options:
|
92
|
+
- --charset=UTF-8
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
requirements: []
|
110
|
+
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 1.3.6
|
113
|
+
signing_key:
|
114
|
+
specification_version: 3
|
115
|
+
summary: Simple Gem used to communicate with the Kynetx Application Manager.
|
116
|
+
test_files: []
|
117
|
+
|