business_insight_api_client 0.2.0 → 0.2.1

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: 7a36aebe2079574cf93797fafe1f574030bae237
4
- data.tar.gz: b4aef315a2dc3fec580eca5774e71a356f5bc485
3
+ metadata.gz: ef59f6b6787285076c903e5cee6acd0df94db21e
4
+ data.tar.gz: ac7856f65e8f8384cfaed30f7ac4f82a375caa14
5
5
  SHA512:
6
- metadata.gz: 54d0bcf354e9e9419eeb4b5c74b61d42a68b1cee49dfd80971267cbe15284bfd33ce8a3636d00836155d42decb52eaba73f6d07b8450b8610b7b43f37eff3ae2
7
- data.tar.gz: d22ca8f0beaa98423a5bde2f0c093f3376dade5c30860cdccac514e922942f88b35af62c97a56c25cebc0c30e2ede96c352aac26f6ed955a7076936bc0d078b4
6
+ metadata.gz: e326364678b9f2ad71258e7479c449c7f3cf50cb62fed8e451e08674f2d5e73bcc69ef9caab324a55940fb31eda9ab19c168b6e5f489291bc772ea9d0b7d6a2d
7
+ data.tar.gz: 1faacecb237f6d43c354977af171fade6a3aaf2e0442ce821ee4d472df31d2b8813eb085374584c4827681a516442295f62956c7b282695e25d136036d103e69
@@ -60,7 +60,7 @@ module BusinessInsightApiClient
60
60
  # @param [Hash] animal animal to be updated. See docs for parameters.
61
61
  # @return [BusinessInsightApiClient::Mash] response mash.
62
62
  # @see https://api.nedap-bi.com/api/docs/
63
- def update_animal(animal_id,animal={})
63
+ def update_animal(animal_id, animal={})
64
64
  ::BusinessInsightApiClient::Mash.from_json client.put("/animals/#{animal_id}", animal.to_json)
65
65
  end
66
66
 
@@ -21,16 +21,16 @@ module BusinessInsightApiClient
21
21
  tokens = ::BusinessInsightApiClient::Mash.from_json client.get('/applications/access_tokens', header: client_credential_token.headers)
22
22
  expired_time = Time.now
23
23
  Hash[tokens.access_tokens.collect do |token|
24
- [
25
- token.resource_owner_id,
26
- authorization.access_token_from_hash(
27
- token: token.token,
28
- expires_in: token.expires_in_seconds,
29
- expires_at: Time.now.to_i + token.expires_in_seconds,
30
- refresh_token: token.refresh_token
31
- )
32
- ]
33
- end]
24
+ [
25
+ token.resource_owner_id,
26
+ authorization.access_token_from_hash(
27
+ token: token.token,
28
+ expires_in: token.expires_in_seconds,
29
+ expires_at: Time.now.to_i + token.expires_in_seconds,
30
+ refresh_token: token.refresh_token
31
+ )
32
+ ]
33
+ end]
34
34
 
35
35
  end
36
36
 
@@ -9,6 +9,7 @@ module BusinessInsightApiClient
9
9
  def groups
10
10
  ::BusinessInsightApiClient::Mash.from_json client.get('/groups')
11
11
  end
12
+
12
13
  # get: '/groups/:id'
13
14
  # @param [Integer] id group id
14
15
  # @see https://api.nedap-bi.com/api/docs/
@@ -39,11 +39,11 @@ module BusinessInsightApiClient
39
39
  # @option options [String] :client_secret (BusinessInsightApiClient.client_secret) the client secret uses in OAuth authorization.
40
40
  def initialize(options = {})
41
41
  @options = options
42
- @options[:api_url] ||= BusinessInsightApiClient.api_url
43
- @options[:authorization_url] ||= BusinessInsightApiClient.authorization_url
44
- @options[:content_type] ||= BusinessInsightApiClient.default_content_type
45
- @options[:client_id] ||= BusinessInsightApiClient.client_id
46
- @options[:client_secret] ||= BusinessInsightApiClient.client_secret
42
+ @options[:api_url] ||= BusinessInsightApiClient.api_url
43
+ @options[:authorization_url] ||= BusinessInsightApiClient.authorization_url
44
+ @options[:content_type] ||= BusinessInsightApiClient.default_content_type
45
+ @options[:client_id] ||= BusinessInsightApiClient.client_id
46
+ @options[:client_secret] ||= BusinessInsightApiClient.client_secret
47
47
  end
48
48
 
49
49
  # Creates new or returns an existing Authorization helper.
@@ -6,9 +6,9 @@ module BusinessInsightApiClient
6
6
  class Authorization
7
7
 
8
8
  DEFAULT_OPTIONS = {
9
- authorization_url: 'https://nedap-bi.com',
10
- client_id: '',
11
- client_secret: ''
9
+ authorization_url: 'https://nedap-bi.com',
10
+ client_id: '',
11
+ client_secret: ''
12
12
  }
13
13
 
14
14
 
@@ -65,8 +65,8 @@ module BusinessInsightApiClient
65
65
  # @param [Hash] params additional params
66
66
  # @option params [String] :redirect_uri the redirect uri to which the client has to be redirected to.
67
67
  # @param [Hash] options options
68
- def auth_code_token_from_access_grant(access_grant, params={},options={})
69
- oauth_client.auth_code.get_token(access_grant,params,options)
68
+ def auth_code_token_from_access_grant(access_grant, params={}, options={})
69
+ oauth_client.auth_code.get_token(access_grant, params, options)
70
70
  end
71
71
 
72
72
 
