sk_sdk 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d8b37450434521a67d849f242b2687bfe6cee7fe
4
+ data.tar.gz: 9bdfed1b7b55e8e14ecdeae80caca207c4abe024
5
+ SHA512:
6
+ metadata.gz: fd0877e811c40bb79a7cf561b4feb9c4ded9c895f4514fae8a373b32742bcc7e68829560829105e5c497aec2bf6cd2f436d4e3a1162621d286198e93a5a4c51a
7
+ data.tar.gz: 66efcacd645a14db318fd921d6aa589493ae8443c531d30681b55ba90d5b7ee5340cb99522578750fe05cfbef6107635ffaff23fd951c56a41be94f94e63b98e
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :gemcutter
1
+ source 'https://rubygems.org'
2
2
  gemspec
data/ci/Gemfile_ar3 ADDED
@@ -0,0 +1,11 @@
1
+ source :gemcutter
2
+ gem "rake"
3
+ gem "rdoc"
4
+ gem "activesupport", "3.2"
5
+ gem "activeresource", "3.2"
6
+ gem "sk_api_schema"
7
+ gem "curb"
8
+ group :test do
9
+ gem "rspec"
10
+ gem "simplecov"
11
+ end
@@ -0,0 +1,50 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.0)
5
+ activesupport (= 3.2.0)
6
+ builder (~> 3.0.0)
7
+ activeresource (3.2.0)
8
+ activemodel (= 3.2.0)
9
+ activesupport (= 3.2.0)
10
+ activesupport (3.2.0)
11
+ i18n (~> 0.6)
12
+ multi_json (~> 1.0)
13
+ builder (3.0.4)
14
+ curb (0.8.5)
15
+ diff-lcs (1.2.5)
16
+ docile (1.1.3)
17
+ i18n (0.6.9)
18
+ json (1.8.1)
19
+ multi_json (1.8.4)
20
+ rake (10.1.1)
21
+ rdoc (4.1.1)
22
+ json (~> 1.4)
23
+ rspec (2.14.1)
24
+ rspec-core (~> 2.14.0)
25
+ rspec-expectations (~> 2.14.0)
26
+ rspec-mocks (~> 2.14.0)
27
+ rspec-core (2.14.7)
28
+ rspec-expectations (2.14.5)
29
+ diff-lcs (>= 1.1.3, < 2.0)
30
+ rspec-mocks (2.14.5)
31
+ simplecov (0.8.2)
32
+ docile (~> 1.1.0)
33
+ multi_json
34
+ simplecov-html (~> 0.8.0)
35
+ simplecov-html (0.8.0)
36
+ sk_api_schema (0.9.4)
37
+ activesupport
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ activeresource (= 3.2)
44
+ activesupport (= 3.2)
45
+ curb
46
+ rake
47
+ rdoc
48
+ rspec
49
+ simplecov
50
+ sk_api_schema
@@ -0,0 +1,34 @@
1
+ module ActiveResource
2
+ # Overridden methods to suit SalesKing's nested json format
3
+ # only valid for AR 4+
4
+ # In the future might add a custom format class, see base.format
5
+ class Base
6
+
7
+ # override ARes method to parse only the client part
8
+ def load_attributes_from_response(response)
9
+ if (response['Transfer-Encoding'] == 'chunked' || (!response['Content-Length'].blank? && response['Content-Length'] != "0")) && !response.body.nil? && response.body.strip.size > 0
10
+ load( self.class.format.decode(response.body)[self.class.element_name] )
11
+ #fix double nested items .. active resource SUCKS soooo bad
12
+ if self.respond_to?(:items)
13
+ new_items = []
14
+ self.items.each { |item| new_items << item.attributes.first[1] }
15
+ self.items = new_items
16
+ end
17
+ @persisted = true
18
+ end
19
+ end
20
+ private
21
+ # Overridden to grab the data(= clients-collection) from json:
22
+ # { 'collection'=> will_paginate infos,
23
+ # 'links' => prev/next links
24
+ # 'clients'=> [data], << what we need
25
+ # }
26
+ def self.instantiate_collection(collection, original_params = {}, prefix_options = {})
27
+ elements_name = self.element_name.pluralize if collection.is_a?(Hash)
28
+ collection_parser.new(collection, elements_name).tap do |parser|
29
+ parser.resource_class = self
30
+ parser.original_params = original_params
31
+ end.collect! { |record| instantiate_record(record, prefix_options) }
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ require 'active_resource/validations'
2
+ class ActiveResource::Errors < ActiveModel::Errors
3
+ # Patched cause we dont need no attribute name magic .. and its just simpler
4
+ # orig version is looking up the humanized name of the attribute in the error
5
+ # message, which we dont supply => only field name is used in returned error msg
6
+ def from_array(messages, save_cache=false)
7
+ clear unless save_cache
8
+ messages.each do |msg|
9
+ add msg[0], msg[1]
10
+ end
11
+ end
12
+ end
data/lib/sk_sdk/base.rb CHANGED
@@ -3,19 +3,24 @@ require 'sk_sdk'
3
3
  require 'active_resource'
