blomming_api 0.4.2 → 0.4.3

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
  SHA1:
3
- metadata.gz: 40ff555448ed2878816e332e6317be02736c8687
4
- data.tar.gz: 65c583d94cc89a3bc7e132dbad16ad3b119b2bb4
3
+ metadata.gz: 9c07beb77f338aba29431d8118ddd2c0ce5af7ad
4
+ data.tar.gz: 46c94be3f7d4c553293f309be85a0f637ec537a8
5
5
  SHA512:
6
- metadata.gz: b290a3bc6cd3febbcbad37da51a414c44460fb226b18cb242d977a482735b4a48663aec300beb1637944b21a727aa827672c284d5e051069b9dc3e98b1a336b8
7
- data.tar.gz: e1b55e7819b70ff46867d89638825e58263bdf5822c54cbbc181e27f60c9ed1c58f3ea6c31b091d53b9cc80aa3ff2afa1031a90774b663ceec5b1c2bf120f49c
6
+ metadata.gz: 8326c6580d05a960713a57c609396b69ab3e82578ac9c9d9b22fb88d40e059c83551b2d6e988381565cb505c0f7ff634229296128e177adc1dbfabf79a081916
7
+ data.tar.gz: edeeabbc466db4d3edacb3fce714811f3c6f3dfc4562ff312a6e54828ea1e38fbef4b99158010610c1becb57c6490047583bcbf7f02f60d98aaf208f2e03512f
data/lib/blomming_api.rb CHANGED
@@ -41,6 +41,15 @@ module BlommingApi
41
41
  # instance variables, read/write
42
42
  attr_accessor :currency, :locale, :verbose
43
43
 
44
+ # integer:
45
+ # number of seconds between a call and the successive
46
+ # in case of a retry of an API call
47
+ attr_reader :retry_seconds
48
+
49
+ # boolean:
50
+ # if false, in case of exceptions, process die with exit
51
+ attr_reader :survive_on_fatal_error
52
+
44
53
  # new
45
54
  def initialize(config_filename)
46
55
  # read YAML configuration file
@@ -51,26 +51,39 @@ module BlommingApi
51
51
  @currency = config['default_currency']
52
52
  @locale = config['default_locale']
53
53
 
54
+
55
+ # assign default value: 2,
56
+ # if parameter is not set in config file
57
+ @retry_seconds = config['retry_seconds']
58
+ @retry_seconds = 2 if @retry_seconds.nil?
59
+
60
+ # assign default value: false,
61
+ # if parameter is not set in config file
62
+ @survive_on_fatal_error = config['survive_on_fatal_error']
63
+ @survive_on_fatal_error = false if @survive_on_fatal_error.nil?
64
+
54
65
  # other behaviours
55
66
  @verbose = config['verbose']
56
67
  @verbose_access_token = @verbose
68
+
57
69
  puts to_s(config_filename) if @verbose
58
70
  end
59
71
  private :read_config_file
60
72
 
61
- def show_config_file (config_file)
62
- "config file: #{config_file}\n\n" +
63
- "\tdescription: #@description\n" +
64
- "\tservices: #@services\n" +
65
- "\tusername: #@username\n" +
66
- "\tpassword: #@password\n" +
67
- "\tclient_id: #@client_id\n" +
68
- "\tclient_secret: #@client_secret\n" +
69
- "\tdomain: #@domain\n" +
70
- "\tdefault_currency: #@currency\n" +
71
- "\tdefault_locale: #@locale\n\n"
73
+ def show_config_file_attributes
74
+ "\n description: #@description\n" +
75
+ " services: #@services\n" +
76
+ " username: #@username\n" +
77
+ " password: #@password\n" +
78
+ " client_id: #@client_id\n" +
79
+ " client_secret: #@client_secret\n" +
80
+ " domain: #@domain\n" +
81
+ " default_currency: #@currency\n" +
82
+ " default_locale: #@locale\n" +
83
+ "survive_on_fatal_error: #@survive_on_fatal_error\n" +
84
+ " retry_seconds: #@retry_seconds\n\n"
72
85
  end