@@ -10,8 +10,8 @@ module BusinessInsightApiClient
10
10
  include HTTPClient::Util
11
11
 
12
12
  DEFAULT_OPTIONS = {
13
- api_url: 'https://api.nedap-bi.com',
14
- default_content_type: 'application/json',
13
+ api_url: 'https://api.nedap-bi.com',
14
+ default_content_type: 'application/json',
15
15
  }
16
16
 
17
17
  API_VERSION_PATH = '/v1'
@@ -40,39 +40,60 @@ module BusinessInsightApiClient
40
40
  def get(path, *args, &block)
41
41
  build_path(path)
42
42
  begin
43
- arguments = argument_to_hash(args,:query, :body, :header, :follow_redirect) || {}
44
- arguments[:header] = access_token_header.merge(arguments[:header] || {})
45
- response = client.request(:get, @base_uri.to_s, arguments, &block)
43
+ execute do
44
+ arguments = argument_to_hash(args, :query, :body, :header, :follow_redirect) || {}
45
+ arguments[:header] = access_token_header.merge(arguments[:header] || {})
46
+ client.request(:get, @base_uri.to_s, arguments, &block)
47
+ end
46
48
  rescue HTTPClient::TimeoutError
47
49
  raise ::BusinessInsightApiClient::Errors::RequestTimedOutError
48
50
  end
49
- raise_errors(response)
50
- response.body
51
+
51
52
  end
52
53
 
53
54
  def put(path, body = '')
54
55
  build_path(path)
55
- response = client.put(@base_uri.to_s, body: body, header: { 'Content-Type' => content_type, 'Accept' => content_type}.merge(access_token_header))
56
- raise_errors(response)
57
- response.body
56
+ execute do
57
+ client.put(@base_uri.to_s, body: body, header: { 'Content-Type' => content_type, 'Accept' => content_type }.merge(access_token_header))
58
+ end
58
59
  end
59
60
 
60
61
  def post(path, body = '')
61
62
  build_path(path)
62
- response = client.post(@base_uri.to_s, body: body, header: { 'Content-Type' => content_type, 'Accept' => content_type}.merge(access_token_header))
63
- raise_errors(response)
64
- response.body
63
+ execute do
64
+ client.post(@base_uri.to_s, body: body, header: { 'Content-Type' => content_type, 'Accept' => content_type }.merge(access_token_header))
65
+ end
65
66
  end
66
67
 
67
68
  def delete(path)
68
69
  build_path(path)
69
- response = client.delete(@base_uri.to_s, header: access_token_header)
70
- raise_errors(response)
71
- response.body
70
+ execute do
71
+ client.delete(@base_uri.to_s, header: access_token_header)
72
+ end
72
73
  end
73
74
 
74
75
  private
75
76
 
77
+ # Executes an request and retries when an token could be expired.
78
+ # It refreshes the token before retrying.
79
+ #
80
+ # @return [String] the response.
81
+ # @raise [::BusinessInsightApiClient::Errors] on request errors.
82
+ def execute
83
+ retries ||= 1
84
+ response = yield
85
+ raise_on_response_errors(response)
86
+ rescue ::BusinessInsightApiClient::Errors::UnauthorizedError => e
87
+ if (retries -= 1) >= 0
88
+ authorization.current_token = authorization.current_token.refresh!
89
+ retry
90
+ else
91
+ raise
92
+ end
93
+ else
94
+ response.body
95
+ end
96
+
76
97
  def access_token_header
77
98
  authorization.current_token_headers
78
99
  end
@@ -85,14 +106,14 @@ module BusinessInsightApiClient
85
106
  @client ||= HTTPClient.new
86
107
  end
87
108
 
88
- def raise_errors(response)
109
+ def raise_on_response_errors(response)
89
110
  case response.status
90
111
  when 400
91
112
  response_body = BusinessInsightApiClient::Mash.from_json(response.body)
92
113
  raise ::BusinessInsightApiClient::Errors::BadRequestError.new(response.status, response_body), "(#{response.status}): #{[*response_body.messages].join(', ')}"
93
114
  when 401
94
115
  response_body = BusinessInsightApiClient::Mash.from_json(response.body)
95
- raise ::BusinessInsightApiClient::Errors::UnauthorizedError.new(response.status,response_body),"(#{response.status}): #{[*response_body.messages].join(', ')}"
116
+ raise ::BusinessInsightApiClient::Errors::UnauthorizedError.new(response.status, response_body), "(#{response.status}): #{[*response_body.messages].join(', ')}"
96
117
  when 403
97
118
  response_body = BusinessInsightApiClient::Mash.from_json(response.body)
98
119
  raise ::BusinessInsightApiClient::Errors::ForbiddenError.new(response.status, response_body), "(#{response.status}): #{[*response_body.messages].join(', ')}"
@@ -102,7 +123,7 @@ module BusinessInsightApiClient
102
123
  when 500
103
124
  raise ::BusinessInsightApiClient::Errors::InternalError.new(response.status, response.reason), "#{response.code} : #{response.reason}"
104
125
  when 501..503
105
- raise ::BusinessInsightApiClient::Errors::UnavailableError.new(response.status, response.reason),"#{response.code} : #{response.reason}"
126
+ raise ::BusinessInsightApiClient::Errors::UnavailableError.new(response.status, response.reason), "#{response.code} : #{response.reason}"
106
127
  end
107
128
  end
108
129
 
@@ -1,3 +1,3 @@
1
1
  module BusinessInsightApiClient
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business_insight_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nedap
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-21 00:00:00.000000000 Z
11
+ date: 2015-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json