4
4
  require 'active_resource/version'
5
5
  # patches are for specific AR version
6
- if ActiveResource::VERSION::MAJOR == 3
7
- require 'sk_sdk/ar_patches/ar3/base'
8
- require 'sk_sdk/ar_patches/ar3/validations'
9
- elsif ActiveResource::VERSION::MAJOR < 3
6
+ case ActiveResource::VERSION::MAJOR
7
+ when 2
10
8
  require 'sk_sdk/ar_patches/ar2/validations'
11
9
  require 'sk_sdk/ar_patches/ar2/base'
10
+ when 3
11
+ require 'sk_sdk/ar_patches/ar3/base'
12
+ require 'sk_sdk/ar_patches/ar3/validations'
13
+ when 4
14
+ require 'sk_sdk/ar_patches/ar4/validations'
15
+ require 'sk_sdk/ar_patches/ar4/base'
12
16
  end
13
17
 
14
18
  class SK::SDK::Base < ActiveResource::Base
15
19
  self.format = :json
16
20
  # hook before init in activeresource base because json comes in nested:
17
21
  # {client={data}
18
- if ActiveResource::VERSION::MAJOR == 3 && ActiveResource::VERSION::MINOR > 0
22
+ if (ActiveResource::VERSION::MAJOR == 3 && ActiveResource::VERSION::MINOR > 0) || ActiveResource::VERSION::MAJOR > 3
23
+ self.include_root_in_json = true
19
24
  def initialize(attributes = {}, *args)
20
25
  attr = attributes[self.class.element_name] || attributes
21
26
  super(attr, *args)
@@ -66,4 +71,28 @@ class SK::SDK::Base < ActiveResource::Base
66
71
  site = site.gsub(/\/$/, '')
67
72
  site =~ /\/api$/ ? site : "#{site}/api"
68
73
  end
74
+
75
+ # Unfortunately only using AR v4+ we can create additional accessors
76
+ # to get info on collection info and links
77
+ if ActiveResource::VERSION::MAJOR == 4
78
+ class SkCollection < ActiveResource::Collection
79
+ attr_accessor :current_page, :per_page, :total_entries, :total_pages
80
+
81
+ attr_accessor :next_url, :self_url, :prev_url
82
+
83
+ def initialize(parsed = {}, element_name = nil)
84
+ @elements = element_name.present? ? parsed[element_name] : parsed
85
+
86
+ %w(current_page per_page total_entries total_pages).each do |collection_info|
87
+ instance_variable_set(:"@#{collection_info}", parsed['collection'][collection_info])
88
+ end
89
+
90
+ %w(next self prev).each do |link_info|
91
+ instance_variable_set(:"@#{link_info}_url", parsed['links'][link_info])
92
+ end
93
+ @next_page = parsed['next_page']
94
+ end
95
+ end
96
+ self.collection_parser = SkCollection
97
+ end
69
98
  end
data/lib/sk_sdk/oauth.rb CHANGED
@@ -16,7 +16,7 @@ module SK::SDK
16
16
  # your app
17
17
  # @option [String] id oAuth app id from SalesKing app registration
18
18
  # @option [String] secret oAuth app secret from SalesKing app registration
19
- # @option [String] scope permission your app requests
19
+ # @option [String|Array[String]] permission scopes for your app requests
20
20
  # @option [String] redirect_url inside your app for auth dialog
