apisync 0.1.6 → 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
  SHA1:
3
- metadata.gz: 6cce9df4729e3a774cf37ff0a4105f6d35959639
4
- data.tar.gz: f867abbe3f8ae536365851eca6826613db5b1aeb
3
+ metadata.gz: 6d0eba4134c51dcf6e3a7c1727db5fbad9dcbab6
4
+ data.tar.gz: 531ee71f557836f25dc58db9671e327bf6a476ad
5
5
  SHA512:
6
- metadata.gz: 6f0539eb5b4a53299acd5b92844d67e01638b47966ff7dd61a18ccfcac2779362e3e2212d8c2b47c09ec7156c5f66ce905b696fc6129076e3b9552b6a7759c6c
7
- data.tar.gz: 480b7c6c67dcbf740b5844a94aa9375614336eb884920130b9e0e0d203d8a1d9bba955f5f24432895c2e4aeeba21441d472da4dfe3c53bdf6bb215f5ba579454
6
+ metadata.gz: b6d04b2709ef4f99351fefce81e93f54bc25d6fa8703483d3cbaf50734b6badb475d0708bc0315941f143f047d1e6f12f4be127c1d964170cce6b58b13939c87
7
+ data.tar.gz: 68a6179d0c0b8684355c1d6a44bcd241f5709a4990458f6a728ac390f857be39a84003eeef62512e597044865bb8bc1419aeb49a305b6fa83cdc88e6fcbc30dd
data/README.md CHANGED
@@ -39,7 +39,7 @@ client.inventory_items.save({
39
39
  For details on the attributes, see the
40
40
  [API Reference documentation](https://docs.apisync.io/api/).
41
41
 
42
- You can also define a global API key:
42
+ You can also define the API key globally:
43
43
 
44
44
  ```ruby
45
45
  Apisync.api_key = "my-key"
@@ -48,6 +48,32 @@ Apisync.api_key = "my-key"
48
48
  client = Apisync.new
49
49
  ```
50
50
 
51
+ #### Verbose
52
+
53
+ If you want to output to `$stdout`, set `verbose: true`:
54
+
55
+ ```rb
56
+ # Define in the instance
57
+ client = Apisync.new(api_key: 'api-key', verbose: true)
58
+
59
+ # or define it globally
60
+ Apisync.verbose = true
61
+ ```
62
+
63
+ That is useful on Rails console in development mode, so you know what requests
64
+ are being made
65
+
66
+ **Logger**
67
+
68
+ You can inject a logger as well:
69
+
70
+ ```rb
71
+ Apisync.logger = Logger.new($stdout)
72
+
73
+ # or
74
+ Apisync.logger = Rails.logger
75
+ ```
76
+
51
77
  ### Responses
52
78
 
53
79
  All HTTP calls return an
@@ -1,5 +1,7 @@
1
1
  require "bundler/setup"
2
2
  require "httparty"
3
+ require "securerandom"
4
+ require "logger"
3
5
 
4
6
  require "apisync/version"
5
7
  require "apisync/exceptions"
@@ -12,9 +14,19 @@ class Apisync
12
14
  @@api_key = nil
13
15
  @@host = nil
14
16
 
15
- def initialize(api_key: nil)
17
+ # Verbose will do $stdout.puts. That's useful on a Rails console in
18
+ # development, where logger output is ommited.
19
+ @@verbose = false
20
+ @@logger = ::Logger.new(IO::NULL)
21
+
22
+ attr_accessor :verbose
23
+
24
+ def initialize(api_key: nil, verbose: nil, logger: nil)
16
25
  @api_key = api_key || @@api_key
17
26
  @host = @@host
27
+ @verbose = verbose || @@verbose
28
+
29
+ @logger = logger || @@logger
18
30
 
19
31
  raise ArgumentError, "missing keyword: api_key" if @api_key.nil?
20
32
  end
@@ -22,8 +34,11 @@ class Apisync
22
34
  def method_missing(name, args = {}, &block)
23
35
  # overrides the instance api_key as `authorization`
24
36
  options = {
25
- host: @host
37
+ host: @host,
38
+ verbose: @verbose,
39
+ logger: @logger
26
40
  }.merge(args).merge(api_key: @api_key)
41
+
27
42
  Apisync::Resource.new(name, options)
28
43
  end
29
44
 
@@ -34,4 +49,16 @@ class Apisync
34
49
  def self.api_key=(value)
35
50
  @@api_key = value
36
51
  end
52
+
53
+ def self.verbose=(value)
54
+ @@verbose = value
55
+ end
56
+
57
+ def self.logger=(value)
58
+ @@logger = value
59
+ end
60
+
61
+ def self.verbose?
62
+ !!@@verbose
63
+ end
37
64
  end
@@ -10,38 +10,63 @@ class Apisync
10
10
  def initialize(resource_name:, options: {})
11
11
  @resource_name = resource_name
12
12
  @options = options
13
+ @logger = options[:logger]
13
14
  end
14
15
 
15
16
  def post(data:, headers: {})
17
+ request_body = {data: payload_from_data(data)}
18
+ url = request_url
19
+ header = request_header.merge(headers)
20
+
21
+ output_verbose_request(url, request_body, header)
22
+
16
23
  wrap_response(HTTParty.post(
17
- url,
18
- body: {data: payload_from_data(data)}.to_json,
19
- headers: header.merge(headers)
24
+ request_url,
25
+ body: request_body.to_json,
26
+ headers: header
20
27
  ))
21
28
  end
22
29
 
23
30
  def put(id:, data:, headers: {})
24
31
  raise Apisync::UrlAndPayloadIdMismatch unless id == data[:id]
25
32
 
33
+ request_body = {data: payload_from_data(data)}
34
+ url = request_url(id: id)
35
+ header = request_header.merge(headers)
36
+
37
+ output_verbose_request(url, request_body, header)
38
+
26
39
  wrap_response(HTTParty.put(
27
- url(id: id),
28
- body: {data: payload_from_data(data)}.to_json,
29
- headers: header.merge(headers)
40
+ url,
41
+ body: request_body.to_json,
42
+ headers: header
30
43
  ))
31
44
  end
32
45
 
33
46
  def get(id: nil, filters: nil, headers: {})
34
47
  raise Apisync::InvalidFilter if !filters.nil? && !filters.is_a?(Hash)
35
48
 
49
+ url = request_url(id: id, filters: filters)
50
+ output_verbose_request(url)
51
+
36
52
  wrap_response(HTTParty.get(
37
- url(id: id, filters: filters),
38
- headers: header.merge(headers)
53
+ url,
54
+ headers: request_header.merge(headers)
39
55
  ))
40
56
  end
41
57
 
42
58
  private
43
59
 
44
- def url(id: nil, filters: nil)
60
+ def verbose?
61
+ @options.fetch(:verbose, false)
62
+ end
63
+
64
+ def output_verbose_request(url, body = nil, headers = nil)
65
+ log("[APISync] Request URL: #{url}")
66
+ log("[APISync] Payload: #{body.to_json}")if body
67
+ end
68
+
69
+ def request_url(id: nil, filters: nil)
45
70
  Apisync::Http::Url.new(
46
71
  resource_name: @resource_name,
47
72
  id: id,
@@ -50,8 +75,9 @@ class Apisync
50
75
  ).to_s
51
76
  end
52
77
 
53
- def header
78
+ def request_header
54
79
  final = HEADER
80
+ final = final.merge("X-Request-Id" => ::SecureRandom.uuid)
55
81
  if @options[:api_key]
56
82
  final = final.merge("Authorization" => "ApiToken #{@options[:api_key]}")
57
83
  end
@@ -73,10 +99,25 @@ class Apisync
73
99
 
74
100
  def wrap_response(response)
75
101
  if response.code.to_i == 429
102
+ if verbose?
103
+ log "[APISync] Response: 429 Too many requests at once, slow down."
104
+ end
76
105
  raise Apisync::TooManyRequests
77
106
  else
107
+ if verbose?
108
+ msg = "[APISync] Response: #{response.code}"
109
+ if response.body != ""
110
+ msg << " #{response.body}"
111
+ end
112
+ log msg
113
+ end
78
114
  response
79
115
  end
80
116
  end
117
+
118
+ def log(msg)
119
+ $stdout.puts(msg) if verbose?
120
+ @logger.info(msg)
121
+ end
81
122
  end
82
123
  end
@@ -1,3 +1,3 @@
1
1
  class Apisync
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apisync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre de Oliveira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-13 00:00:00.000000000 Z
11
+ date: 2018-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty