parasut-v4 1.0.1 → 1.0.2

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
  SHA1:
3
- metadata.gz: 56aa65816aff5c6e8dd257f65fb9fa63e8d292da
4
- data.tar.gz: 4682413a7b3fb8b217c720496a2ab06ae7c684ce
3
+ metadata.gz: c4974936eedcb48fd61edf1fa58d3cc5a882be34
4
+ data.tar.gz: 95bcf0ab3381ed666eed7fc16f1162427bfd385e
5
5
  SHA512:
6
- metadata.gz: 7f179b78048db4d446dcffa53d360f654e577ea39c33ac5ad1a02ad22454e6d516f9fe6e19a4c6d000a21ebee35b2cdfaac1912742de91bb1871a8febdfa5eff
7
- data.tar.gz: b96e5fefeeddfa80f9d2fb94fe37ce29fe96682312936c7342c0f3599045c0b32ae6844897cdcaea743fae828953a9d0ccccc5208d61c1553687226227a2cd36
6
+ metadata.gz: 1ea1ac80d5f9bfa9ae2aa570b52f39b963a9d6cee747f7ca5dde259b28d373ba16652620116c7bc70149a085c01d8afa84109189312bd93e1204d0bf585bf968
7
+ data.tar.gz: 13ea243191a8dabd1e771a6969d65f72e5f05bea6acb7e17a58eabdc340c6bc8aceef4e2ff3dd7533d470f5e853b9bd9beae59c92e9062ec79ccc269e684ccb7
data/README.md CHANGED
@@ -62,6 +62,40 @@ To paginate collections:
62
62
  # Pagination uses kaminari. (https://github.com/kaminari/kaminari)
63
63
 
64
64
 
