ecoportal-api 0.2.2 → 0.3.0.pre1

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: 0a48c4137d9f3a6fd2c2089e984257ee11122bf14e62ab67d8d9d5d5450cd4ec
4
- data.tar.gz: cf156327767d58bf8b6db87793122fa1790ef8f4ddcf343d86d7f866a395e148
3
+ metadata.gz: 83c71a815e9029a177b05a72733165c2a4d4868a8df3c9ef6c93f27a7a5e2458
4
+ data.tar.gz: 57e7f0759ca52ad93150da122fd92220ad043668b4402128c8550c284c90f91b
5
5
  SHA512:
6
- metadata.gz: 2b63ff282e00e7d550cc90d350712505ab57d258f84365b13f8484be00c440f01c9b98183dcbecad42e124ca714076277a6ffd668cea997125496e3ee852530e
7
- data.tar.gz: 162e54978d470815a3c2c43e3f9c39b84a5bb591b50567e3e338e26ef9715eca2a91a1a4fa76e72fb70658af2e45aafa36d2a6ac0b4b00c9de71f9ad83491f17
6
+ metadata.gz: bf01726252c61adbb66f8ff78f4b0d5c05efec942b3248ef64b92df8e66c2d105e886d9e7bbb61f4d305d233359e73706659ed40522e5114387f5736e83572f9
7
+ data.tar.gz: e2b12acff4f9fbf1fdd6841ccce074c4c41ff330aa5f59de7184462b29f8642b8afa467babbc80583f0f91f5e7bb0c52c34b39c3499ddfb304d03a9173477d25
@@ -1,4 +1,5 @@
1
1
  require "cgi"
2
+ require "logger"
2
3
  require "hash-polyfill"
3
4
  require "ecoportal/api/version"
4
5
 
@@ -5,6 +5,7 @@ module Ecoportal
5
5
  end
6
6
  end
7
7
  require 'ecoportal/api/common/client'
8
+ require 'ecoportal/api/common/logging'
8
9
  require 'ecoportal/api/common/response'
9
10
  require 'ecoportal/api/common/wrapped_response'
10
11
  require 'ecoportal/api/common/base_model'
@@ -3,37 +3,52 @@ module Ecoportal
3
3
  module API
4
4
  module Common
5
5
  class Client
6
- def initialize(api_key:, version: "v1", host: "live.ecoportal.com")
6
+ def initialize(api_key:, version: "v1", host: "live.ecoportal.com", logger: nil)
7
7
  @version = version
8
8
  @api_key = api_key
9
+ @logger = logger
9
10
  if host.match(/^localhost|^127\.0\.0\.1/)
10
11
  @base_uri = "http://#{host}/api/"
11
12
  else
12
13
  @base_uri = "https://#{host}/api/"
13
14
  end
15
+ log(:info) { "#{version} client initialized pointing at #{host}" }
16
+ log(:info) { "Api-key: #{@api_key}" }
17
+ end
18
+
19
+ def log(level, &block)
20
+ @logger.send(level, &block) if @logger
14
21
  end
15
22
 
16
23
  def get(path, params: {})
17
- request do |http|
18
- http.get(url_for(path), params: params)
24
+ instrument("GET", path, params) do
25
+ request do |http|
26
+ http.get(url_for(path), params: params)
27
+ end
19
28
  end
20
29
  end
21
30
 
22
31
  def post(path, data:)
23
- request do |http|
24
- http.post(url_for(path), json: data)
32
+ instrument("POST", path, data) do
33
+ request do |http|
34
+ http.post(url_for(path), json: data)
35
+ end
25
36
  end
26
37
  end
27
38
 
28
39
  def patch(path, data:)
29
- request do |http|
30
- http.patch(url_for(path), json: data)
40
+ instrument("PATCH", path, data) do
41
+ request do |http|
42
+ http.patch(url_for(path), json: data)
43
+ end
31
44
  end
32
45
  end
33
46
 
34
47
  def delete(path)
35
- request do |http|
36
- http.delete(url_for(path))
48
+ instrument("DELETE", path) do
49
+ request do |http|
50
+ http.delete(url_for(path))
51
+ end
37
52
  end
38
53
  end
39
54
 
@@ -52,6 +67,29 @@ module Ecoportal
52
67
  def url_for(path)
