apiql 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03deaa31d43b9af72802ed8c1b3ccea05452acabac5ea8e9264a643cd89984d8
4
- data.tar.gz: ffed0696e116b50271068581a582922f69f020de02809a36685e57677bd8c92d
3
+ metadata.gz: def514d5f185be467c6fe1d3cb0a45f1713ff47950d366f40c4830268064cf6b
4
+ data.tar.gz: 6e23322d7c596b40ebd2740bdd3e98ec961b88f1d2825bec8ecc829bb01e63b8
5
5
  SHA512:
6
- metadata.gz: 0b4e55589c3945aab740d933b96a9a11a429c71c1e16ff98f781e9ddd84cae6cda193ef108235a89baf14e3d947687df6eaa52bed3ae184d3a9e130d2556421a
7
- data.tar.gz: 1a8aba4a9920736810238cd133475960820d54580531bfc2850e59d6b564b7ee0e12a4318bc86fbc86638cef6a54850cbcf4fc0e6bef0ebf30e17f9bf1b573d9
6
+ metadata.gz: 5c4253fe05c60846ddb813e7c09ef431d6d21d6574ecb9f3660a1d6a9cdc4ef128debe34fa1d601bd42eec8eb64a218a06e55f14eb29ef72b682bd97c7d7e6c4
7
+ data.tar.gz: c93a2ffeafe521a9eccf3e27bd6b452fd1878ad2aff2d18652f71e0597e09981e3ef8059d3f8173d94312c535d3afb986eac184929d2835c939789b8e65e6d66
data/README.md CHANGED
@@ -36,17 +36,17 @@ def user
36
36
  end
37
37
 
38
38
  ```
39
- variables `session`, `current_user` and `params` will be stored into context you can use in presenters and handlers
39
+ variables `session`, `current_user` and `params` (you can list any you need) will be stored into context you can use in presenters and handlers
40
40
 
41
41
  Define presenters for your models:
42
42
 
43
43
  ```ruby
44
44
  class User < ApplicationRecord
45
45
  class Entity < ::APIQL::Entity
46
- attributes :full_name, :email, :token, :role, :roles
46
+ attributes :full_name, :email, :token, :role, :roles # any attributes, methods or associations
47
47
 
48
- def token
49
- object.token if object == current_user
48
+ def token # if defined, method will be called instead of attribute
49
+ object.token if object == current_user # directly access to current_user from context
50
50
  end
51
51
  end
52
52
 
@@ -66,28 +66,34 @@ APIQL.endpoint = "/"
66
66
  ```
67
67
 
68
68
  ```javascript
69
+ // function apiql(endpoint, schema, params = {}, form = null) -- schema is cached, so entire request is passed only for first time, later - short hashes only
70
+
69
71
  authenticate(email, password) {
70
- let api = new APIQL("user")
71
- api.call(`
72
+ apiql("user", `
72
73
  logout()
73
- authenticate(email,password) {
74
+
75
+ authenticate(email, password) {
74
76
  token
75
77
  }
78
+
76
79
  me {
77
80
  email full_name role token
81
+
82
+ roles {
83
+ id title
84
+ }
78
85
  }
79
86
  `, {
80
- email: email,
87
+ email: email, // these var will be passed into methods on the server side
81
88
  password: password
82
89
  })
83
- .then(response => {
90
+ .then(response => { // response contains results of called methods
84
91
  let user = response.me
85
92
  })
86
93
  }
87
94
 
88
95
  logout() {
89
- let api = new APIQL("user")
90
- api.call(`
96
+ apiql("user", `
91
97
  logout
92
98
  `)
93
99
  .then(response => {
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'apiql'
7
- spec.version = '0.1.7'
7
+ spec.version = '0.2.0'
8
8
  spec.authors = ['Dmitry Silchenko']
9
9
  spec.email = ['dmitry@desofto.com']
10
10
 
@@ -1,7 +1,6 @@
1
- class APIQL {
2
- constructor(endpoint) {
3
- this.endpoint = endpoint
4
- }
1
+ const APIQL = {
2
+ on_error: null,
3
+ endpoint: '',
5
4
 
6
5
  hash(s) {
7
6
  let hash = 0, i, chr
@@ -12,9 +11,9 @@ class APIQL {
12
11
  hash |= 0
13
12
  }
14
13
  return hash
15
- }
14
+ },
16
15
 
17
- call(schema, params = {}, form = null) {
16
+ call(endpoint, schema, params = {}, form = null) {
18
17
  return new Promise((resolve, reject) => {
19
18
  if(params instanceof FormData) {
20
19
  form = params
@@ -33,13 +32,14 @@ class APIQL {
33
32
  params.apiql = this.hash(schema)
34
33
  }
35
34
 
36
- Vue.http.post(`${APIQL.endpoint}${this.endpoint}`, params)
35
+ Vue.http.post(`${APIQL.endpoint}${endpoint}`, params)
37
36
  .then(response => {
38
37
  resolve(response.body)
39
38
  })
40
39
  .catch(response => {
41
- if(response.status == 401 && APIQL.unauthenticated) {
42
- APIQL.unauthenticated()
40
+ if(response.status == 401 && APIQL.on_error) {
41
+ APIQL.on_error(response)
42
+ return
43
43
  }
44
44
 
45
45
  if(form) {
@@ -48,17 +48,22 @@ class APIQL {
48
48
  params.apiql_request = schema
49
49
  }
50
50
 
51
- Vue.http.post(`${APIQL.endpoint}${this.endpoint}`, form || params)
51
+ Vue.http.post(`${APIQL.endpoint}${endpoint}`, form || params)
52
52
  .then(response => {
53
53
  resolve(response.body)
54
54
  })
55
55
  .catch(response => {
56
- alert(response.body.errors)
56
+ if(APIQL.on_error) {
57
+ APIQL.on_error(response)
58
+ } else {
59
+ alert(response.body)
60
+ }
57
61
  })
58
62
  })
59
63
  })
60
64
  }
61
65
  }
62
66
 
63
- APIQL.unauthenticated = null
64
- APIQL.endpoint = ''
67
+ function apiql(endpoint, schema, params = {}, form = null) {
68
+ return APIQL.call(endpoint, schema, params, form)
69
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apiql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Silchenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-04 00:00:00.000000000 Z
11
+ date: 2018-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler