sk_sdk 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +5 -14
- data/VERSION +1 -1
- data/lib/sk_sdk/README_Base.rdoc +120 -0
- data/lib/sk_sdk/ar_cli.rb +3 -38
- data/lib/sk_sdk/base.rb +45 -0
- data/lib/sk_sdk/oauth.rb +17 -9
- data/lib/sk_sdk/omni_auth/README.rdoc +41 -0
- data/lib/sk_sdk/omni_auth/salesking.rb +71 -0
- data/lib/sk_sdk.rb +1 -1
- data/sk_sdk.gemspec +8 -3
- data/spec/resources_spec_helper.rb +6 -13
- data/spec/sk_sdk/base_spec.rb +38 -0
- data/spec/spec_helper.rb +2 -1
- metadata +10 -5
- data/lib/sk_sdk/README_ArCli.rdoc +0 -84
data/README.rdoc
CHANGED
@@ -6,7 +6,7 @@ and the API.
|
|
6
6
|
|
7
7
|
== Install
|
8
8
|
|
9
|
-
|
9
|
+
gem install sk_sdk
|
10
10
|
|
11
11
|
Dependencies (gem's):
|
12
12
|
|
@@ -20,8 +20,11 @@ All classes must be explicitly required so each can be used on their own.
|
|
20
20
|
|
21
21
|
require 'sk_sdk/signed_request'
|
22
22
|
require 'sk_sdk/oauth'
|
23
|
-
require 'sk_sdk/
|
23
|
+
require 'sk_sdk/base'
|
24
24
|
|
25
|
+
=== API client
|
26
|
+
|
27
|
+
Uses ActiveResource to CRUD SalesKing object's {see usage in README}[https://github.com/salesking/sk_sdk/blob/master/lib/sk_sdk/README_Base.rdoc]
|
25
28
|
|
26
29
|
=== oAuth
|
27
30
|
|
@@ -32,21 +35,9 @@ Handling oAuth related URL's and getting an access token
|
|
32
35
|
Helping in de/encoding of signed_request parameter available in canvas pages and
|
33
36
|
PubSub/Webhook callbacks.
|
34
37
|
|
35
|
-
=== API client
|
36
|
-
|
37
|
-
Create classes out if thin air to CRUD SalesKing object's using activeresource
|
38
|
-
{see README}[https://github.com/salesking/sk_sdk/blob/master/lib/sk_sdk/README_ArCli.rdoc]
|
39
|
-
|
40
|
-
This client uses HTTPBasic Auth with email/password and does NOT use oAuth. This
|
41
|
-
authentication method might be kicked in the future, as oAuth2 is considered
|
42
|
-
safer.
|
43
|
-
If you have found a convenient way to enable ActiveResource with oAuth2 we are
|
44
|
-
glad to hear from you.
|
45
|
-
|
46
38
|
== Usage
|
47
39
|
|
48
40
|
Read specs: https://github.com/salesking/sk_sdk/tree/master/spec/sk_sdk
|
49
41
|
|
50
42
|
|
51
|
-
|
52
43
|
Copyright (c) 2011 Georg Leciejewski, released under the MIT license
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6
|
@@ -0,0 +1,120 @@
|
|
1
|
+
= SalesKing API Client
|
2
|
+
|
3
|
+
ActiveResource based SalesKing API client.
|
4
|
+
This does NOT rely on parsing the JSON Schema, since ActiveResource creates the
|
5
|
+
Getter/Setter methods(less restrict). The client also adds some patches to AR to
|
6
|
+
fix json parsing issues.
|
7
|
+
|
8
|
+
== Install
|
9
|
+
|
10
|
+
gem install sk_sdk
|
11
|
+
|
12
|
+
If you are using bundler add to Gemfile
|
13
|
+
|
14
|
+
gem "sk_sdk"
|
15
|
+
# or directly require a used class
|
16
|
+
gem "sk_sdk", :require => "sk_sdk/base"
|
17
|
+
|
18
|
+
Dependencies (gem's):
|
19
|
+
|
20
|
+
* activeresupport
|
21
|
+
* activeresource
|
22
|
+
|
23
|
+
== Authorization
|
24
|
+
|
25
|
+
This client should be used with an oAuth2 access_token, or as long as we support
|
26
|
+
it can use HTTBasic Auth with username and password. For a quick start BasicAuth
|
27
|
+
is definitly easier, but it is less secure since you cannot track which client
|
28
|
+
is accessing your account and the client has the full rights of the user. So if
|
29
|
+
someone gets your credentials he can log into your SalesKing account and do
|
30
|
+
whatever you can!
|
31
|
+
|
32
|
+
Getting an access_token or checking is validity is not the scope of this
|
33
|
+
library, it merely sets the AUTHORIZATION header field to
|
34
|
+
AUTHORIZATION: Bearer YourAccessToken
|
35
|
+
See our {oAuth helper class}[https://github.com/salesking/sk_sdk/blob/master/lib/sk_sdk/oauth.rb]
|
36
|
+
and read our {oAuth docs}[http://dev.blog.salesking.eu/docs/authentication/].
|
37
|
+
|
38
|
+
== Usage
|
39
|
+
|
40
|
+
SalesKing's api interface is RESTful(mostly) and returns & accepts JSON data.
|
41
|
+
All resources such as clients, invoices, products can be accessed via URL's
|
42
|
+
through standard HTTP methods GET, POST, PUT and DELETE.
|
43
|
+
see available objects and endpoints here:
|
44
|
+
https://github.com/salesking/sk_api_schema/tree/master/json
|
45
|
+
|
46
|
+
First create the classes, which simply need to descend from SK::SDK::Base
|
47
|
+
There are several ways to do so:
|
48
|
+
|
49
|
+
require 'sk_sdk/base'
|
50
|
+
# simple object creating
|
51
|
+
class Client < SK::SDK::Base;end
|
52
|
+
|
53
|
+
# create objects in custom King namespace
|
54
|
+
module; King; end
|
55
|
+
%w[Client Invoice Product LineItem].each do |model|
|
56
|
+
eval "class King::#{model} < SK::SDK::Base;end"
|
57
|
+
end
|
58
|
+
|
59
|
+
# add your connection settings
|
60
|
+
SK::SDK::Base.set_connection {:site => 'https://my_sub.salesking.eu/api',
|
61
|
+
:user => 'my-users@login-email.com',
|
62
|
+
:password => 'password' })
|
63
|
+
#OR Use an oAuth access token
|
64
|
+
#SK::SDK::Base.set_connection {:site => 'https://my_sub.salesking.eu/api',
|
65
|
+
# :token => 'someAccessToken'})
|
66
|
+
|
67
|
+
# use it
|
68
|
+
client = Client.new(:last_name=> 'Meister')
|
69
|
+
client.first_name = "Bau"
|
70
|
+
client.save
|
71
|
+
credit_note = King::CreditNote.new
|
72
|
+
|
73
|
+
You MUST provide the full url using the right protocol when you set the
|
74
|
+
connection and remember our production system only supports HTTPS:
|
75
|
+
http/https + SUBDomain + salesking url + /api
|
76
|
+
|
77
|
+
The old ArCli.make method to create the classes, still exists but is deprecated:
|
78
|
+
|
79
|
+
require "sk_sdk/ar_cli"
|
80
|
+
SK::SDK::ArCli.make(:client)
|
81
|
+
|
82
|
+
# OR create a class within a namespace
|
83
|
+
module; King; end
|
84
|
+
SK::SDK::ArCli.make(:credit_note, King)
|
85
|
+
|
86
|
+
|
87
|
+
Want to know more about REST style webservices?
|
88
|
+
* http://en.wikipedia.org/wiki/Representational_State_Transfer
|
89
|
+
* http://www.google.com/search?q=REST+site%3Awww.infoq.com
|
90
|
+
|
91
|
+
=== Authentification & Safety
|
92
|
+
|
93
|
+
Authentification can be done with oAuth2 but we still support HTTP BasicAuth
|
94
|
+
using your SalesKing login email and password. oAuth examples(support) will
|
95
|
+
follow.
|
96
|
+
|
97
|
+
For a production environment be advised to create a user, per api client, and
|
98
|
+
restrict his rights with our build in role-system!
|
99
|
+
|
100
|
+
|
101
|
+
=== API Tutorial and Tools
|
102
|
+
|
103
|
+
Since browsers do not support PUT/DELETE methods you can use CURL, a linux
|
104
|
+
command-line http client, for testing. And of course any http library supporting
|
105
|
+
http-basic-auth can be used.
|
106
|
+
|
107
|
+
* Getting started: http://dev.blog.salesking.eu/api/
|
108
|
+
* ActiveResource readme: https://github.com/rails/rails/tree/master/activeresource
|
109
|
+
* Chrome cRest extension https://chrome.google.com/extensions/detail/baedhhmoaooldchehjhlpppaieoglhml
|
110
|
+
* Poster FF-Plugin - make HTTP request https://addons.mozilla.org/en-US/firefox/addon/2691/ (you must be logged into SalesKing)
|
111
|
+
* JSONView FF-Plugin - view json in firefox https://addons.mozilla.org/de/firefox/addon/10869/
|
112
|
+
* JSONovich FF-Plugin - https://addons.mozilla.org/de/firefox/addon/10122/
|
113
|
+
|
114
|
+
== Tests / Specs
|
115
|
+
|
116
|
+
Take a look into spec/resources to see a how-to
|
117
|
+
|
118
|
+
rake coverage
|
119
|
+
|
120
|
+
Copyright (c) 2011 Georg Leciejewski, released under the MIT license
|
data/lib/sk_sdk/ar_cli.rb
CHANGED
@@ -1,18 +1,9 @@
|
|
1
|
-
require 'rubygems'
|
2
1
|
require 'sk_sdk'
|
3
|
-
require '
|
4
|
-
require 'active_resource/version'
|
5
|
-
# patches are for specific AR version
|
6
|
-
if ActiveResource::VERSION::MAJOR == 3
|
7
|
-
require 'sk_sdk/ar_cli/patches/ar3/base'
|
8
|
-
require 'sk_sdk/ar_cli/patches/ar3/validations'
|
9
|
-
elsif ActiveResource::VERSION::MAJOR < 3
|
10
|
-
require 'sk_sdk/ar_cli/patches/ar2/validations'
|
11
|
-
require 'sk_sdk/ar_cli/patches/ar2/base'
|
12
|
-
end
|
2
|
+
require 'sk_sdk/base'
|
13
3
|
|
14
4
|
module SK::SDK
|
15
5
|
class ArCli
|
6
|
+
# TODO deprecated
|
16
7
|
# Create a class for a given name
|
17
8
|
#
|
18
9
|
# === Example
|
@@ -36,33 +27,7 @@ module SK::SDK
|
|
36
27
|
raise "Constant #{class_name} already defined in scope of #{obj_scope}!" if obj_scope.const_defined?(class_name)
|
37
28
|
# create a new class from given name:
|
38
29
|
# :line_item => # class LineItem < ActiveResource::Base
|
39
|
-
|
40
|
-
klass.class_eval do
|
41
|
-
self.extend(ClassMethods)
|
42
|
-
self.send(:include, InstanceMethods) # include is private
|
43
|
-
end
|
44
|
-
klass.format = :json # bug in AR must be set here
|
30
|
+
obj_scope.const_set( class_name, Class.new(SK::SDK::Base) )
|
45
31
|
end
|
46
32
|
end
|
47
|
-
|
48
|
-
module ClassMethods
|
49
|
-
# Define the connection to be used when talking to a salesking server
|
50
|
-
def set_connection(opts)
|
51
|
-
self.site = opts[:site]
|
52
|
-
self.user = opts[:user]
|
53
|
-
self.password = opts[:password]
|
54
|
-
self.format = opts[:format].to_sym
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
module InstanceMethods
|
59
|
-
# hook before init in activeresource base because json comes in nested:
|
60
|
-
# {client={data}
|
61
|
-
def initialize(attributes = {})
|
62
|
-
attr = attributes[self.class.element_name] || attributes
|
63
|
-
super(attr)
|
64
|
-
end
|
65
|
-
|
66
|
-
def save; save_with_validation; end
|
67
|
-
end
|
68
33
|
end
|
data/lib/sk_sdk/base.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sk_sdk'
|
3
|
+
require 'active_resource'
|
4
|
+
require 'active_resource/version'
|
5
|
+
# patches are for specific AR version
|
6
|
+
if ActiveResource::VERSION::MAJOR == 3
|
7
|
+
require 'sk_sdk/ar_cli/patches/ar3/base'
|
8
|
+
require 'sk_sdk/ar_cli/patches/ar3/validations'
|
9
|
+
elsif ActiveResource::VERSION::MAJOR < 3
|
10
|
+
require 'sk_sdk/ar_cli/patches/ar2/validations'
|
11
|
+
require 'sk_sdk/ar_cli/patches/ar2/base'
|
12
|
+
end
|
13
|
+
|
14
|
+
class SK::SDK::Base < ActiveResource::Base
|
15
|
+
self.format = :json
|
16
|
+
# hook before init in activeresource base because json comes in nested:
|
17
|
+
# {client={data}
|
18
|
+
def initialize(attributes = {})
|
19
|
+
attr = attributes[self.class.element_name] || attributes
|
20
|
+
super(attr)
|
21
|
+
end
|
22
|
+
|
23
|
+
def save; save_with_validation; end
|
24
|
+
|
25
|
+
# Define the connection to be used when talking to a salesking server
|
26
|
+
# === Params
|
27
|
+
# opts<Hash{Symbol=>String}>:: options
|
28
|
+
# == opts
|
29
|
+
# :site<String>:: SalesKing Url, required
|
30
|
+
# :token<String>:: oAuth2 access token, will be added to the request header if
|
31
|
+
# set user/pass are not needed, so this is what you should be using!
|
32
|
+
# :user<String>:: if using httpBasic auth set to sk user login email
|
33
|
+
# :password<String>:: if using httpBasic sk user password
|
34
|
+
def self.set_connection(opts)
|
35
|
+
self.site = opts[:site]
|
36
|
+
self.format = :json # f*** xml
|
37
|
+
if opts[:token] #oAuth access token in header
|
38
|
+
self.headers['Authorization'] = "Bearer #{opts[:token]}"
|
39
|
+
else
|
40
|
+
self.user = opts[:user]
|
41
|
+
self.password = opts[:password]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/lib/sk_sdk/oauth.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'cgi'
|
2
2
|
require 'curb'
|
3
3
|
require 'sk_sdk'
|
4
|
+
|
4
5
|
module SK::SDK
|
5
6
|
# Authenticate your SalesKing App using oAuth2. This class provides helpers
|
6
7
|
# to create the token & dialog url and to get an access token
|
@@ -12,19 +13,24 @@ module SK::SDK
|
|
12
13
|
# Setup a new oAuth connection requires you to set some default:
|
13
14
|
# === Params
|
14
15
|
# opts<Hash{String=>String}>:: options for your app
|
16
|
+
#
|
15
17
|
# == Options(opts)
|
16
|
-
# id
|
17
|
-
# secret
|
18
|
-
# scope
|
19
|
-
# redirect_url
|
18
|
+
# id<String>:: oAuth app id from SalesKing app registration
|
19
|
+
# secret<String>:: oAuth app secret from SalesKing app registration
|
20
|
+
# scope<String>:: permission your app requests
|
21
|
+
# redirect_url<String>:: redirect url inside your app for auth dialog
|
22
|
+
# sk_url<String>:: SalesKing base url, * is replaced with users subdomain,
|
23
|
+
# default https://*.salesking.eu, optional
|
24
|
+
# sub_domain<String>:: optinal, will probably be set later after a users
|
25
|
+
# provided his subdomain
|
20
26
|
def initialize(opts)
|
21
27
|
@id = opts['id']
|
22
28
|
@secret = opts['secret']
|
23
29
|
@scope = opts['scope']
|
24
30
|
@redirect_url = opts['redirect_url']
|
25
31
|
@canvas_slug = opts['canvas_slug']
|
26
|
-
@sk_url
|
27
|
-
@sub_domain
|
32
|
+
@sk_url = opts['sk_url'] || "https://*.salesking.eu"
|
33
|
+
@sub_domain = opts['sub_domain']
|
28
34
|
end
|
29
35
|
|
30
36
|
# URL showing the auth dialog to the user
|
@@ -47,6 +53,8 @@ module SK::SDK
|
|
47
53
|
|
48
54
|
# URL to get the access_token, used in the second step after you have
|
49
55
|
# requested the authorization and gotten a code
|
56
|
+
# The token url is located at /oauth/token like proposed in draft oAuth2.16
|
57
|
+
# but can still be reached at /access_token so older libs still work
|
50
58
|
# === Parameter
|
51
59
|
# code<String>:: code received after auth
|
52
60
|
# === Returns
|
@@ -56,11 +64,11 @@ module SK::SDK
|
|
56
64
|
:client_secret => @secret,
|
57
65
|
:redirect_uri => @redirect_url,
|
58
66
|
:code => code }
|
59
|
-
"#{sk_url}/oauth/
|
67
|
+
"#{sk_url}/oauth/token?#{to_url_params(params)}"
|
60
68
|
end
|
61
69
|
|
62
70
|
# Makes a GET request to the access_token endpoint in SK and receives the
|
63
|
-
#
|
71
|
+
# access token
|
64
72
|
def get_token(code)
|
65
73
|
c = Curl::Easy.perform( token_url( code ) )
|
66
74
|
# grab token from response body, containing json string
|
@@ -68,7 +76,7 @@ module SK::SDK
|
|
68
76
|
end
|
69
77
|
|
70
78
|
# Each company has it's own subdomain so the url must be dynamic.
|
71
|
-
# This is achieved by replacing the * with the subdomain
|
79
|
+
# This is achieved by replacing the * with the subdomain in the instance
|
72
80
|
# === Returns
|
73
81
|
# <String>:: url
|
74
82
|
def sk_url
|
@@ -0,0 +1,41 @@
|
|
1
|
+
= SalesKing OmniAuth Strategy
|
2
|
+
|
3
|
+
If you are into omniAuth you can use this class to authenticate with SalesKing.
|
4
|
+
Since every SK Account resides within its own subdomain, you need to set it
|
5
|
+
before you want to use it.
|
6
|
+
|
7
|
+
You can use this file as a starting point because you might want to change the
|
8
|
+
subdomain detection. If you find it useful and fiddled around with it, please
|
9
|
+
let us know so we can enhance it.
|
10
|
+
|
11
|
+
|
12
|
+
== Install
|
13
|
+
|
14
|
+
gem install sk_sdk
|
15
|
+
|
16
|
+
If you are using bundler add to Gemfile
|
17
|
+
|
18
|
+
gem "sk_sdk"
|
19
|
+
# or directly require used class
|
20
|
+
gem "sk_sdk", :require => "sk_sdk/ar_omni_auth/salesking"
|
21
|
+
|
22
|
+
== Usage
|
23
|
+
|
24
|
+
An initializers/omniauth.rb might look somethign like this:
|
25
|
+
# if you copied the file into your lib/
|
26
|
+
require File.expand_path('../../../lib/sk_auth', __FILE__)
|
27
|
+
# or from gem
|
28
|
+
require "sk_sdk/omni_auth/salesking"
|
29
|
+
begin
|
30
|
+
sk = YAML.load_file(File.join(Rails.root, 'config', 'salesking.yml'))
|
31
|
+
sk = sk[Rails.env]
|
32
|
+
rescue
|
33
|
+
puts "Error reading config/salesking.yml please create and review this file"
|
34
|
+
end
|
35
|
+
|
36
|
+
Rails.application.config.middleware.use OmniAuth::Builder do
|
37
|
+
provider :salesking, sk['key'], sk['secret'], sk['url']
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
Copyright (c) 2011 Georg Leciejewski, released under the MIT license
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'omniauth/oauth'
|
2
|
+
require 'multi_json'
|
3
|
+
|
4
|
+
module OmniAuth
|
5
|
+
module Strategies
|
6
|
+
class Salesking < OAuth2
|
7
|
+
# SalesKing requires a subdomain up front
|
8
|
+
# It needs to be provided in a form and retrieved from the request
|
9
|
+
# The one on init is not used as it is grabbed from the session later
|
10
|
+
# === Params
|
11
|
+
# app<Rack Application]>:: app standard middleware application parameter
|
12
|
+
# client_id<String>:: the application id as registered on SalesKing
|
13
|
+
# client_secret<String>:: the application secret as registered on SalesKing
|
14
|
+
# scope<String>:: space separated extended permissions such as `api/invoices` or `api/clients:read,delete`
|
15
|
+
def initialize(app, client_id, client_secret, sk_url, scope)
|
16
|
+
@base_url = sk_url
|
17
|
+
@scope = scope
|
18
|
+
super(app, :salesking, client_id, client_secret)
|
19
|
+
end
|
20
|
+
|
21
|
+
#inject salesking url and scope
|
22
|
+
def request_phase
|
23
|
+
options[:scope] = @scope
|
24
|
+
set_sk_url
|
25
|
+
super
|
26
|
+
end
|
27
|
+
|
28
|
+
#Monkey-patching to inject subdomain again
|
29
|
+
def callback_phase
|
30
|
+
set_sk_url
|
31
|
+
super
|
32
|
+
end
|
33
|
+
|
34
|
+
def user_data
|
35
|
+
@data ||= begin
|
36
|
+
ret = MultiJson.decode(@access_token.get('api/users/current'))
|
37
|
+
ret['user']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def user_info
|
42
|
+
{
|
43
|
+
'email' => (user_data["email"] if user_data["email"]),
|
44
|
+
'first_name' => user_data["first_name"],
|
45
|
+
'last_name' => user_data["last_name"],
|
46
|
+
'name' => "#{user_data['first_name']} #{user_data['last_name']}"
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def auth_hash
|
51
|
+
OmniAuth::Utils.deep_merge(super, {
|
52
|
+
# 'uid' => "#{user_data['current_company']['company']['id']}.#{user_data['id']}",
|
53
|
+
'uid' => user_data['id'],
|
54
|
+
'user_info' => user_info,
|
55
|
+
'credentials' => {
|
56
|
+
'expires_in' => @access_token.expires_in
|
57
|
+
},
|
58
|
+
'extra' => {'user_hash' => user_data}
|
59
|
+
})
|
60
|
+
end
|
61
|
+
|
62
|
+
# Each company has it's own subdomain so the url must be dynamic.
|
63
|
+
# This is achieved by replacing the * with the subdomain from the session
|
64
|
+
# === Returns
|
65
|
+
# <String>:: url
|
66
|
+
def set_sk_url
|
67
|
+
client_options[:site] = @base_url.gsub('*', session[:subdomain]).gsub(/\/\z/, '' )
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/sk_sdk.rb
CHANGED
data/sk_sdk.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sk_sdk}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Georg Leciejewski"]
|
12
|
-
s.date = %q{2011-05-
|
12
|
+
s.date = %q{2011-05-27}
|
13
13
|
s.description = %q{Connect your business world with SalesKing. This gem gives ruby developers a jump-start for building SalesKing Business Apps. Under the hood it provides classes to handle oAuth, make RESTfull API requests and parses JSON Schema }
|
14
14
|
s.email = %q{gl@salesking.eu}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -21,18 +21,22 @@ Gem::Specification.new do |s|
|
|
21
21
|
"Rakefile",
|
22
22
|
"VERSION",
|
23
23
|
"lib/sk_sdk.rb",
|
24
|
-
"lib/sk_sdk/
|
24
|
+
"lib/sk_sdk/README_Base.rdoc",
|
25
25
|
"lib/sk_sdk/ar_cli.rb",
|
26
26
|
"lib/sk_sdk/ar_cli/patches/ar2/base.rb",
|
27
27
|
"lib/sk_sdk/ar_cli/patches/ar2/validations.rb",
|
28
28
|
"lib/sk_sdk/ar_cli/patches/ar3/base.rb",
|
29
29
|
"lib/sk_sdk/ar_cli/patches/ar3/validations.rb",
|
30
|
+
"lib/sk_sdk/base.rb",
|
30
31
|
"lib/sk_sdk/oauth.rb",
|
32
|
+
"lib/sk_sdk/omni_auth/README.rdoc",
|
33
|
+
"lib/sk_sdk/omni_auth/salesking.rb",
|
31
34
|
"lib/sk_sdk/signed_request.rb",
|
32
35
|
"sk_sdk.gemspec",
|
33
36
|
"spec/resources_spec_helper.rb",
|
34
37
|
"spec/settings.yml",
|
35
38
|
"spec/sk_sdk/ar_cli_spec.rb",
|
39
|
+
"spec/sk_sdk/base_spec.rb",
|
36
40
|
"spec/sk_sdk/oauth_spec.rb",
|
37
41
|
"spec/sk_sdk/resources/README.rdoc",
|
38
42
|
"spec/sk_sdk/resources/clients_spec.rb",
|
@@ -49,6 +53,7 @@ Gem::Specification.new do |s|
|
|
49
53
|
s.test_files = [
|
50
54
|
"spec/resources_spec_helper.rb",
|
51
55
|
"spec/sk_sdk/ar_cli_spec.rb",
|
56
|
+
"spec/sk_sdk/base_spec.rb",
|
52
57
|
"spec/sk_sdk/oauth_spec.rb",
|
53
58
|
"spec/sk_sdk/resources/clients_spec.rb",
|
54
59
|
"spec/sk_sdk/resources/credit_note_spec.rb",
|
@@ -1,21 +1,14 @@
|
|
1
1
|
CONNECTION = {
|
2
|
-
:site => "
|
3
|
-
|
4
|
-
:user => "demo@salesking.eu"
|
5
|
-
:password => "demouser",
|
6
|
-
:format => :json
|
2
|
+
:site => "http://demo.salesking.local:3000/api/",
|
3
|
+
:password => "demo",
|
4
|
+
:user => "demo@salesking.eu"
|
7
5
|
} unless defined?(CONNECTION)
|
8
6
|
|
9
7
|
# create all classes and set their connection
|
10
|
-
|
11
|
-
# SK::SDK::
|
12
|
-
# Client.set_connection( CONNECTION )
|
13
|
-
[:client, :address, :credit_note, :line_item, :invoice, :product].each do |name|
|
14
|
-
class_name = "#{name}".camelize
|
15
|
-
SK::SDK::ArCli.make(class_name) unless Object.const_defined?(class_name)
|
16
|
-
class_name.constantize.set_connection( CONNECTION )
|
8
|
+
%w[Client Address CreditNote Invoice Product LineItem].each do |model|
|
9
|
+
eval "class #{model} < SK::SDK::Base;end" unless Object.const_defined?(model)
|
17
10
|
end
|
18
|
-
|
11
|
+
SK::SDK::Base.set_connection CONNECTION
|
19
12
|
|
20
13
|
# check if a SalesKing instance is available by calling /users/current.json
|
21
14
|
def sk_available?
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
require 'spec/resources_spec_helper'
|
3
|
+
|
4
|
+
class Client < SK::SDK::Base;end
|
5
|
+
# create objects in King namespace
|
6
|
+
module KingTester; end
|
7
|
+
%w[Invoice Product].each do |model|
|
8
|
+
eval "class KingTester::#{model} < SK::SDK::Base;end"
|
9
|
+
end
|
10
|
+
|
11
|
+
describe SK::SDK::Base, "make new class" do
|
12
|
+
|
13
|
+
it "should create class" do
|
14
|
+
c = Client.new
|
15
|
+
c.first_name = 'herbert' # implicit setter
|
16
|
+
c.first_name.should == 'herbert' # implicit getter
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should have properties as attributes" do
|
20
|
+
c = Client.new :some_field => ''
|
21
|
+
c.attributes.should == {"some_field"=>""}
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should create save method" do
|
25
|
+
c = Client.new
|
26
|
+
c.respond_to?(:save).should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should have new_record?" do
|
30
|
+
c = Client.new
|
31
|
+
c.new_record?.should be_true
|
32
|
+
i = KingTester::Invoice.new
|
33
|
+
i.new_record?.should be_true
|
34
|
+
p = KingTester::Product.new
|
35
|
+
p.new_record?.should be_true
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,12 +4,13 @@ require 'spec'
|
|
4
4
|
require "active_support"
|
5
5
|
require "active_support/json"
|
6
6
|
require "#{File.dirname(__FILE__)}/../lib/sk_sdk"
|
7
|
+
require "#{File.dirname(__FILE__)}/../lib/sk_sdk/base"
|
7
8
|
require "#{File.dirname(__FILE__)}/../lib/sk_sdk/oauth"
|
8
9
|
require "#{File.dirname(__FILE__)}/../lib/sk_sdk/signed_request"
|
9
10
|
require "#{File.dirname(__FILE__)}/../lib/sk_sdk/ar_cli"
|
10
11
|
|
11
12
|
|
12
|
-
puts "Testing with ActiveResource v: #{ActiveResource::VERSION::STRING}
|
13
|
+
puts "Testing with ActiveResource v: #{ActiveResource::VERSION::STRING}"
|
13
14
|
|
14
15
|
|
15
16
|
def load_settings
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sk_sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Georg Leciejewski
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-27 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -88,18 +88,22 @@ files:
|
|
88
88
|
- Rakefile
|
89
89
|
- VERSION
|
90
90
|
- lib/sk_sdk.rb
|
91
|
-
- lib/sk_sdk/
|
91
|
+
- lib/sk_sdk/README_Base.rdoc
|
92
92
|
- lib/sk_sdk/ar_cli.rb
|
93
93
|
- lib/sk_sdk/ar_cli/patches/ar2/base.rb
|
94
94
|
- lib/sk_sdk/ar_cli/patches/ar2/validations.rb
|
95
95
|
- lib/sk_sdk/ar_cli/patches/ar3/base.rb
|
96
96
|
- lib/sk_sdk/ar_cli/patches/ar3/validations.rb
|
97
|
+
- lib/sk_sdk/base.rb
|
97
98
|
- lib/sk_sdk/oauth.rb
|
99
|
+
- lib/sk_sdk/omni_auth/README.rdoc
|
100
|
+
- lib/sk_sdk/omni_auth/salesking.rb
|
98
101
|
- lib/sk_sdk/signed_request.rb
|
99
102
|
- sk_sdk.gemspec
|
100
103
|
- spec/resources_spec_helper.rb
|
101
104
|
- spec/settings.yml
|
102
105
|
- spec/sk_sdk/ar_cli_spec.rb
|
106
|
+
- spec/sk_sdk/base_spec.rb
|
103
107
|
- spec/sk_sdk/oauth_spec.rb
|
104
108
|
- spec/sk_sdk/resources/README.rdoc
|
105
109
|
- spec/sk_sdk/resources/clients_spec.rb
|
@@ -145,6 +149,7 @@ summary: SalesKing SDK Ruby
|
|
145
149
|
test_files:
|
146
150
|
- spec/resources_spec_helper.rb
|
147
151
|
- spec/sk_sdk/ar_cli_spec.rb
|
152
|
+
- spec/sk_sdk/base_spec.rb
|
148
153
|
- spec/sk_sdk/oauth_spec.rb
|
149
154
|
- spec/sk_sdk/resources/clients_spec.rb
|
150
155
|
- spec/sk_sdk/resources/credit_note_spec.rb
|
@@ -1,84 +0,0 @@
|
|
1
|
-
= SalesKing API Client
|
2
|
-
|
3
|
-
ActiveResource based SalesKing API client. This client does not uses oAuth2 yet,
|
4
|
-
it still uses HTTBasic Auth with username and password.
|
5
|
-
|
6
|
-
This does NOT rely on parsing the JSON Schema, since ActiveResource creates the
|
7
|
-
Getter/Setter methods(less restrict). The client also adds some patches to AR to
|
8
|
-
fix json parsing issues.
|
9
|
-
|
10
|
-
== Install
|
11
|
-
|
12
|
-
gem install sk_sdk
|
13
|
-
|
14
|
-
Dependencies (gem's):
|
15
|
-
|
16
|
-
* activeresupport
|
17
|
-
* activeresource
|
18
|
-
|
19
|
-
== Usage
|
20
|
-
|
21
|
-
SalesKing's api interface is RESTful(mostly) and returns & accepts JSON data.
|
22
|
-
All resources such as clients, invoices, products can be accessed via URL's
|
23
|
-
through standard HTTP methods GET, POST, PUT and DELETE.
|
24
|
-
see available objects and endpoints here:
|
25
|
-
https://github.com/salesking/sk_api_schema/tree/master/json
|
26
|
-
|
27
|
-
First create the classes:
|
28
|
-
|
29
|
-
require "sk_sdk/ar_client"
|
30
|
-
SK::SDK::ArCli.make(:client)
|
31
|
-
|
32
|
-
# Create class within namespace
|
33
|
-
module; King; end
|
34
|
-
SK::SDK::ArCli.make(:credit_note, King)
|
35
|
-
|
36
|
-
Now the classes are available they need connection settings first. Those Must
|
37
|
-
be set for each class separate and you MUST provide the full url using the right
|
38
|
-
protocol http/https + SUBDomain + salesking url + /api
|
39
|
-
Our production system only supports HTTPS.
|
40
|
-
|
41
|
-
[Client, CreditNote].each do |i|
|
42
|
-
i.send(:set_connection, {:site => 'https://my_sub.salesking.eu/api',
|
43
|
-
:user => 'my-users@login-email.com',
|
44
|
-
:password => 'password' })
|
45
|
-
client = Client.new(:last_name=> 'Meister')
|
46
|
-
client.first_name = "Bau"
|
47
|
-
client.save
|
48
|
-
credit_note = King::CreditNote.new
|
49
|
-
|
50
|
-
Also see ActiveResource readme:
|
51
|
-
https://github.com/rails/rails/tree/master/activeresource
|
52
|
-
|
53
|
-
Want to know more about REST style webservices?
|
54
|
-
* http://en.wikipedia.org/wiki/Representational_State_Transfer
|
55
|
-
* http://www.google.com/search?q=REST+site%3Awww.infoq.com
|
56
|
-
|
57
|
-
=== Authentification & Safety
|
58
|
-
|
59
|
-
Authentification can be done with oAuth2 but this client still uses HTTP basic
|
60
|
-
using your SalesKing login email and password.
|
61
|
-
|
62
|
-
For a production environment be advised to create a user, per api client, and
|
63
|
-
restrict his rights with our build in role-system!
|
64
|
-
|
65
|
-
|
66
|
-
=== API Tutorial and Tools
|
67
|
-
|
68
|
-
Since browsers do not support PUT/DELETE methods you can use CURL, a linux
|
69
|
-
command-line http client, for testing. And of course any http library supporting
|
70
|
-
http-basic-auth can be used.
|
71
|
-
|
72
|
-
* Getting started: http://dev.blog.salesking.eu/api/
|
73
|
-
* Chrome cRest extension https://chrome.google.com/extensions/detail/baedhhmoaooldchehjhlpppaieoglhml
|
74
|
-
* Poster FF-Plugin - make HTTP request https://addons.mozilla.org/en-US/firefox/addon/2691/ (you must be logged into SalesKing)
|
75
|
-
* JSONView FF-Plugin - view json in firefox https://addons.mozilla.org/de/firefox/addon/10869/
|
76
|
-
* JSONovich FF-Plugin - https://addons.mozilla.org/de/firefox/addon/10122/
|
77
|
-
|
78
|
-
== Tests / Specs
|
79
|
-
|
80
|
-
Take a look into spec/resources to see a how-to
|
81
|
-
|
82
|
-
rake coverage
|
83
|
-
|
84
|
-
Copyright (c) 2011 Georg Leciejewski, released under the MIT license
|