ps_utilities 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b53435e5d2f6da0cd21f54f5780806986b87e7ff36b963702e4cd1009862e7d
4
- data.tar.gz: 0f3133a755d4f2f67e8bde12bbb3769969afe60015f609b672166582b06d2898
3
+ metadata.gz: 637da17d1d419b00baea629e618529517c09fa7e16f71dd358a850b0978fc599
4
+ data.tar.gz: a1ec6ced4f33ac5ff849dd48c0f216133e7fe291b1224c8dc082d42c16a66876
5
5
  SHA512:
6
- metadata.gz: 0b39f2d31153c8fc135d6fc39a415c55f8a347f4c97f0de18b91bec030f99d182e9baa655de000a29996d01997b2e44023f6d55d4f0b02e6187f254fd3f555b3
7
- data.tar.gz: 87407650b7062b75f4ae4158bd48103a2acc302472923ad4d0456fbb900dc4a07191588df67621f028bcdf6e8d18aadf908dcfe8c666467335ecb872f68a6ea1
6
+ metadata.gz: 062a02dd9e855ad0533da5eb6c735021a0607ea6a4db59d33f063b223d0c3918e99071760bb456b9316f81b58e5daf1e06b977c92fbe02b0aa035a81d2841244
7
+ data.tar.gz: cca916c4339884397ebf5cc40fd6dcd1f8f0ff6d6a17b3cafdf61f30fccf0584ecab4f6bec7b58c7dc3d476b6168810155780dd3a61832861f9958519f2b3690
@@ -24,7 +24,7 @@ module PsUtilities
24
24
  # @note You should use environment variables to initialize your server.
25
25
  class Connection
26
26
 
27
- attr_reader :credentials, :headers
27
+ attr_reader :credentials, :headers, :base_uri, :auth_path, :version
28
28
 
29
29
  include PsUtilities::PreBuiltGet
30
30
  include PsUtilities::PreBuiltPut
@@ -33,6 +33,9 @@ module PsUtilities
33
33
  def initialize(attributes: {}, headers: {})
34
34
  @credentials = attr_defaults.merge(attributes)
35
35
  @headers = header_defaults.merge(headers)
36
+ @base_uri = credentials[:base_uri]
37
+ @auth_path = credentials[:auth_endpoint]
38
+ @version = "v#{PsUtilities::Version::VERSION}"
36
39
 
37
40
  raise ArgumentError, "missing client_secret" if credentials[:client_secret].nil? or
38
41
  credentials[:client_secret].empty?
@@ -43,13 +46,13 @@ module PsUtilities
43
46
  end
44
47
 
45
48
  # with no command it just authenticates
46
- def run(command: nil, params: {}, url: nil, options: {})
49
+ def run(command: nil, params: {}, api_path: "", options: {})
47
50
  authenticate unless token_valid?
48
51
  case command
49
52
  when nil, :authenticate
50
53
  # authenticate unless token_valid?
51
54
  when :get, :put, :post
52
- send(command, url, options) unless url.empty?
55
+ send(command, api_path, options) unless api_path.empty?
53
56
  else
54
57
  send(command, params)
55
58
  end
@@ -58,17 +61,17 @@ module PsUtilities
58
61
  private
59
62
 
60
63
  # options = {query: {}}
61
- def get(url, options={})
62
- max_retries = 3
63
- times_retried = 0
64
+ def get(api_path, options={})
65
+ count = 0
66
+ retries = 3
67
+ ps_url = base_uri + api_path
64
68
  options = options.merge(headers)
65
- ps_url = credentials[:base_uri] + url
66
69
  begin
67
70
  HTTParty.get(ps_url, options)
68
71
  # self.class.get(url, query: options[:query], headers: options[:headers])
69
72
  rescue Net::ReadTimeout, Net::OpenTimeout
70
- if times_retried < max_retries
71
- times_retried += 1
73
+ if count < retries
74
+ count += 1
72
75
  retry
73
76
  else
74
77
  { error: "no response (timeout) from URL: #{url}" }
@@ -77,17 +80,17 @@ module PsUtilities
77
80
  end
78
81
 
79
82
  # options = {body: {}}
80
- def put(url, options={})
81
- max_retries = 3
82
- times_retried = 0
83
+ def put(api_path, options={})
84
+ count = 0
85
+ retries = 3
86
+ ps_url = base_uri + api_path
83
87
  options = options.merge(headers)
84
- ps_url = credentials[:base_uri] + url
85
88
  begin
86
- HTTParty.put(ps_url, options )
87
- # self.class.get(url, body: options[:body], headers: options[:headers])
89
+ HTTParty.get(ps_url, options)
90
+ # self.class.get(url, query: options[:query], headers: options[:headers])
88
91
  rescue Net::ReadTimeout, Net::OpenTimeout
89
- if times_retried < max_retries
90
- times_retried += 1
92
+ if count < retries
93
+ count += 1
91
94
  retry
92
95
  else
93
96
  { error: "no response (timeout) from URL: #{url}" }
@@ -96,17 +99,17 @@ module PsUtilities
96
99
  end
97
100
 
98
101
  # options = {body: {}}
99
- def post(url, options={})
100
- max_retries = 3
101
- times_retried = 0
102
+ def post(api_path, options={})
103
+ count = 0
104
+ retries = 3
105
+ ps_url = base_uri + api_path
102
106
  options = options.merge(headers)
103
- ps_url = credentials[:base_uri] + url
104
107
  begin
105
- HTTParty.post(ps_url, options )
106
- # self.class.get(url, body: options[:body], headers: options[:headers])
108
+ HTTParty.get(ps_url, options)
109
+ # self.class.get(url, query: options[:query], headers: options[:headers])
107
110
  rescue Net::ReadTimeout, Net::OpenTimeout
108
- if times_retried < max_retries
109
- times_retried += 1
111
+ if count < retries
112
+ count += 1
110
113
  retry
111
114
  else
112
115
  { error: "no response (timeout) from URL: #{url}" }
@@ -117,7 +120,8 @@ module PsUtilities
117
120
  # In PowerSchool go to System>System Settings>Plugin Management Configuration>your plugin>Data Provider Configuration to manually check plugin expiration date
118
121
  def authenticate
119
122
  @headers[:headers] ||= {}
120
- ps_url = credentials[:base_uri] + credentials[:auth_endpoint]
123
+ ps_url = base_uri + auth_path
124
+ # ps_url = credentials[:base_uri] + credentials[:auth_endpoint]
121
125
  response = HTTParty.post(ps_url, {headers: auth_headers,
122
126
  body: 'grant_type=client_credentials'})
123
127
 
@@ -139,16 +143,16 @@ module PsUtilities
139
143
  return true
140
144
  end
141
145
 
142
- def auth_headers
146
+ def auth_headers(creds64 = encode_credentials)
143
147
  { 'ContentType' => 'application/x-www-form-urlencoded;charset=UTF-8',
144
148
  'Accept' => 'application/json',
145
- 'Authorization' => 'Basic ' + encode_credentials
149
+ 'Authorization' => 'Basic ' + creds64
146
150
  }
147
151
  end
148
152
 
149
- def encode_credentials
150
- ps_auth_text = [ credentials[:client_id],
151
- credentials[:client_secret]
153
+ def encode_credentials(creds = credentials)
154
+ ps_auth_text = [ creds[:client_id],
155
+ creds[:client_secret]
152
156
  ].join(':')
153
157
  Base64.encode64(ps_auth_text).gsub(/\n/, '')
154
158
  end
@@ -1,5 +1,5 @@
1
1
  module PsUtilities
2
2
  module Version
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ps_utilities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Weisbecker