73
- public :show_config_file
86
+ public :show_config_file_attributes
74
87
 
75
88
  end
76
89
  end
@@ -7,7 +7,7 @@ module BlommingApi
7
7
  # build complete URL of any API request endpoint URI
8
8
  #
9
9
  def api_url (api_endpoint_uri)
10
- "#{@domain}#{@api_version}#{api_endpoint_uri}"
10
+ "#{@domain}#{@api_version}#{api_endpoint_uri}"
11
11
  end
12
12
 
13
13
  #
@@ -15,17 +15,30 @@ module BlommingApi
15
15
  # embedding authentication token and all optional parameters
16
16
  #
17
17
  def request_params(params={})
18
- { authorization: "Bearer #{@access_token}", params: params }
18
+ { authorization: "Bearer #{@access_token}", params: params }
19
19
  end
20
20
 
21
21
  #
22
- # load_or_retry: manage RestClient exceptions. iterator.
22
+ # load_or_retry:
23
+ #
24
+ # 1. call RestClient verb,
25
+ # passed in a block (load_or_retry is an iterator)
23
26
  #
24
- # === attributes
27
+ # 2. without any exception (http errors):
28
+ # return data (convert response data from JSON to Ruby Hash)
25
29
  #
26
- # retry_seconds
27
- # number of seconds between a call and the successivem in case of a retry
30
+ # 2. in case of exceptions:
31
+ # manage connection/RestClient exceptions
32
+ # (by example retry the API call in case of authentication token expired).
33
+ #
34
+ # In case of fatal error:
35
+ # if @survive_on_fatal_error if false (default)
36
+ # process die with exit
37
+ # if @survive_on_fatal_error if true (must be specified in config file)
38
+ # load_or_retry return value: nil (no data returned!).
28
39
  #
40
+ # === arguments
41
+ #
29
42
  # &restclient_call_block
30
43
  # the block to be passed (containing call to RestCient method)
31
44
  #
@@ -33,62 +46,100 @@ module BlommingApi
33
46
  #
34
47
  # load_or_retry { RestClient.get url, req }
35
48
  #
36
- # load_or_retry(5) do
37
- # RestClient.get url, req
38
- # end
39
- #
40
- def load_or_retry (retry_seconds=2, &restclient_call_block)
41
- begin
49
+ def load_or_retry (&restclient_call_block)
50
+ begin
51
+ # call RestClient verb
42
52
  json_data = restclient_call_block.call
43
53
 
44
- # IP connection error, wrong url, etc.
45
- rescue SocketError => e
46
- STDERR.puts "#{Time.now}: socket error: #{e}. Possible net connection problems. Retry in #{retry_seconds} seconds."
47
- sleep retry_seconds
48
- retry
49
-
50
- # restclient exceptions
51
- rescue => e
54
+ # IP connection error, wrong url
55
+ rescue SocketError => e
56
+ socket_error! e
57
+ retry
52
58
 
53
- #
54
- # HTTP status code manager
55
- #
56
- if 401 == e.response.code
57
- # Client authentication failed due to unknown client, no client authentication included,
58
- # or unsupported authentication method
59
- # After your bearer token has expired, each request done with that stale token will return an HTTP code 401
60
- STDERR.puts "#{Time.now}: restclient error. http status code: #{e.response.code}: #{e.response.body}. Invalid or expired token. Retry in #{retry_seconds} seconds."
59
+ # RestClient exceptions, manage HTTP status code
60
+ rescue => e
61
61
 
62
- # opinabile mettere la sleep qui, ma meglio per evitare loop stretto
63
- sleep retry_seconds
64
-
65
- # richiede il token daccapo. da rivedere.
66
- authenticate :refresh
62
+ if http_status_code_401? e
63
+ authenticate_refresh e
67
64
  retry
68
65
 
69
- elsif 404 == e.response.code
70
- STDERR.puts "#{Time.now}: restclient error. http status code: #{e.response.code}: #{e.response.body}. Exit."
71
- exit
66
+ elsif http_status_code_404? e
67
+ return fatal_error! e
72
68
 
