motion-http 1.0.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38301fffb680c2ff73c68cc0cafef260be772a4c09892c8f6706bb415014bdc6
4
- data.tar.gz: 562ac8347a504aa3c061beeb6555ec54b73d0ec5c6d27266349150c1e4239c45
3
+ metadata.gz: f008cdd4f451d3c07f846a6129ee78759ef8c3b2a1eb0ff0374f2cf05f2b6f8e
4
+ data.tar.gz: 83d1124e7b3a3afcbec2a8d28436586eb0392b12922b4bd30174439d32f86cda
5
5
  SHA512:
6
- metadata.gz: d34d53372cd85ff50915ca586a36fdba710852a62e9f60c511139d0d72887084e974cc7a39faedc02a5737c96d858900e66ef661960572c22970aebccc613e32
7
- data.tar.gz: 01d2e62e89ed670128c423c4f005383cc2ff46e351c3dc2163cf567a88ee7a5507257c3321cac4ba151f1bb91f2865af9ebd4ce1aae9f0c06b369d46ba63117a
6
+ metadata.gz: 40bec4425a0992d25cce5407c0edd47f675d5a255cc01368f3f7441abb2ecd2c8920895f83e5b0e6b569709b7d59025f9a2fcca35b19e47bd522ecb579d9456d
7
+ data.tar.gz: 7fe5336df48fac9bd7c58e16bae95da6cc05333a07e95ead7406d278cf3b3bb20a92bbb686e1d2e168765259a9ac1df5ef46546ccc3879bbd5eb5658638d07e3
data/README.md CHANGED
@@ -6,9 +6,9 @@ Supported platforms:
6
6
  - iOS, macOS, tvOS, watchOS
7
7
  - Android
8
8
 
9
- On Android, this gem depends on the super popular [OkHttp](http://square.github.io/okhttp/) networking library.
9
+ It makes use of the officially supported networking libraries provided by Apple and Google. The goal of this gem is to provide you with a stable alternative to using these libraries directly, using a syntax that is much easier to use.
10
10
 
11
- Please note that this library is still a work in progress. Please report bugs and suggestions for improvement!
11
+ Please report any bugs or suggestions for improvement!
12
12
 
13
13
  ## Installation
14
14
 
@@ -19,19 +19,23 @@ Add this line to your application's Gemfile:
19
19
  And then execute:
20
20
 
21
21
  $ bundle
22
- $ rake gradle:install # for Android apps
22
+ $ rake gradle:install # Android only
23
23
 
24
24
  ### iOS Specific Configuration
25
25
 
26
- If you will be making insecure HTTP requests (not HTTPS), you will need to explicitly allow insecure HTTP requests by adding this line to your app's configuration in your Rakefile:
26
+ If you will be making insecure requests (not using HTTPS), you will need to explicitly allow insecure HTTP requests by adding this line to your app's configuration (in your `Rakefile`). You might want to do this if you are trying to access localhost in development.
27
27
 
28
- app.info_plist['NSAppTransportSecurity'] = { 'NSAllowsArbitraryLoads' => true }
28
+ ```ruby
29
+ app.development do
30
+ app.info_plist['NSAppTransportSecurity'] = { 'NSAllowsArbitraryLoads' => true }
31
+ end
32
+ ```
29
33
 
30
34
  ## Usage
31
35
 
32
- Using `motion-http` is quick and easy. You can use the simple approach for making one-off requests, or the advanced approach of creating a reusable API client for further customization.
36
+ Using `motion-http` is quick and easy. You can either make one-off requests or create a reusable API client for further customization.
33
37
 
34
- ### Simple Usage
38
+ ### Basic Usage
35
39
 
36
40
  The basic syntax for a request looks like this:
37
41
  ```ruby
@@ -85,16 +89,25 @@ HTTP.get("http://api.example.com/people.json") do |response|
85
89
  end
86
90
  ```
87
91
 
88
- Use the `follow_redirects` option to specify whether or not to follow redirects. It defaults to true:
92
+ Use the `follow_redirects` option to specify whether or not to follow redirects. The default is `true`:
89
93
  ```ruby
90
94
  HTTP.get("http://example.com/redirect", follow_redirects: false) do |response|
91
95
  # ...
92
96
  end
93
97
  ```
94
98
 
95
- When making a `POST` request, specify the `:form` option and it will automatically be encoded as `application/x-www-form-urlencoded` request body:
99
+ #### POST Requests
100
+
101
+ When making a `POST` request, specifying the request body is easy:
102
+ ```ruby
103
+ HTTP.post("http://www.example.com/endpoint", body: raw_request_body) do |response|
104
+ # ...
105
+ end
106
+ ```
107
+
108
+ Specify the `:form` option and it will automatically be encoded as `application/x-www-form-urlencoded` request body:
96
109
  ```ruby
97
- HTTP.post("http://www.example.com/login", form: { user: 'andrew', pass: 'secret'}) do |response|
110
+ HTTP.post("http://www.example.com/login", form: { user: 'andrew', password: 'secret'}) do |response|
98
111
  if response.success?
99
112
  puts "Authenticated!"
100
113
  elsif response.client_error?
@@ -118,7 +131,7 @@ HTTP.post("http://www.example.com/widgets", json: { widget: { name: "Foobar" } }
118
131
  end
119
132
  ```
120
133
 
121
- Request specific headers can also be specified with the `:headers` option (overriding any previously set headers):
134
+ To specify request specific headers, use the `:headers` option. This overrides any previously set headers. In this example, we override the default JSON content type:
122
135
  ```ruby
123
136
  HTTP.post("http://www.example.com/widgets",
124
137
  headers: { 'Content-Type' => 'application/vnd.api+json' },
@@ -130,12 +143,12 @@ end
130
143
 
131
144
  All other HTTP method requests work the same way:
132
145
  ```ruby
133
- HTTP.put(url, params) { ... }
134
- HTTP.patch(url, params) { ... }
135
- HTTP.delete(url, params) { ... }
136
- HTTP.head(url, params) { ... }
137
- HTTP.options(url, params) { ... }
138
- HTTP.trace(url, params) { ... }
146
+ HTTP.put(url, options) { ... }
147
+ HTTP.patch(url, options) { ... }
148
+ HTTP.delete(url, options) { ... }
149
+ HTTP.head(url, options) { ... }
150
+ HTTP.options(url, options) { ... }
151
+ HTTP.trace(url, options) { ... }
139
152
  ```
140
153
 
141
154
  ### Advanced Usage
@@ -188,7 +201,13 @@ client.headers['Authorization'] = "Token token=#{my_token}"
188
201
 
189
202
  ### Logging
190
203
 
191
- By default, requests and responses will be logged. If you would like to disable this:
204
+ Logging is disabled by default. To enable logging:
205
+
206
+ ```
207
+ HTTP.logger.enable!
208
+ ```
209
+
210
+ To disable it again:
192
211
 
193
212
  ```
194
213
  HTTP.logger.disable!
@@ -202,12 +221,37 @@ HTTP.logger.disable!
202
221
  4. Push to the branch (`git push origin my-new-feature`)
203
222
  5. Create new Pull Request
204
223
 
205
- ## MIT License
224
+ ## License
206
225
 
207
- Copyright 2018 Andrew Havens
226
+ Copyright 2018-2019 Andrew Havens
208
227
 
209
228
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
210
229
 
211
230
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
212
231
 
213
232
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
233
+
234
+ Parts of the source are under the following license:
235
+
236
+ Copyright (c) 2015-2016, HipByte (info@hipbyte.com) and contributors.
237
+ All rights reserved.
238
+
239
+ Redistribution and use in source and binary forms, with or without
240
+ modification, are permitted provided that the following conditions are met:
241
+
242
+ 1. Redistributions of source code must retain the above copyright notice, this
243
+ list of conditions and the following disclaimer.
244
+ 2. Redistributions in binary form must reproduce the above copyright notice,
245
+ this list of conditions and the following disclaimer in the documentation
246
+ and/or other materials provided with the distribution.
247
+
248
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
249
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
250
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
251
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
252
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
253
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
254
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
255
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
257
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -1,3 +1,26 @@
1
+ # Copyright (c) 2015-2016, HipByte (info@hipbyte.com) and contributors.
2
+ # All rights reserved.
3
+
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are met:
6
+
7
+ # 1. Redistributions of source code must retain the above copyright notice, this
8
+ # list of conditions and the following disclaimer.
9
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ # this list of conditions and the following disclaimer in the documentation
11
+ # and/or other materials provided with the distribution.
12
+
13
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+
1
24
  # Copied from https://github.com/HipByte/Flow/blob/44283b31a63bc826d2c068557b6357dc1195680b/flow/base64/android/base64.rb
2
25
  class Base64
3
26
  def self.encode(string)
data/lib/android/json.rb CHANGED
@@ -1,3 +1,26 @@
1
+ # Copyright (c) 2015-2016, HipByte (info@hipbyte.com) and contributors.
2
+ # All rights reserved.
3
+
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are met:
6
+
7
+ # 1. Redistributions of source code must retain the above copyright notice, this
8
+ # list of conditions and the following disclaimer.
9
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ # this list of conditions and the following disclaimer in the documentation
11
+ # and/or other materials provided with the distribution.
12
+
13
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+
1
24
  # NOTE: Copied from https://github.com/HipByte/Flow/blob/master/flow/json/android/json.rb
2
25
  class JSON
3
26
  def self.parse(str)
data/lib/cocoa/adapter.rb CHANGED
@@ -14,11 +14,48 @@ class Motion
14
14
  def perform(&callback)
15
15
  # TODO: dataTask is good for general HTTP requests but not for file downloads
16
16
  ns_url_request = build_ns_url_request
17
+ if @request.options[:download]
18
+ perform_download_request(ns_url_request, &callback)
19
+ else
20
+ perform_normal_http_request(ns_url_request, &callback)
21
+ end
22
+ end
23
+
24
+ def perform_download_request(ns_url_request, &callback)
25
+ # TODO: need to set up delegate methods when using a background session
26
+ # @background_session = NSURLSession.sessionWithConfiguration(NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("motion-http"))
27
+ # task = @background_session.downloadTaskWithRequest(ns_url_request, completionHandler: -> (location, response, error) {
28
+ task = @session.downloadTaskWithRequest(ns_url_request, completionHandler: -> (location, response, error) {
29
+ if error
30
+ log_error "Error while requesting #{@request.url}: #{error_description(error)}"
31
+ response = Response.new(@request, response&.statusCode, Headers.new(response&.allHeaderFields), error_description(error))
32
+ else
33
+ if @request.options[:to]
34
+ error_ptr = Pointer.new(:object)
35
+ file_data = NSFileManager.defaultManager.contentsAtPath(location)
36
+ file_data.writeToFile(@request.options[:to], options: NSDataWritingAtomic, error: error_ptr)
37
+
38
+ if error_ptr[0]
39
+ log_error "Error while saving downloaded file: #{error_description(error_ptr[0])}"
40
+ response = Response.new(@request, response.statusCode, Headers.new(response.allHeaderFields), error_description(error_ptr[0]))
41
+ else
42
+ response = Response.new(@request, response.statusCode, Headers.new(response.allHeaderFields), nil)
43
+ end
44
+ else
45
+ log_error "No local save path specified."
46
+ end
47
+ Motion::HTTP.logger.log_response(response)
48
+ end
49
+ callback.call(response) if callback
50
+ })
51
+ task.resume
52
+ end
53
+
54
+ def perform_normal_http_request(ns_url_request, &callback)
17
55
  task = @session.dataTaskWithRequest(ns_url_request, completionHandler: -> (data, response, error) {
18
56
  if error
19
- error_message = "#{error.localizedDescription} #{error.userInfo['NSLocalizedDescriptionKey']}"
20
- Motion::HTTP.logger.error("Error while requesting #{@request.url}: #{error_message}")
21
- response = Response.new(@request, response&.statusCode, Headers.new(response&.allHeaderFields), error_message)
57
+ log_error "Error while requesting #{@request.url}: #{error_description(error)}"
58
+ response = Response.new(@request, response&.statusCode, Headers.new(response&.allHeaderFields), error_description(error))
22
59
  else
23
60
  response = Response.new(@request, response.statusCode, Headers.new(response.allHeaderFields), data.to_s)
24
61
  Motion::HTTP.logger.log_response(response)
@@ -40,13 +77,26 @@ class Motion
40
77
  end
41
78
 
42
79
  if @request.body
43
- ns_url_request.HTTPBody = NSString.alloc.initWithString(@request.body).dataUsingEncoding(NSUTF8StringEncoding)
80
+ if @request.body.is_a?(NSData)
81
+ body_data = @request.body
82
+ else
83
+ body_data = NSString.alloc.initWithString(@request.body).dataUsingEncoding(NSUTF8StringEncoding)
84
+ end
85
+ ns_url_request.HTTPBody = body_data
44
86
  end
45
87
 
46
88
  # TODO: add other headers
47
89
  ns_url_request
48
90
  end
49
91
 
92
+ def log_error(message, error = nil)
93
+ Motion::HTTP.logger.error(message)
94
+ end
95
+
96
+ def error_description(error)
97
+ "#{error.localizedDescription} #{error.userInfo['NSLocalizedDescriptionKey']}"
98
+ end
99
+
50
100
  # NSURLSessionTaskDelegate methods
51
101
 
52
102
  def URLSession(session, task: task, willPerformHTTPRedirection: response, newRequest: request, completionHandler: completion_handler)
data/lib/cocoa/base64.rb CHANGED
@@ -1,3 +1,26 @@
1
+ # Copyright (c) 2015-2016, HipByte (info@hipbyte.com) and contributors.
2
+ # All rights reserved.
3
+
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are met:
6
+
7
+ # 1. Redistributions of source code must retain the above copyright notice, this
8
+ # list of conditions and the following disclaimer.
9
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ # this list of conditions and the following disclaimer in the documentation
11
+ # and/or other materials provided with the distribution.
12
+
13
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+
1
24
  # Copied from https://github.com/HipByte/Flow/blob/44283b31a63bc826d2c068557b6357dc1195680b/flow/base64/cocoa/base64.rb
2
25
  class Base64
3
26
  def self.encode(string)
data/lib/cocoa/json.rb CHANGED
@@ -1,3 +1,27 @@
1
+ # Copyright (c) 2015-2016, HipByte (info@hipbyte.com) and contributors.
2
+ # All rights reserved.
3
+
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are met:
6
+
7
+ # 1. Redistributions of source code must retain the above copyright notice, this
8
+ # list of conditions and the following disclaimer.
9
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ # this list of conditions and the following disclaimer in the documentation
11
+ # and/or other materials provided with the distribution.
12
+
13
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+
24
+ # Copied from https://github.com/HipByte/Flow/blob/44283b31a63bc826d2c068557b6357dc1195680b/flow/json/cocoa/json.rb
1
25
  class JSON
2
26
  def self.parse(json_string)
3
27
  error_ptr = Pointer.new(:id)
@@ -69,6 +69,13 @@ class Motion
69
69
  request(:trace, path, options, &callback)
70
70
  end
71
71
 
72
+ def download(remote_path, options = nil, &callback)
73
+ options ||= {}
74
+ http_method = options[:method] || :get
75
+ options[:download] = true
76
+ request(http_method, remote_path, options, &callback)
77
+ end
78
+
72
79
  def request(http_method, path, options = nil, &callback)
73
80
  options ||= {}
74
81
  headers_dup = headers.dup
@@ -3,8 +3,8 @@ class Motion
3
3
  class Logger
4
4
  attr_reader :enabled
5
5
 
6
- def initialize(enabled = true)
7
- @enabled = enabled
6
+ def initialize
7
+ @enabled = false # logging is disabled by default
8
8
  end
9
9
 
10
10
  def enable!
@@ -15,63 +15,42 @@ class Motion
15
15
  @enabled = false
16
16
  end
17
17
 
18
+ def _logger
19
+ @_logger ||= Motion::Lager.new
20
+ end
21
+
22
+ def debug(message, color = :gray)
23
+ _logger.debug(message, color) if enabled
24
+ end
25
+
18
26
  def log(message, color = :white)
19
- puts colorize(color, message) if enabled
27
+ _logger.log(message, color) if enabled
20
28
  end
21
29
 
22
- def error(message)
23
- puts colorize(:red, message)
30
+ def error(message, color = :red)
31
+ _logger.error(message, color) # always log even if logging is disabled
24
32
  end
25
33
 
26
34
  def log_request(request)
27
- log "\nRequest:\n#{request.http_method.to_s.upcase} #{request.url}", :dark_gray
28
-
29
- if request.headers
30
- request.headers.each do |k,v|
31
- log "#{k}: #{v}", :dark_gray
32
- end
35
+ debug "\nRequest:\n#{request.http_method.to_s.upcase} #{request.url}"
36
+ request.headers.each do |k,v|
37
+ debug "#{k}: #{v}"
33
38
  end
34
-
35
- log(request.body, :dark_gray) if request.body
39
+ debug(request.body) if request.body
36
40
  end
37
41
 
38
42
  def log_response(response)
39
- log "\nResponse:", :dark_gray
43
+ debug "\nResponse:"
40
44
  if response.original_request
41
- log "URL: #{response.original_request.url}", :dark_gray
45
+ debug "URL: #{response.original_request.url}"
42
46
  end
43
- log "Status: #{response.status_code}", :dark_gray
47
+ debug "Status: #{response.status_code}"
44
48
  response.headers.each do |k,v|
45
- log "#{k}: #{v}", :dark_gray
49
+ debug "#{k}: #{v}"
46
50
  end
47
- log "\n#{response.body}", :dark_gray
51
+ debug("\n#{response.body}")
48
52
  end
49
53
 
50
- def colorize(color, string)
51
- return string unless simulator? # Only colorize in the simulator
52
-
53
- code = {
54
- red: 31,
55
- dark_gray: 90,
56
- }[color]
57
-
58
- if code
59
- "\e[#{code}m#{string}\e[0m"
60
- else
61
- string
62
- end
63
- end
64
-
65
- # Copied from https://github.com/rubymotion/BubbleWrap/blob/8eaf99a0966f2b375e774f5940279a704c10ad29/motion/core/ios/device.rb#L46
66
- def simulator?
67
- @simulator_state ||= begin
68
- if defined?(NSObject) # iOS
69
- !NSBundle.mainBundle.bundlePath.start_with?('/var/')
70
- else # android
71
- false
72
- end
73
- end
74
- end
75
54
  end
76
55
  end
77
56
  end
data/lib/motion-http.rb CHANGED
@@ -4,6 +4,8 @@ unless defined?(Motion::Project::Config)
4
4
  raise "This gem is only intended to be used in a RubyMotion project."
5
5
  end
6
6
 
7
+ require 'motion-lager'
8
+
7
9
  lib_dir_path = File.dirname(File.expand_path(__FILE__))
8
10
  Motion::Project::App.setup do |app|
9
11
  app.files.unshift(*Dir.glob(File.join(lib_dir_path, "common/**/*.rb")))
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Havens
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-28 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: motion-lager
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: A cross-platform HTTP client for RubyMotion that's quick and easy to
14
28
  use.
15
29
  email:
@@ -35,11 +49,11 @@ files:
35
49
  - lib/common/http/request.rb
36
50
  - lib/common/http/response.rb
37
51
  - lib/motion-http.rb
38
- homepage: https://github.com/andrewhavens/motion-http
52
+ homepage: https://github.com/rubymotion-community/motion-http
39
53
  licenses:
40
54
  - MIT
41
55
  metadata: {}
42
- post_install_message:
56
+ post_install_message:
43
57
  rdoc_options: []
44
58
  require_paths:
45
59
  - lib
@@ -54,9 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
68
  - !ruby/object:Gem::Version
55
69
  version: '0'
56
70
  requirements: []
57
- rubyforge_project:
58
- rubygems_version: 2.7.6
59
- signing_key:
71
+ rubygems_version: 3.2.15
72
+ signing_key:
60
73
  specification_version: 4
61
74
  summary: A cross-platform HTTP client for RubyMotion that's quick and easy to use.
62
75
  test_files: []