stream-ruby 2.4.5 → 2.5.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 +4 -4
- data/lib/stream/client.rb +78 -33
- data/lib/stream/version.rb +1 -1
- metadata +5 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1398099d6f288e0c5bad1cd13bc9b63def1464b2
|
4
|
+
data.tar.gz: c9f19bcff3c6c6012c04b692e9e90194aaf2f905
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 576e0e4b71ef340081a9f87d1165434ee88c773ca7ae42a156d57fd5ba1e8eb10f3ffedeb8d3411eeaa9de8190da976b0e5672ab7d4567a6d71c30e55f04afcf
|
7
|
+
data.tar.gz: fea36c574fc895385b1549b29099478f6609e51309b8cfbfb9ff2131fd4de26342e3d7096a847f1cb39e077d418113c0a1d925c5901378bcf0b00714342f47c6
|
data/lib/stream/client.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "
|
1
|
+
require "faraday"
|
2
2
|
require "stream/errors"
|
3
3
|
require "stream/feed"
|
4
4
|
require "stream/signer"
|
@@ -10,9 +10,7 @@ module Stream
|
|
10
10
|
attr_reader :api_key
|
11
11
|
attr_reader :api_secret
|
12
12
|
attr_reader :app_id
|
13
|
-
attr_reader :
|
14
|
-
attr_reader :location
|
15
|
-
attr_reader :default_timeout
|
13
|
+
attr_reader :client_options
|
16
14
|
|
17
15
|
if RUBY_VERSION.to_f >= 2.1
|
18
16
|
require "stream/batch"
|
@@ -49,13 +47,16 @@ module Stream
|
|
49
47
|
@api_key = api_key
|
50
48
|
@api_secret = api_secret
|
51
49
|
@app_id = app_id
|
52
|
-
@location = opts[:location]
|
53
|
-
@api_version = opts.fetch(:api_version, "v1.0")
|
54
|
-
@default_timeout = opts.fetch(:default_timeout, 3)
|
55
50
|
@signer = Stream::Signer.new(api_secret)
|
51
|
+
|
52
|
+
@client_options = {
|
53
|
+
:api_version => opts.fetch(:api_version, "v1.0"),
|
54
|
+
:location => opts.fetch(:location, nil),
|
55
|
+
:default_timeout => opts.fetch(:default_timeout, 3),
|
56
|
+
:api_key => @api_key
|
57
|
+
}
|
56
58
|
end
|
57
59
|
|
58
|
-
#
|
59
60
|
# Creates a feed instance
|
60
61
|
#
|
61
62
|
# @param [string] feed_slug the feed slug (eg. flat, aggregated...)
|
@@ -82,7 +83,7 @@ module Stream
|
|
82
83
|
end
|
83
84
|
|
84
85
|
def get_http_client
|
85
|
-
@http_client ||= StreamHTTPClient.new(@
|
86
|
+
@http_client ||= StreamHTTPClient.new(@client_options)
|
86
87
|
end
|
87
88
|
|
88
89
|
def make_query_params(params)
|
@@ -98,48 +99,92 @@ module Stream
|
|
98
99
|
end
|
99
100
|
|
100
101
|
class StreamHTTPClient
|
101
|
-
|
102
|
+
require "faraday"
|
102
103
|
|
104
|
+
attr_reader :conn
|
105
|
+
attr_reader :options
|
103
106
|
attr_reader :base_path
|
104
107
|
|
105
|
-
def initialize(
|
108
|
+
def initialize(client_params)
|
109
|
+
@options = client_params
|
106
110
|
location_name = "api"
|
107
|
-
unless location.nil?
|
108
|
-
location_name = "#{location}-api"
|
111
|
+
unless client_params[:location].nil?
|
112
|
+
location_name = "#{client_params[:location]}-api"
|
109
113
|
end
|
110
|
-
@base_path = "/api/#{api_version}"
|
111
114
|
|
112
115
|
protocol = "https"
|
113
|
-
if location == "qa"
|
116
|
+
if @options[:location] == "qa"
|
114
117
|
protocol = "http"
|
115
118
|
end
|
116
119
|
|
117
|
-
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
def _build_error_message(response)
|
122
|
-
msg = "#{response['exception']} details: #{response['detail']}"
|
120
|
+
@base_path = "/api/#{@options[:api_version]}"
|
121
|
+
base_url = "#{protocol}://#{location_name}.getstream.io#{@base_path}"
|
123
122
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
123
|
+
@conn = Faraday.new(:url => base_url) do |faraday|
|
124
|
+
# faraday.request :url_encoded
|
125
|
+
faraday.adapter Faraday.default_adapter
|
126
|
+
faraday.use RaiseHttpException
|
127
|
+
faraday.options[:open_timeout] = @options[:default_timeout]
|
128
|
+
faraday.options[:timeout] = @options[:default_timeout]
|
128
129
|
end
|
129
|
-
|
130
|
-
msg
|
130
|
+
@conn.path_prefix = @base_path
|
131
131
|
end
|
132
132
|
|
133
133
|
def make_http_request(method, relative_url, params = nil, data = nil, headers = nil)
|
134
134
|
headers["Content-Type"] = "application/json"
|
135
135
|
headers["X-Stream-Client"] = "stream-ruby-client-#{Stream::VERSION}"
|
136
|
-
|
137
|
-
|
138
|
-
|
136
|
+
|
137
|
+
params["api_key"] = @options[:api_key]
|
138
|
+
relative_url = "#{@base_path}#{relative_url}?#{URI.encode_www_form(params)}"
|
139
|
+
response = @conn.run_request(method, relative_url, data.to_json, headers)
|
140
|
+
|
141
|
+
case response[:status].to_i
|
139
142
|
when 200..203
|
140
|
-
return response
|
141
|
-
|
142
|
-
|
143
|
+
return ::JSON.parse(response[:body])
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
class RaiseHttpException < Faraday::Middleware
|
149
|
+
def call(env)
|
150
|
+
@app.call(env).on_complete do |response|
|
151
|
+
case response[:status].to_i
|
152
|
+
when 200..203
|
153
|
+
return response
|
154
|
+
when 401
|
155
|
+
raise StreamApiResponseException, error_message(response, "Bad feed")
|
156
|
+
when 403
|
157
|
+
raise StreamApiResponseException, error_message(response, "Bad auth/headers")
|
158
|
+
when 404
|
159
|
+
raise StreamApiResponseException, error_message(response, "url not found")
|
160
|
+
when 204...600
|
161
|
+
raise StreamApiResponseException, error_message(response, "something else")
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def initialize(app)
|
167
|
+
super app
|
168
|
+
@parser = nil
|
169
|
+
end
|
170
|
+
|
171
|
+
private
|
172
|
+
|
173
|
+
def error_message(response, body = nil)
|
174
|
+
"#{response[:method].to_s.upcase} #{response[:url]}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
|
175
|
+
end
|
176
|
+
|
177
|
+
def error_body(body)
|
178
|
+
if !body.nil? && !body.empty? && body.is_a?(String)
|
179
|
+
body = ::JSON.parse(body)
|
180
|
+
end
|
181
|
+
|
182
|
+
if body.nil?
|
183
|
+
nil
|
184
|
+
elsif body["meta"] && body["meta"]["error_message"] && !body["meta"]["error_message"].empty?
|
185
|
+
": #{body['meta']['error_message']}"
|
186
|
+
elsif body["error_message"] && !body["error_message"].empty?
|
187
|
+
": #{body['error_type']}: #{body['error_message']}"
|
143
188
|
end
|
144
189
|
end
|
145
190
|
end
|
data/lib/stream/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stream-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tommaso Barbugli
|
@@ -9,22 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-12-
|
12
|
+
date: 2016-12-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: faraday
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 0.10.0
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 0.10.0
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: http_signatures
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,20 +53,6 @@ dependencies:
|
|
53
53
|
- - '='
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: 1.5.2
|
56
|
-
- !ruby/object:Gem::Dependency
|
57
|
-
name: persistent_httparty
|
58
|
-
requirement: !ruby/object:Gem::Requirement
|
59
|
-
requirements:
|
60
|
-
- - "~>"
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
version: 0.1.2
|
63
|
-
type: :runtime
|
64
|
-
prerelease: false
|
65
|
-
version_requirements: !ruby/object:Gem::Requirement
|
66
|
-
requirements:
|
67
|
-
- - "~>"
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: 0.1.2
|
70
56
|
- !ruby/object:Gem::Dependency
|
71
57
|
name: rake
|
72
58
|
requirement: !ruby/object:Gem::Requirement
|