21
21
  # @option [String] sk_url SalesKing base url, * is replaced with users subdomain,
22
22
  # no trailing slash, optional defaults to https://*.salesking.eu
@@ -34,9 +34,10 @@ module SK::SDK
34
34
 
35
35
  # @return [String] URL with parameter to show the auth dialog to the user
36
36
  def auth_dialog
37
+ scope_string = Array === @scope ? @scope.join(' ') : @scope
37
38
  params = { :client_id => @id,
38
39
  :redirect_uri=> @redirect_url,
39
- :scope => @scope }
40
+ :scope => scope_string }
40
41
  "#{sk_url}/oauth/authorize?#{to_url_params(params)}"
41
42
  end
42
43
 
@@ -88,10 +89,8 @@ module SK::SDK
88
89
  end
89
90
 
90
91
  def to_url_params(params_hash)
91
- out = []
92
- params_hash.each { |k,v| out << "#{CGI::escape k.to_s}=#{CGI::escape v.to_s}" }
93
- out.join('&')
92
+ params_hash.map { |k,v| "#{CGI::escape k.to_s}=#{CGI::escape v.to_s}" }.join('&')
94
93
  end
95
94
 
96
95
  end
97
- end
96
+ end
@@ -1,5 +1,5 @@
1
1
  module SK
2
2
  module SDK
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
data/sk_sdk.gemspec CHANGED
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
13
13
  s.description = %q{Connect your business with SalesKing. This gem gives ruby developers a jump-start for building SalesKing Business Apps. It provides classes to handle oAuth, make RESTfull API requests and parses JSON Schema }
14
14
  s.extra_rdoc_files = ['README.rdoc']
15
15
  s.rubygems_version = %q{1.6.2}
16
+ s.license = 'MIT'
16
17
 
17
18
  s.files = `git ls-files`.split("\n")
18
19
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -42,7 +42,8 @@ else
42
42
 
43
43
  it "should find contacts" do
44
44
  contacts = Contact.find(:all)
45
- contacts.should_not be_empty
45
+ ActiveResource::Collection
46
+ contacts.length.should > 0
46
47
  end
47
48
  end
48
49
 
@@ -170,16 +170,30 @@ describe Invoice do
170
170
  end
171
171
 
172
172
  describe 'with items of different type' do
173
- before :all do
173
+ before :each do
174
174
  @contact = Contact.new(:type=>'Client', :organisation=>'Credit Note API-Tester')
175
175
  @contact.save.should be_true
176
176
  @doc = Invoice.new(:contact_id => @contact.id)
177
177
  end
178
178
 
179
- after :all do
179
+ after :each do
180
180
  delete_test_data(@doc, @contact)
181
181
  end
182
182
 
183
+ it 'should create items with price of 0' do
184
+ # :quantity => 1, is default
185
+ @doc.items = [
186
+ { :position=>1, :name => 'Pork Chops', :price_single =>'0.00', :type=>'LineItem' },
187
+ { :position=>2, :name => 'Pork Chops', :price_single =>'0,0', :type=>'LineItem' },
188
+ { :position=>3, :name => 'Pork Chops', :price_single =>'0', :type=>'LineItem' },
189
+ { :position=>4, :name => 'Pork Chops', :price_single =>0, :type=>'LineItem' },
190
+ { :position=>5, :name => 'Pork Chops', :price_single =>0.0, :type=>'LineItem' },
191
+ ]
192
+ @doc.save.should be_true
193
+ @doc.items.length.should == 5
194
+ @doc.gross_total.should == 0.0
195
+ end
196
+
183
197
  it 'should create items from array' do
184
198
  @doc.items = [
185
199
  { :position=>1, :name => 'Pork Chops', :quantity => 12, :price_single =>'10.00', :type=>'LineItem' },
@@ -30,7 +30,7 @@ describe Payment do
30
30
  # damn i hate active_resource
31
31
  @doc.post(:payments, {}, p.encode)
32
32
  payments_json = @doc.get(:payments)
33
- payments = Payment.instantiate_collection(payments_json)
33
+ payments = Payment.send(:instantiate_collection, payments_json)
34
34
 
35
35
  payments.first.amount.should == 10
36
36
  end
@@ -43,7 +43,7 @@ describe Payment do
43
43
 
44
44
  # damn i hate active_resource
45
45
  @doc.post(:payments, {}, p.encode)
46
- payments = Payment.instantiate_collection(@doc.get(:payments))
46
+ payments = Payment.send(:instantiate_collection, @doc.get(:payments))
47
47
  payment = payments.detect{|p| p.amount == 11 }
48
48
  payment.external_ref.should == 'from sdk-test'
49
49
  payment.date.should == Date.today.strftime("%Y-%m-%d")
@@ -69,7 +69,7 @@ describe Payment do
69
69
  p = Payment.new :amount => 12.34, :related_object_id=>@doc.id
70
70
  p.save.should be_true
71
71
 
72
- payments = Payment.instantiate_collection(@doc.get(:payments))
72
+ payments = Payment.send( :instantiate_collection, @doc.get(:payments))
73
73
  payments.map(&:amount).should include 12.34
74
74
  end
75
75
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sk_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
5
- prerelease:
4
+ version: 0.4.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Georg Leciejewski
@@ -15,132 +14,116 @@ dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: curb
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
- - - ! '>='
18
+ - - ">="
21
19
  - !ruby/object:Gem::Version
22
20
  version: '0'
23
21
  type: :runtime
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
- - - ! '>='
25
+ - - ">="
29
26
  - !ruby/object:Gem::Version
30
27
  version: '0'
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: activesupport
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
- - - ! '>='
32
+ - - ">="
37
33
  - !ruby/object:Gem::Version
38
34
  version: '0'
39
35
  type: :runtime
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
- - - ! '>='
39
+ - - ">="
45
40
  - !ruby/object:Gem::Version
46
41
  version: '0'
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: activeresource
49
44
  requirement: !ruby/object:Gem::Requirement
50
- none: false
51
45
  requirements:
52
- - - ! '>='
46
+ - - ">="
53
47
  - !ruby/object:Gem::Version
54
48
  version: '0'
55
49
  type: :runtime
56
50
  prerelease: false
57
51
  version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
52
  requirements:
60
- - - ! '>='
53
+ - - ">="
61
54
  - !ruby/object:Gem::Version
62
55
  version: '0'
63
56
  - !ruby/object:Gem::Dependency
64
57
  name: sk_api_schema
65
58
  requirement: !ruby/object:Gem::Requirement
66
- none: false
67
59
  requirements:
68
- - - ! '>='
60
+ - - ">="
69
61
  - !ruby/object:Gem::Version
70
62
  version: '0'
71
63
  type: :runtime
72
64
  prerelease: false
73
65
  version_requirements: !ruby/object:Gem::Requirement
74
- none: false
75
66
  requirements:
76
- - - ! '>='
67
+ - - ">="
77
68
  - !ruby/object:Gem::Version
78
69
  version: '0'
79
70
  - !ruby/object:Gem::Dependency
80
71
  name: rake
81
72
  requirement: !ruby/object:Gem::Requirement
82
- none: false
83
73
  requirements:
84
- - - ! '>='
74
+ - - ">="
85
75
  - !ruby/object:Gem::Version
86
76
  version: '0'
87
77
  type: :development
88
78
  prerelease: false
89
79
  version_requirements: !ruby/object:Gem::Requirement
90
- none: false
91
80
  requirements:
92
- - - ! '>='
81
+ - - ">="
93
82
  - !ruby/object:Gem::Version
94
83
  version: '0'
95
84
  - !ruby/object:Gem::Dependency
96
85
  name: simplecov
97
86
  requirement: !ruby/object:Gem::Requirement
98
- none: false
99
87
  requirements:
100
- - - ! '>='
88
+ - - ">="
101
89
  - !ruby/object:Gem::Version
102
90
  version: '0'
103
91
  type: :development
104
92
  prerelease: false
105
93
  version_requirements: !ruby/object:Gem::Requirement
106
- none: false
107
94
  requirements:
108
- - - ! '>='
95
+ - - ">="
109
96
  - !ruby/object:Gem::Version
110
97
  version: '0'
111
98
  - !ruby/object:Gem::Dependency
112
99
  name: rspec
113
100
  requirement: !ruby/object:Gem::Requirement
114
- none: false
115
101
  requirements:
116
- - - ! '>='
102
+ - - ">="
117
103
  - !ruby/object:Gem::Version
118
104
  version: '0'
119
105
  type: :development
120
106
  prerelease: false
121
107
  version_requirements: !ruby/object:Gem::Requirement
122
- none: false
123
108
  requirements:
124
- - - ! '>='
109
+ - - ">="
125
110
  - !ruby/object:Gem::Version
126
111
  version: '0'
127
112
  - !ruby/object:Gem::Dependency
128
113
  name: rdoc
129
114
  requirement: !ruby/object:Gem::Requirement
130
- none: false
131
115
  requirements:
132
- - - ! '>='
116
+ - - ">="
133
117
  - !ruby/object:Gem::Version
134
118
  version: '0'
135
119
  type: :development
136
120
  prerelease: false
137
121
  version_requirements: !ruby/object:Gem::Requirement
138
- none: false
139
122
  requirements:
140
- - - ! '>='
123
+ - - ">="
141
124
  - !ruby/object:Gem::Version
142
125
  version: '0'
143
- description: ! 'Connect your business with SalesKing. This gem gives ruby developers
126
+ description: 'Connect your business with SalesKing. This gem gives ruby developers
144
127
  a jump-start for building SalesKing Business Apps. It provides classes to handle
145
128
  oAuth, make RESTfull API requests and parses JSON Schema '
146
129
  email:
@@ -150,20 +133,24 @@ extensions: []
150
133
  extra_rdoc_files:
151
134
  - README.rdoc
152
135
  files:
153
- - .gitignore
154
- - .travis.yml
136
+ - ".gitignore"
137
+ - ".travis.yml"
155
138
  - Gemfile
156
139
  - MIT-LICENSE
157
140
  - README.rdoc
158
141
  - Rakefile
159
142
  - ci/Gemfile_ar2
160
143
  - ci/Gemfile_ar2.lock
144
+ - ci/Gemfile_ar3
145
+ - ci/Gemfile_ar3.lock
161
146
  - lib/sk_sdk.rb
162
147
  - lib/sk_sdk/README.rdoc
163
148
  - lib/sk_sdk/ar_patches/ar2/base.rb
164
149
  - lib/sk_sdk/ar_patches/ar2/validations.rb
165
150
  - lib/sk_sdk/ar_patches/ar3/base.rb
166
151
  - lib/sk_sdk/ar_patches/ar3/validations.rb
152
+ - lib/sk_sdk/ar_patches/ar4/base.rb
153
+ - lib/sk_sdk/ar_patches/ar4/validations.rb
167
154
  - lib/sk_sdk/base.rb
168
155
  - lib/sk_sdk/oauth.rb
169
156
  - lib/sk_sdk/omni_auth/README.rdoc
@@ -187,33 +174,27 @@ files:
187
174
  - spec/sk_sdk/sync_spec.rb
188
175
  - spec/spec_helper.rb
189
176
  homepage: http://github.com/salesking/sk_sdk
190
- licenses: []
177
+ licenses:
178
+ - MIT
179
+ metadata: {}
191
180
  post_install_message:
192
181
  rdoc_options: []
193
182
  require_paths:
194
183
  - lib
195
184
  required_ruby_version: !ruby/object:Gem::Requirement
196
- none: false
197
185
  requirements:
198
- - - ! '>='
186
+ - - ">="
199
187
  - !ruby/object:Gem::Version
200
188
  version: '0'
201
- segments:
202
- - 0
203
- hash: 892546568127510729
204
189
  required_rubygems_version: !ruby/object:Gem::Requirement
205
- none: false
206
190
  requirements:
207
- - - ! '>='
191
+ - - ">="
208
192
  - !ruby/object:Gem::Version
209
193
  version: '0'
210
- segments:
211
- - 0
212
- hash: 892546568127510729
213
194
  requirements: []
214
195
  rubyforge_project:
215
- rubygems_version: 1.8.24
196
+ rubygems_version: 2.2.2
216
197
  signing_key:
217
- specification_version: 3
198
+ specification_version: 4
218
199
  summary: SalesKing Ruby SDK - simplify your Business
219
200
  test_files: []