65
+
66
+
67
+
68
+ ## HttpClient
69
+
70
+
71
+ Parasut-v4 provides an http client integration to execute tasks manually.
72
+ You can make your calls simply providing endpoint, body and headers. It automatically handles authentication and handles your request.
73
+
74
+ This is an http client, hence it does not manipulate the response into objects.
75
+
76
+ Making get requests:
77
+
78
+ # Parasut::HttpClient.get(endpoint, query_parameters, headers, parse_json=true)
79
+ # parse_json tries to parse the response body into hash
80
+ contact=Parasut::HttpClient.get("contacts/123")
81
+ contact[:data][:attributes][:name]
82
+
83
+ Executing post calls:
84
+
85
+ # Parasut::HttpClient.post(endpoint, body, headers, parse_json=true)
86
+ contact=Parasut::HttpClient.post("contacts", {
87
+ data:{
88
+ type: :contacts,
89
+ attributes:{
90
+ name: "Test From Api",
91
+ account_type: :customer
92
+ }
93
+ }
94
+ )
95
+
96
+
97
+
98
+
65
99
  ## ENVs
66
100
 
67
101
  - PARASUT_API_BASE_URL
@@ -2,50 +2,60 @@ module Her
2
2
  module Middleware
3
3
  # OAuthProviderHeader
4
4
  class OAuthProviderHeader < Faraday::Middleware
5
- def add_header(headers)
6
- headers.merge! Authorization: "Bearer #{refresh_token['access_token']}"
7
- end
5
+
6
+ class << self
7
+
8
+ def add_header(headers)
9
+ headers.merge! Authorization: "Bearer #{refresh_token['access_token']}"
10
+ end
8
11
 
9
- def refresh_token
10
- uri = URI("#{token_url}?#{URI.encode_www_form(refresh_token_params)}")
11
- res = Net::HTTP.post_form(uri, {})
12
- JSON.parse(res.body)
13
- end
12
+ def refresh_token
13
+ uri = URI("#{token_url}?#{URI.encode_www_form(refresh_token_params)}")
14
+ res = Net::HTTP.post_form(uri, {})
15
+ JSON.parse(res.body)
16
+ end
14
17
 
15
- def refresh_token_params
16
- {
17
- client_id: Parasut.options.client_id,
18
- client_secret: Parasut.options.client_secret,
19
- grant_type: 'refresh_token',
20
- refresh_token: password['refresh_token']
21
- }
22
- end
18
+ def refresh_token_params
19
+ {
20
+ client_id: Parasut.options.client_id,
21
+ client_secret: Parasut.options.client_secret,
22
+ grant_type: 'refresh_token',
23
+ refresh_token: password['refresh_token']
24
+ }
25
+ end
23
26
 
24
- def password_params
25
- {
26
- client_id: Parasut.options.client_id,
27
- client_secret: Parasut.options.client_secret,
28
- username: Parasut.options.username,
29
- password: Parasut.options.password,
30
- grant_type: 'password',
31
- redirect_uri: 'urn:ietf:wg:oauth:2.0:oob'
32
- }
33
- end
27
+ def password_params
28
+ {
29
+ client_id: Parasut.options.client_id,
30
+ client_secret: Parasut.options.client_secret,
31
+ username: Parasut.options.username,
32
+ password: Parasut.options.password,
33
+ grant_type: 'password',
34
+ redirect_uri: 'urn:ietf:wg:oauth:2.0:oob'
35
+ }
36
+ end
34
37
 
35
- def token_url
36
- "#{Parasut.options.api_base_url}/oauth/token"
37
- end
38
+ def token_url
39
+ "#{Parasut.options.api_base_url}/oauth/token"
40
+ end
41
+
42
+ def password
43
+ uri = URI("#{token_url}?#{URI.encode_www_form(password_params)}")
44
+ res = Net::HTTP.post_form(uri, {})
45
+ JSON.parse(res.body)
46
+ end
47
+
48
+
38
49
 
39
- def password
40
- uri = URI("#{token_url}?#{URI.encode_www_form(password_params)}")
41
- res = Net::HTTP.post_form(uri, {})
42
- JSON.parse(res.body)
43
50
  end
51
+ #end static
44
52
 
45
53
  def call(env)
46
- add_header(env[:request_headers])
54
+ Her::Middleware::OAuthProviderHeader.add_header(env[:request_headers])
47
55
  @app.call(env)
48
56
  end
57
+
58
+
49
59
  end
50
60
  end
51
61
  end
@@ -23,6 +23,7 @@ require_relative 'her/model/pagination'
23
23
 
24
24
  # Models that are including Her::JsonApi::Model has to be required after Her initialization
25
25
  def require_parasut
26
+ require_relative 'parasut/http_client'
26
27
  require_relative 'parasut/version'
27
28
  require_relative 'parasut/product'
28
29
  require_relative 'parasut/document_status'
@@ -4,6 +4,8 @@ module Parasut
4
4
  class Contact
5
5
  include Her::JsonApi::Model
6
6
 
7
+ include_root_in_json true
8
+ parse_root_in_json true
7
9
 
8
10
  # Relations
9
11
  has_many :outstanding_payments, class_name: 'Payment'
@@ -0,0 +1,67 @@
1
+ # lib/parasut/http_client.rb
2
+ module Parasut
3
+ class HttpClient
4
+
5
+ class << self
6
+
7
+ # The url base
8
+ @@url_base="#{Parasut.options.api_base_url}/v4/#{Parasut.options.company_id}"
9
+
10
+ def handle_headers(headers)
11
+
12
+ # Append json content type if not exists
13
+ headers.merge!({'Content-type': 'application/json'}) if headers['Content-type'].nil?
14
+
15
+ # Handle access token header
16
+ Her::Middleware::OAuthProviderHeader.add_header(headers)
17
+
18
+ end
19
+ # end handle_headers
20
+
21
+
22
+ def post(endpoint, body={}, headers={}, parse_json=true)
23
+
24
+ # The url
25
+ url="#{@@url_base}/#{endpoint}"
26
+
27
+ handle_headers(headers)
28
+
29
+ # Convert body to json if hash provided
30
+ body=body.to_json if body.is_a?(Hash)
31
+
32
+ # Execute the request
33
+ request=Faraday.post(url, body, headers)
34
+
35
+ # Return
36
+ parse_json ? JSON.parse(request.body).with_indifferent_access : request.body
37
+
38
+ end
39
+ # end post
40
+
41
+
42
+
43
+ def get(endpoint, query_parameters={}, headers={}, parse_json=true)
44
+
45
+ # The url
46
+ url="#{@@url_base}/#{endpoint}"
47
+
48
+ handle_headers(headers)
49
+
50
+ # Request
51
+ request=Faraday.get(url, query_parameters, headers)
52
+
53
+ # Return
54
+ parse_json ? JSON.parse(request.body).with_indifferent_access : request.body
55
+
56
+ end
57
+ # end get
58
+
59
+
60
+
61
+
62
+ end
63
+ # end static
64
+
65
+ end
66
+ end
67
+
@@ -7,6 +7,7 @@ module Parasut
7
7
  include_root_in_json true
8
8
  parse_root_in_json true
9
9
 
10
+ has_one :contact
10
11
  has_one :e_document_status, class_name: 'DocumentStatus'
11
12
 
12
13
  def e_document_type
@@ -1,3 +1,3 @@
1
1
  module Parasut
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parasut-v4
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dijital Garaj
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: exe
13
13
  cert_chain: []
14
- date: 2019-09-12 00:00:00.000000000 Z
14
+ date: 2019-09-13 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: bundler
@@ -182,6 +182,7 @@ files:
182
182
  - lib/parasut/account.rb
183
183
  - lib/parasut/contact.rb
184
184
  - lib/parasut/document_status.rb
185
+ - lib/parasut/http_client.rb
185
186
  - lib/parasut/item_category.rb
186
187
  - lib/parasut/options.rb
187
188
  - lib/parasut/payment.rb
@@ -210,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
211
  version: '0'
211
212
  requirements: []
212
213
  rubyforge_project:
213
- rubygems_version: 2.6.14
214
+ rubygems_version: 2.6.14.4
214
215
  signing_key:
215
216
  specification_version: 4
216
217
  summary: Parasut client.