73
- # Invalid or blank request body given (selll services endpoints)
74
- elsif 422 == e.response.code
75
- STDERR.puts "#{Time.now}: restclient error. http status code: #{e.response.code}: #{e.response.body}. Exit."
76
- exit
69
+ elsif http_status_code_422? e
70
+ return fatal_error! e
77
71
 
78
- elsif [500, 520].include? e.response.code
79
- STDERR.puts "#{Time.now}: restclient error. http status code: #{e.response.code}: #{e.response.body}. Retry in #{retry_seconds} seconds."
80
- sleep retry_seconds
81
- retry
72
+ elsif http_status_code_5xx? e
73
+ return fatal_error! e
82
74
 
75
+ # any other exception
83
76
  else
84
- STDERR.puts "#{Time.now}: restclient error. http status code: #{e.response.code}: #{e.response.body}. Exit."
85
- exit
77
+ return fatal_error! e
86
78
  end
87
79
  end
88
80
 
89
- # HTTP status 200: return data (json to hash)
81
+ # HTTP status 200: return data (hash from JSON)!
90
82
  MultiJson.load json_data
91
83
  end
92
84
 
85
+
86
+ # Client authentication failed due to unknown client,
87
+ # no client authentication included,
88
+ # or unsupported authentication method
89
+ # After your bearer token has expired,
90
+ # each request done with that stale token will return an HTTP code 401
91
+ def http_status_code_401?(e)
92
+ 401 == e.response.code
93
+ end
94
+
95
+ def http_status_code_404?(e)
96
+ 404 == e.response.code
97
+ end
98
+
99
+ # Invalid or blank request body given (sell services endpoints)
100
+ def http_status_code_422?(e)
101
+ 422 == e.response.code
102
+ end
103
+
104
+ def http_status_code_5xx?(e)
105
+ [500, 520].include? e.response.code
106
+ end
107
+
108
+ #
109
+ # Errors managers
110
+ #
111
+ def authenticate_refresh(e)
112
+ STDERR.puts "#{Time.now}: restclient error. http status code: #{e.response.code}: #{e.response.body}. Invalid or expired token. Retry in #{retry_seconds} seconds."
113
+
114
+ # sleep maybe useless here
115
+ sleep @retry_seconds
116
+
117
+ # authenticate with refresh token
118
+ authenticate :refresh
119
+ end
120
+
121
+
122
+ def socket_error!(e)
123
+ STDERR.puts "#{Time.now}: socket error: #{e}. Possible net connection problems. Retry in #{retry_seconds} seconds."
124
+
125
+ sleep @retry_seconds
126
+ end
127
+
128
+
129
+ def fatal_error!(e)
130
+ STDERR.puts "#{Time.now}: restclient error. http status code: #{e.response.code}: #{e.response.body}. Exit."
131
+
132
+ #
133
+ # survive_on_fatal_error initialized in config file
134
+ #
135
+ unless @survive_on_fatal_error
136
+ # Process die!
137
+ exit
138
+ else
139
+ # no data!
140
+ return nil
141
+ end
142
+ end
143
+
93
144
  end
94
145
  end
@@ -9,7 +9,7 @@ module BlommingApi
9
9
  # all_pages
10
10
  # It's a Ruby block iterator that retrieve all items of all pages of any API endpoint.
11
11
  #
12
- # === attributes
12
+ # === arguments
13
13
  #
14
14
  # +verbose+: :quite (silent mode)
15
15
  # :stdout (verbose mode: stdout puts)
@@ -1,3 +1,3 @@
1
1
  module BlommingApi
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blomming_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giorgio Robino
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-05 00:00:00.000000000 Z
11
+ date: 2014-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: method_source
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  version: '0'
141
141
  requirements: []
142
142
  rubyforge_project:
143
- rubygems_version: 2.1.11
143
+ rubygems_version: 2.2.0
144
144
  signing_key:
145
145
  specification_version: 4
146
146
  summary: www.blomming.com social commerce API's wrapper