sk_api_schema 0.0.16 → 0.0.17

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.16
1
+ 0.0.17
@@ -1,6 +1,6 @@
1
- {"type":"object",
1
+ { "type":"object",
2
2
  "title": "company",
3
- "description": "A company is the main entity. Any data is retrieved scoped to the current company, identified by the subdomain.",
3
+ "description": "A company is the main entity, so any data belongs to a company, identified by the current subdomain.",
4
4
  "properties": {
5
5
  "id": {
6
6
  "identity":true,
@@ -101,8 +101,8 @@
101
101
  },
102
102
 
103
103
  "links":[
104
- { "rel": "self",
105
- "href": "companies"
104
+ { "rel": "current",
105
+ "href": "companies/current"
106
106
  }
107
107
  ]
108
108
  }
@@ -0,0 +1,59 @@
1
+ {"type":"object",
2
+ "title": "sub",
3
+ "description": "Subscribe to push notifications(webhooks) about object livecycle events like new, update or delete. Only returns subscription for the current app, so you MUST be using oAuth. When a user removes your app its subs are also deleted. <br/>To register you must supply <br/>- callback_url: receives the object in it's latest state, the user_id & subdomain, company_id, hmac-signed with your app secret.<br/>- channel: defining the event, made up of the SINGULAR object name and the action: invoice.delete, payment.new<br/> - register auth_permissions: An app MUST request non-api permissions f.ex.: clients:create. Perms(on company,app,user,team) are checked before any publishing, so if a user cannot create clients in the interface, his apps will not receive any callbacks.",
4
+ "properties": {
5
+ "id": {
6
+ "identity":true,
7
+ "readonly":true,
8
+ "type":"string"
9
+ },
10
+ "channel": {
11
+ "description": "The channel to which to subscribe. See available at subs/channels.",
12
+ "required":true,
13
+ "type":"string"
14
+ },
15
+ "callback_url": {
16
+ "description": "An url receiving the notification. The url MUST be within the url of the current app",
17
+ "type":"string",
18
+ "required":true
19
+ },
20
+ "method": {
21
+ "description": "HTTP method used for the request, defaults to POST",
22
+ "type":"string",
23
+ "default":"POST",
24
+ "enum":["POST","GET", "DELETE", "PUT"]
25
+ },
26
+ "created_at": {
27
+ "description": "Date the record was created in SK. Never changes afterwards.",
28
+ "format":"date-time",
29
+ "readonly":true,
30
+ "type":"string"
31
+ },
32
+ "updated_at": {
33
+ "description": "Last date when the record was edited.",
34
+ "format":"date-time",
35
+ "readonly":true,
36
+ "type":"string"
37
+ }
38
+ },
39
+
40
+ "links":[
41
+ { "rel": "self",
42
+ "href": "{id}"
43
+ },
44
+ { "rel": "instances",
45
+ "href": "subs/"
46
+ },
47
+ { "rel": "destroy",
48
+ "href": "subs/{id}",
49
+ "method": "DELETE"
50
+ },
51
+ { "rel": "update",
52
+ "href": "subs/{id}",
53
+ "method": "PUT"
54
+ },
55
+ { "rel": "subs/channels",
56
+ "href": "subs/channels"
57
+ }
58
+ ]
59
+ }
data/lib/sk_api_schema.rb CHANGED
@@ -5,6 +5,19 @@ module SK
5
5
  class Schema
6
6
 
7
7
  class << self
8
+
9
+ # class var remembering already read-in schema's
10
+ # {
11
+ # 'v1.0'=>{
12
+ # 'invoice'=>{schema}
13
+ # 'credit_note'=>{schema}
14
+ # }
15
+ # }
16
+ # === Return
17
+ #<Hash>::
18
+ def registry
19
+ @registry ||={}
20
+ end
8
21
  # Read a schema with a given version and return it as hash
9
22
  # See ../json folder for available schema's and versions
10
23
  # === Parameter
@@ -13,11 +26,15 @@ module SK
13
26
  # === Return
14
27
  # <HashWithIndifferentAccess>:: schema as hash
15
28
  def read(schema, version)
29
+ return registry[version][schema] if registry[version] && registry[version][schema]
16
30
  # prefix version with v1.0 of v is not present
17
31
  v = (version =~ /^v/) ? version : "v#{version}"
32
+ registry[v] = {} unless registry[v]
33
+ # read schema from file
18
34
  file_path = File.join(File.dirname(__FILE__), '../json', v, "#{schema}.json")
19
35
  plain_data = File.open(file_path, 'r'){|f| f.read}
20
- ActiveSupport::JSON.decode(plain_data).with_indifferent_access
36
+ # remember
37
+ registry[v][schema] = ActiveSupport::JSON.decode(plain_data).with_indifferent_access
21
38
  end
22
39
 
23
40
  # Read all available schemas from a given version(folder) and return
@@ -83,21 +100,26 @@ module SK
83
100
  data[field] = obj.send(field) if obj.respond_to?(field.to_sym)
84
101
  end
85
102
  end
86
- #add links
87
- { obj_class_name => data,
88
- 'links' => parse_links(obj, schema)
89
- }
103
+ hsh = { obj_class_name => data }
104
+ #add links if present
105
+ links = parse_links(obj, schema)
106
+ links && hsh['links'] = links
107
+ # return hash
108
+ hsh
90
109
  end
91
110
 
92
111
  # Parse the link section of the schema by replacing {id} in urls
93
112
  # === Returns
94
113
  # <Array[Hash{String=>String}]>::
114
+ # <nil>:: no links present
95
115
  def parse_links(obj, schema)
96
116
  links = []
97
117
  schema['links'] && schema['links'].each do |link|
98
118
  links << { 'rel' => link['rel'], 'href' => link['href'].gsub(/\{id\}/, obj.id) }
99
119
  end
100
120
  links.uniq
121
+ # return links only if not empty
122
+ links.empty? ? nil : links
101
123
  end
102
124
 
103
125
  end # class methods
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sk_api_schema}
8
- s.version = "0.0.16"
8
+ s.version = "0.0.17"
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-02-14}
12
+ s.date = %q{2011-03-10}
13
13
  s.description = %q{SalesKing API JSON schema and utility methods}
14
14
  s.email = %q{gl@salesking.eu}
15
15
  s.extra_rdoc_files = [
@@ -38,6 +38,7 @@ Gem::Specification.new do |s|
38
38
  "json/v1.0/payment_reminder.json",
39
39
  "json/v1.0/product.json",
40
40
  "json/v1.0/recurring.json",
41
+ "json/v1.0/sub.json",
41
42
  "json/v1.0/text_template.json",
42
43
  "json/v1.0/user.json",
43
44
  "lib/sk_api_schema.rb",
@@ -38,6 +38,12 @@ describe SK::Api::Schema do
38
38
  }.should raise_error
39
39
  end
40
40
 
41
+ it "should add schema to registry" do
42
+ SK::Api::Schema.read(:credit_note, 'v1.0')
43
+ File.should_not_receive(:open)
44
+ SK::Api::Schema.read(:credit_note, 'v1.0')
45
+ end
46
+
41
47
  end
42
48
 
43
49
  describe SK::Api::Schema, 'object parsing' do
@@ -76,8 +82,8 @@ describe SK::Api::Schema, 'object parsing' do
76
82
  @invoice.client = @client
77
83
  obj_hash = SK::Api::Schema.to_hash_from_schema(@invoice, 'v1.0')
78
84
  obj_hash["invoice"]['client']['client'].should == {"number"=>"911", "addresses"=>[], "id"=>"some-uuid", "organisation"=>"Dirty Food Inc.", "last_name"=>nil}
79
- obj_hash["invoice"]["client"]['links'].should == []
80
- obj_hash["invoice"]["line_items"].should == [ {'links'=>[], "line_item"=>{"name"=>"Pork Chops", "position"=>1, \
85
+ obj_hash["invoice"]["client"]['links'].should_not be_nil
86
+ obj_hash["invoice"]["line_items"].should == [ {"line_item"=>{"name"=>"Pork Chops", "position"=>1, \
81
87
  "id"=>"some-uuid", "description"=>"Yummi Pork chopped by mexian emigrants", "price_single"=>0.99}}]
82
88
  end
83
89
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sk_api_schema
3
3
  version: !ruby/object:Gem::Version
4
- hash: 63
4
+ hash: 61
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 16
10
- version: 0.0.16
9
+ - 17
10
+ version: 0.0.17
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-02-14 00:00:00 +01:00
18
+ date: 2011-03-10 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -77,6 +77,7 @@ files:
77
77
  - json/v1.0/payment_reminder.json
78
78
  - json/v1.0/product.json
79
79
  - json/v1.0/recurring.json
80
+ - json/v1.0/sub.json
80
81
  - json/v1.0/text_template.json
81
82
  - json/v1.0/user.json
82
83
  - lib/sk_api_schema.rb