53
68
  @base_uri+@version+path
54
69
  end
70
+
71
+ private
72
+
73
+ def instrument(method, path, data = nil)
74
+ start_time = Time.now.to_f
75
+ log :info do
76
+ "GET #{url_for(path)}"
77
+ end
78
+ JSON.pretty_generate(data).lines.each.with_index do |line, index|
79
+ line = "Data: " + line if index.zero?
80
+ log(:info) { line.chomp }
81
+ end
82
+ yield.tap do |result|
83
+ end_time = Time.now.to_f
84
+ log :info do
85
+ "Took %.2fs, Status #{result.status}" % (end_time - start_time)
86
+ end
87
+ JSON.pretty_generate(result.body).lines.each.with_index do |line, index|
88
+ line = "Response: " + line if index.zero?
89
+ log(:debug) { line.chomp }
90
+ end
91
+ end
92
+ end
55
93
  end
56
94
  end
57
95
  end
@@ -0,0 +1,17 @@
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ module Logging
5
+ private
6
+ def default_logger
7
+ Logger.new(STDOUT).tap do |_logger|
8
+ _logger.formatter = proc do |severity, datetime, progname, msg|
9
+ "#{severity.to_s[0]}: #{datetime.strftime("%Y-%m-%d %H:%M:%S")} > #{msg}\n"
10
+ end
11
+ _logger.level = Logger::DEBUG
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,12 +1,15 @@
1
1
  module Ecoportal
2
2
  module API
3
3
  class Internal
4
- attr_reader :client
5
- def initialize(api_key, host: "live.ecoportal.com")
4
+ include Common::Logging
5
+ attr_reader :client, :logger
6
+ def initialize(api_key, host: "live.ecoportal.com", logger: default_logger)
7
+ @logger = logger
6
8
  @client = Common::Client.new(
7
9
  api_key: api_key,
8
10
  host: host,
9
- version: "v0"
11
+ version: "v0",
12
+ logger: @logger
10
13
  )
11
14
  end
12
15
  def people
@@ -28,4 +31,4 @@ end
28
31
  require 'ecoportal/api/internal/people'
29
32
  require 'ecoportal/api/internal/person_schemas'
30
33
  require 'ecoportal/api/internal/policy_groups'
31
- require 'ecoportal/api/internal/login_providers'
34
+ require 'ecoportal/api/internal/login_providers'
@@ -1,12 +1,15 @@
1
1
  module Ecoportal
2
2
  module API
3
3
  class V1
4
- attr_reader :client
5
- def initialize(api_key, host: "live.ecoportal.com")
4
+ include Common::Logging
5
+ attr_reader :client, :logger
6
+ def initialize(api_key, host: "live.ecoportal.com", logger: default_logger)
7
+ @logger = logger
6
8
  @client = Common::Client.new(
7
9
  api_key: api_key,
8
10
  host: host,
9
- version: "v1"
11
+ version: "v1",
12
+ logger: @logger
10
13
  )
11
14
  end
12
15
  def people
@@ -1,5 +1,5 @@
1
1
  module Ecoportal
2
2
  module API
3
- VERSION = "0.2.2"
3
+ VERSION = "0.3.0.pre1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecoportal-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tapio Saarinen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-21 00:00:00.000000000 Z
11
+ date: 2018-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -106,6 +106,7 @@ files:
106
106
  - lib/ecoportal/api/common/client.rb
107
107
  - lib/ecoportal/api/common/doc_helpers.rb
108
108
  - lib/ecoportal/api/common/hash_diff.rb
109
+ - lib/ecoportal/api/common/logging.rb
109
110
  - lib/ecoportal/api/common/response.rb
110
111
  - lib/ecoportal/api/common/wrapped_response.rb
111
112
  - lib/ecoportal/api/internal.rb
@@ -149,9 +150,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
149
150
  version: '0'
150
151
  required_rubygems_version: !ruby/object:Gem::Requirement
151
152
  requirements:
152
- - - ">="
153
+ - - ">"
153
154
  - !ruby/object:Gem::Version
154
- version: '0'
155
+ version: 1.3.1
155
156
  requirements: []
156
157
  rubyforge_project:
157
158
  rubygems_version: 2.7.6