motion-http 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +36 -24
- data/lib/android/adapter.rb +11 -8
- data/lib/android/json.rb +31 -0
- data/lib/cocoa/adapter.rb +42 -31
- data/lib/cocoa/form_data_serializer.rb +29 -0
- data/lib/cocoa/json.rb +26 -0
- data/lib/common/http/client.rb +20 -8
- data/lib/common/http/logger.rb +37 -0
- data/lib/common/http/request.rb +8 -8
- data/lib/common/http/response.rb +14 -5
- data/lib/common/http.rb +18 -12
- data/lib/motion-http.rb +0 -4
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a32a0271f1f711f5a11522c48894a06ea9e8924615d38bae2a235572f15d2135
|
4
|
+
data.tar.gz: 590d6d30eb2889d97f47ed0e072313eb93dd38eb3e6485f0f894e526869f6a79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a01adbafd00b594929e7b7f86de05e55ff60e1040db5e736247dcb5dca8f01d21804c54ae5ecca7f365a6181dfb030794acfb667a93694bf0a0da0cd4a17f0d4
|
7
|
+
data.tar.gz: 684d0b718b0d84c7e4222cc16c97f39b4bf875e43b752c7d3f72d9e6540b9cc2e938a690a4542678eb5bff4d89f6409197750a4c608df0a48c9e7a327794901f
|
data/README.md
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
# motion-http
|
2
2
|
|
3
|
-
|
3
|
+
A cross-platform HTTP Client for RubyMotion that's quick and easy to use.
|
4
4
|
|
5
5
|
Supported platforms:
|
6
6
|
- iOS, macOS, tvOS, watchOS
|
7
7
|
- Android
|
8
8
|
|
9
|
-
|
10
|
-
- [AFNetworking](https://github.com/AFNetworking/AFNetworking) (for Cocoa platforms)
|
11
|
-
- [OkHttp](http://square.github.io/okhttp/) (for Android)
|
9
|
+
On Android, this gem depends on the super popular [OkHttp](http://square.github.io/okhttp/) networking library.
|
12
10
|
|
13
11
|
Please note that this library is still a work in progress. Please report bugs and suggestions for improvement!
|
14
12
|
|
@@ -21,7 +19,6 @@ Add this line to your application's Gemfile:
|
|
21
19
|
And then execute:
|
22
20
|
|
23
21
|
$ bundle
|
24
|
-
$ rake pod:install # for iOS apps
|
25
22
|
$ rake gradle:install # for Android apps
|
26
23
|
|
27
24
|
### iOS Specific Configuration
|
@@ -32,22 +29,21 @@ If you will be making insecure HTTP requests (not HTTPS), you will need to expli
|
|
32
29
|
|
33
30
|
## Usage
|
34
31
|
|
35
|
-
Using `
|
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
33
|
|
37
34
|
### Simple Usage
|
38
35
|
|
39
|
-
The basic syntax for a
|
36
|
+
The basic syntax for a request looks like this:
|
40
37
|
```ruby
|
41
|
-
|
38
|
+
HTTP.method(url, params, options) do |response|
|
42
39
|
# this block will be called asynchronously
|
43
40
|
end
|
44
41
|
```
|
42
|
+
Where `method` can be `get`, `post`, `put`, `patch`, or `delete`.
|
45
43
|
|
46
|
-
|
44
|
+
For example, to make a simple `GET` request:
|
47
45
|
```ruby
|
48
|
-
|
49
|
-
puts "status code: #{response.status_code}"
|
50
|
-
puts "response body: #{response.body}"
|
46
|
+
HTTP.get("http://www.example.com") do |response|
|
51
47
|
if response.success?
|
52
48
|
puts "Success!"
|
53
49
|
else
|
@@ -58,14 +54,23 @@ end
|
|
58
54
|
|
59
55
|
You can specify query params as the second argument:
|
60
56
|
```ruby
|
61
|
-
|
57
|
+
HTTP.get("http://www.example.com/search", term: "my search term") do |response|
|
62
58
|
# ...
|
63
59
|
end
|
64
60
|
```
|
65
61
|
|
66
|
-
|
62
|
+
The response object contains the status code, headers, and body from the response as well:
|
67
63
|
```ruby
|
68
|
-
|
64
|
+
HTTP.get("http://example.com") do |response|
|
65
|
+
puts response.status_code
|
66
|
+
puts response.headers.inspect
|
67
|
+
puts response.body
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
JSON responses will automatically be parsed when requesting the `response.object`:
|
72
|
+
```ruby
|
73
|
+
HTTP.get("http://api.example.com/people.json") do |response|
|
69
74
|
if response.success?
|
70
75
|
response.object["people"].each do |person|
|
71
76
|
puts "name: #{person["name"]}"
|
@@ -76,25 +81,32 @@ Motion::HTTP.get("http://api.example.com/people.json") do |response|
|
|
76
81
|
end
|
77
82
|
```
|
78
83
|
|
84
|
+
The third argument is a hash of options. Currently the only option supported at this time is `follow_redirects` which defaults to true:
|
85
|
+
```ruby
|
86
|
+
HTTP.get("http://example.com/redirect", nil, follow_redirects: false) do |response|
|
87
|
+
# ...
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
79
91
|
To make a simple `POST` request, the value passed as the second argument will be encoded as the request body:
|
80
92
|
```ruby
|
81
93
|
json = { widget: { name: "Foobar" } }
|
82
|
-
|
94
|
+
HTTP.post("http://www.example.com/widgets", json) do |response|
|
83
95
|
if response.success?
|
84
96
|
puts "Widget created!"
|
85
|
-
elsif response.
|
97
|
+
elsif response.status_code == 422
|
86
98
|
puts "Oops, you did something wrong: #{response.object["error_message"]}"
|
87
99
|
else
|
88
|
-
puts "Oops! Something
|
100
|
+
puts "Oops! Something went wrong."
|
89
101
|
end
|
90
102
|
end
|
91
103
|
```
|
92
104
|
|
93
105
|
`PUT`, `PATCH`, and `DELETE` requests work the same way:
|
94
106
|
```ruby
|
95
|
-
|
96
|
-
|
97
|
-
|
107
|
+
HTTP.put(url, params) { ... }
|
108
|
+
HTTP.patch(url, params) { ... }
|
109
|
+
HTTP.delete(url, params) { ... }
|
98
110
|
```
|
99
111
|
|
100
112
|
### Advanced Usage
|
@@ -102,7 +114,7 @@ Motion::HTTP.delete(url, params) { ... }
|
|
102
114
|
A common use case is to create a reusable HTTP client that uses a common base URL or request headers.
|
103
115
|
|
104
116
|
```ruby
|
105
|
-
client =
|
117
|
+
client = HTTP::Client.new("http://api.example.com")
|
106
118
|
# Set or replace a single header:
|
107
119
|
client.header "X-API-TOKEN", "abc123xyz"
|
108
120
|
|
@@ -110,13 +122,13 @@ client.header "X-API-TOKEN", "abc123xyz"
|
|
110
122
|
client.headers "X-API-TOKEN" => "abc123xyz",
|
111
123
|
"Accept" => "application/json"
|
112
124
|
|
113
|
-
#
|
125
|
+
# Note that it is valid for some headers to appear multiple times (Accept, Vary, etc).
|
114
126
|
# To append multiple headers of the same key:
|
115
127
|
client.add_header "Accept", "application/json"
|
116
128
|
client.add_header "Accept", "application/vnd.api+json"
|
117
129
|
```
|
118
130
|
|
119
|
-
Then make your requests relative to the base URL that you specified when creating your client.
|
131
|
+
Then you can make your requests relative to the base URL that you specified when creating your client.
|
120
132
|
```ruby
|
121
133
|
client.get("/people") do |response|
|
122
134
|
# ...
|
data/lib/android/adapter.rb
CHANGED
@@ -7,8 +7,12 @@ class Motion
|
|
7
7
|
@client ||= Okhttp3::OkHttpClient.new
|
8
8
|
end
|
9
9
|
|
10
|
-
def self.request
|
11
|
-
|
10
|
+
def self.perform(request, &callback)
|
11
|
+
http_method = request.http_method
|
12
|
+
url = request.url
|
13
|
+
headers = request.headers
|
14
|
+
params = request.params
|
15
|
+
|
12
16
|
request = OkHttp3::Request::Builder.new
|
13
17
|
request.url(url) # TODO: encode GET params and append to URL prior to calling this method
|
14
18
|
headers.each do |key, value|
|
@@ -23,17 +27,18 @@ class Motion
|
|
23
27
|
# body = OkHttp3::RequestBody.create(JSONMediaType, params) # TODO: allow other content types
|
24
28
|
# request.method(http_method.to_s, body)
|
25
29
|
end
|
26
|
-
client.newCall(request.build).enqueue(OkhttpCallback.new(callback))
|
30
|
+
client.newCall(request.build).enqueue(OkhttpCallback.new(request, callback))
|
27
31
|
end
|
28
32
|
|
29
33
|
class OkhttpCallback
|
30
|
-
def initialize(callback)
|
34
|
+
def initialize(request, callback)
|
35
|
+
@request = request
|
31
36
|
@callback = callback
|
32
37
|
end
|
33
38
|
|
34
39
|
def onFailure(call, e)
|
35
40
|
puts "Error: #{e.getMessage}"
|
36
|
-
@callback.call(Response.new(nil, Headers.new, e.getMessage))
|
41
|
+
@callback.call(Response.new(@request, nil, Headers.new, e.getMessage))
|
37
42
|
end
|
38
43
|
|
39
44
|
def onResponse(call, response)
|
@@ -49,9 +54,7 @@ class Motion
|
|
49
54
|
headers.add(key, value)
|
50
55
|
i += 1
|
51
56
|
end
|
52
|
-
|
53
|
-
json = JSON.load(body_string) if headers['content-type'] =~ /application\/json/
|
54
|
-
Response.new(response.code, headers, body_string, json)
|
57
|
+
Response.new(@request, response.code, headers, response.body.string)
|
55
58
|
end
|
56
59
|
end
|
57
60
|
end
|
data/lib/android/json.rb
CHANGED
@@ -34,3 +34,34 @@ class JSON
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
37
|
+
|
38
|
+
class Object
|
39
|
+
def to_json
|
40
|
+
# The Android JSON API expects real Java String objects.
|
41
|
+
@@fix_string ||= (lambda do |obj|
|
42
|
+
case obj
|
43
|
+
when String, Symbol
|
44
|
+
obj = obj.toString
|
45
|
+
when Hash
|
46
|
+
map = Hash.new
|
47
|
+
obj.each do |key, value|
|
48
|
+
key = key.toString if key.is_a?(String) || key.is_a?(Symbol)
|
49
|
+
value = @@fix_string.call(value)
|
50
|
+
map[key] = value
|
51
|
+
end
|
52
|
+
obj = map
|
53
|
+
when Array
|
54
|
+
obj = obj.map do |item|
|
55
|
+
(item.is_a?(String) || item.is_a?(Symbol)) ? item.toString : @@fix_string.call(item)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
obj
|
59
|
+
end)
|
60
|
+
|
61
|
+
obj = Org::JSON::JSONObject.wrap(@@fix_string.call(self))
|
62
|
+
if obj == nil
|
63
|
+
raise "Can't serialize object to JSON"
|
64
|
+
end
|
65
|
+
obj.toString.to_s
|
66
|
+
end
|
67
|
+
end
|
data/lib/cocoa/adapter.rb
CHANGED
@@ -1,44 +1,55 @@
|
|
1
1
|
class Motion
|
2
2
|
class HTTP
|
3
3
|
class Adapter
|
4
|
-
def self.
|
5
|
-
|
4
|
+
def self.perform(request, &callback)
|
5
|
+
Motion::HTTP::Adapter::Request.new(request).perform(&callback)
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
callback.call(Response.new(response.statusCode, Headers.new(response.allHeaderFields), nil, response_object))
|
8
|
+
class Request
|
9
|
+
def initialize(request)
|
10
|
+
@request = request
|
11
|
+
@session = NSURLSession.sessionWithConfiguration(NSURLSessionConfiguration.defaultSessionConfiguration, delegate: self, delegateQueue: nil)
|
13
12
|
end
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
13
|
+
|
14
|
+
def perform(&callback)
|
15
|
+
# TODO: dataTask is good for general HTTP requests but not for file downloads
|
16
|
+
ns_url_request = build_ns_url_request
|
17
|
+
task = @session.dataTaskWithRequest(ns_url_request, completionHandler: -> (data, response, error) {
|
18
|
+
if error
|
19
|
+
NSLog("Error: %@", error) # TODO: use configurable logging
|
20
|
+
error_message = error.localizedDescription
|
21
|
+
error_message += error.userInfo[NSLocalizedDescriptionKey] if error.userInfo[NSLocalizedDescriptionKey]
|
22
|
+
response = Response.new(@request, response.statusCode, Headers.new(response.allHeaderFields), error_message)
|
23
|
+
else
|
24
|
+
response = Response.new(@request, response.statusCode, Headers.new(response.allHeaderFields), data.to_s)
|
25
|
+
end
|
26
|
+
Motion::HTTP.logger.log_response(response)
|
27
|
+
callback.call(response)
|
28
|
+
})
|
29
|
+
task.resume
|
24
30
|
end
|
25
31
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
manager.DELETE url, parameters: params, progress: progress, success: on_success, failure: on_error
|
32
|
+
def build_ns_url_request
|
33
|
+
ns_url_request = NSMutableURLRequest.alloc.initWithURL(NSURL.URLWithString(@request.url))
|
34
|
+
ns_url_request.HTTPMethod = @request.http_method.to_s.upcase
|
35
|
+
if @request.params
|
36
|
+
# TODO: json serialization
|
37
|
+
ns_url_request.setValue('application/x-www-form-urlencoded', forHTTPHeaderField: 'Content-Type')
|
38
|
+
ns_url_request.HTTPBody = FormDataSerializer.serialize(@request.params).dataUsingEncoding(NSUTF8StringEncoding)
|
39
|
+
end
|
40
|
+
# TODO: add other headers
|
41
|
+
ns_url_request
|
37
42
|
end
|
38
43
|
|
39
|
-
#
|
40
|
-
|
41
|
-
|
44
|
+
# NSURLSessionTaskDelegate methods
|
45
|
+
|
46
|
+
def URLSession(session, task: task, willPerformHTTPRedirection: response, newRequest: request, completionHandler: completion_handler)
|
47
|
+
if @request.options[:follow_redirects] == false
|
48
|
+
completion_handler.call(nil)
|
49
|
+
else
|
50
|
+
completion_handler.call(request)
|
51
|
+
end
|
52
|
+
end
|
42
53
|
end
|
43
54
|
end
|
44
55
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class FormDataSerializer
|
2
|
+
def self.serialize(params)
|
3
|
+
flattened_params = {}
|
4
|
+
params.each do |k, v|
|
5
|
+
add_param(flattened_params, k, v)
|
6
|
+
end
|
7
|
+
serialized_params = []
|
8
|
+
flattened_params.each do |k, v|
|
9
|
+
serialized_params << "#{k}=#{serialize_value(v)}"
|
10
|
+
end
|
11
|
+
serialized_params.join('&')
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.add_param(hash, k, v)
|
15
|
+
if v.is_a? Hash
|
16
|
+
v.each do |sub_k, sub_v|
|
17
|
+
add_param(hash, "#{k}[#{sub_k}]", sub_v)
|
18
|
+
end
|
19
|
+
else
|
20
|
+
hash[k] = v
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.serialize_value(v)
|
25
|
+
allowed_characters = NSCharacterSet.URLQueryAllowedCharacterSet.mutableCopy
|
26
|
+
allowed_characters.removeCharactersInString(":#[]@!$&'()*+,;=")
|
27
|
+
NSString.alloc.initWithString(v.to_s).stringByAddingPercentEncodingWithAllowedCharacters(allowed_characters)
|
28
|
+
end
|
29
|
+
end
|
data/lib/cocoa/json.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
class JSON
|
2
|
+
def self.parse(json_string)
|
3
|
+
error_ptr = Pointer.new(:id)
|
4
|
+
object = NSJSONSerialization.JSONObjectWithData(json_string.dataUsingEncoding(NSUTF8StringEncoding), options: 0, error: error_ptr)
|
5
|
+
unless object
|
6
|
+
raise error_ptr[0].description
|
7
|
+
end
|
8
|
+
object
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.generate(object)
|
12
|
+
raise "Invalid JSON object" unless NSJSONSerialization.isValidJSONObject(object)
|
13
|
+
error_ptr = Pointer.new(:id)
|
14
|
+
data = NSJSONSerialization.dataWithJSONObject(object, options: 0, error: error_ptr)
|
15
|
+
unless data
|
16
|
+
raise error_ptr[0].description
|
17
|
+
end
|
18
|
+
data.to_s
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Object
|
23
|
+
def to_json
|
24
|
+
JSON.generate(self)
|
25
|
+
end
|
26
|
+
end
|
data/lib/common/http/client.rb
CHANGED
@@ -4,7 +4,7 @@ class Motion
|
|
4
4
|
attr_reader :base_url
|
5
5
|
|
6
6
|
def initialize(base_url = nil)
|
7
|
-
@base_url = base_url ||
|
7
|
+
@base_url = base_url || ''
|
8
8
|
@headers = Headers.new
|
9
9
|
end
|
10
10
|
|
@@ -25,19 +25,31 @@ class Motion
|
|
25
25
|
@headers
|
26
26
|
end
|
27
27
|
|
28
|
-
# FIXME: doesn't work on Android
|
28
|
+
# FIXME: doesn't work on Android for some reason
|
29
29
|
# [:get, :post, :put, :patch, :delete].each do |method|
|
30
|
-
# define_method "#{method}", do |path, params = nil, &callback|
|
31
|
-
# Request.new(method, base_url + path, headers, params, &callback)
|
30
|
+
# define_method "#{method}", do |path, params = nil, options = nil, &callback|
|
31
|
+
# Request.new(method, base_url + path, headers, params, options).perform(&callback)
|
32
32
|
# end
|
33
33
|
# end
|
34
34
|
|
35
|
-
def get(path, params = nil, &callback)
|
36
|
-
Request.new(:get, base_url + path,
|
35
|
+
def get(path, params = nil, options = nil, &callback)
|
36
|
+
Request.new(:get, base_url + path, headers, params, options).perform(&callback)
|
37
37
|
end
|
38
38
|
|
39
|
-
def post(path, params = nil, &callback)
|
40
|
-
Request.new(:post, base_url + path,
|
39
|
+
def post(path, params = nil, options = nil, &callback)
|
40
|
+
Request.new(:post, base_url + path, headers, params, options).perform(&callback)
|
41
|
+
end
|
42
|
+
|
43
|
+
def put(path, params = nil, options = nil, &callback)
|
44
|
+
Request.new(:put, base_url + path, headers, params, options).perform(&callback)
|
45
|
+
end
|
46
|
+
|
47
|
+
def patch(path, params = nil, options = nil, &callback)
|
48
|
+
Request.new(:patch, base_url + path, headers, params, options).perform(&callback)
|
49
|
+
end
|
50
|
+
|
51
|
+
def delete(path, params = nil, options = nil, &callback)
|
52
|
+
Request.new(:delete, base_url + path, headers, params, options).perform(&callback)
|
41
53
|
end
|
42
54
|
end
|
43
55
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Motion
|
2
|
+
class HTTP
|
3
|
+
class Logger
|
4
|
+
def log(message)
|
5
|
+
puts message # TODO: add option to enable/disable logging
|
6
|
+
end
|
7
|
+
|
8
|
+
def log_request(request)
|
9
|
+
log "Request:\n#{request.http_method.to_s.upcase} #{request.url}"
|
10
|
+
|
11
|
+
if request.headers
|
12
|
+
request.headers.each do |k,v|
|
13
|
+
log "#{k}: #{v.inspect}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
if request.params
|
18
|
+
# log serialized_params
|
19
|
+
request.params.each do |k,v|
|
20
|
+
log "\t#{k}=#{v.inspect}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
log "\n"
|
24
|
+
end
|
25
|
+
|
26
|
+
def log_response(response)
|
27
|
+
log "Response:"
|
28
|
+
log "URL: #{response.original_request.url}"
|
29
|
+
log "Status: #{response.status_code}"
|
30
|
+
response.headers.each do |key, value|
|
31
|
+
log "#{key}: #{value}"
|
32
|
+
end
|
33
|
+
log "\n#{response.body}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/common/http/request.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
class Motion
|
2
2
|
class HTTP
|
3
3
|
class Request
|
4
|
-
attr_reader :
|
4
|
+
attr_reader :http_method, :url, :headers, :params, :options
|
5
5
|
|
6
|
-
def initialize(
|
7
|
-
@
|
6
|
+
def initialize(http_method, url, headers = nil, params = nil, options = nil)
|
7
|
+
@http_method = http_method
|
8
8
|
@url = url
|
9
9
|
@headers = headers || Headers.new
|
10
|
-
@
|
11
|
-
@
|
10
|
+
@params = params
|
11
|
+
@options = options
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
|
16
|
-
Adapter.
|
14
|
+
def perform(&callback)
|
15
|
+
Motion::HTTP.logger.log_request(self)
|
16
|
+
Adapter.perform(self, &callback)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
data/lib/common/http/response.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
class Motion
|
2
2
|
class HTTP
|
3
3
|
class Response
|
4
|
-
attr_reader :status_code, :headers, :body
|
4
|
+
attr_reader :original_request, :status_code, :headers, :body
|
5
5
|
|
6
|
-
def initialize(status_code, headers,
|
6
|
+
def initialize(original_request, status_code, headers, body)
|
7
|
+
@original_request = original_request
|
7
8
|
@status_code = status_code
|
8
9
|
@headers = headers
|
9
|
-
@body =
|
10
|
-
@object = body_object
|
10
|
+
@body = body
|
11
11
|
end
|
12
12
|
|
13
13
|
def success?
|
@@ -15,8 +15,17 @@ class Motion
|
|
15
15
|
status_code >= 200 && status_code < 300
|
16
16
|
end
|
17
17
|
|
18
|
+
def object
|
19
|
+
@object ||= case headers['Content-Type']
|
20
|
+
when /^application\/json/, /^application\/vnd.api\+json/
|
21
|
+
JSON.parse(body)
|
22
|
+
else
|
23
|
+
body
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
18
27
|
def inspect
|
19
|
-
"<Motion::HTTP::Response status_code:#{status_code} headers
|
28
|
+
"<Motion::HTTP::Response status_code:#{status_code} headers:#{headers.inspect} body:#{body.inspect}>"
|
20
29
|
end
|
21
30
|
end
|
22
31
|
end
|
data/lib/common/http.rb
CHANGED
@@ -1,36 +1,42 @@
|
|
1
1
|
class Motion
|
2
2
|
class HTTP
|
3
3
|
class << self
|
4
|
+
def logger
|
5
|
+
@logger ||= Logger.new
|
6
|
+
end
|
7
|
+
|
4
8
|
def client
|
5
9
|
@client ||= Client.new
|
6
10
|
end
|
7
11
|
|
8
12
|
# FIXME: doesn't work on Android
|
9
13
|
# [:get, :post, :put, :patch, :delete].each do |method|
|
10
|
-
# define_method "#{method}", do |url, params = nil, &callback|
|
11
|
-
# client.send(method, url, params, &callback)
|
14
|
+
# define_method "#{method}", do |url, params = nil, options = nil, &callback|
|
15
|
+
# client.send(method, url, params, options, &callback)
|
12
16
|
# end
|
13
17
|
# end
|
14
18
|
|
15
|
-
def get(url, params = nil, &callback)
|
16
|
-
client.get(url, params, &callback)
|
19
|
+
def get(url, params = nil, options = nil, &callback)
|
20
|
+
client.get(url, params, options, &callback)
|
17
21
|
end
|
18
22
|
|
19
|
-
def post(url, params = nil, &callback)
|
20
|
-
client.post(url, params, &callback)
|
23
|
+
def post(url, params = nil, options = nil, &callback)
|
24
|
+
client.post(url, params, options, &callback)
|
21
25
|
end
|
22
26
|
|
23
|
-
def put(url, params = nil, &callback)
|
24
|
-
client.put(url, params, &callback)
|
27
|
+
def put(url, params = nil, options = nil, &callback)
|
28
|
+
client.put(url, params, options, &callback)
|
25
29
|
end
|
26
30
|
|
27
|
-
def patch(url, params = nil, &callback)
|
28
|
-
client.patch(url, params, &callback)
|
31
|
+
def patch(url, params = nil, options = nil, &callback)
|
32
|
+
client.patch(url, params, options, &callback)
|
29
33
|
end
|
30
34
|
|
31
|
-
def delete(url, params = nil, &callback)
|
32
|
-
client.delete(url, params, &callback)
|
35
|
+
def delete(url, params = nil, options = nil, &callback)
|
36
|
+
client.delete(url, params, options, &callback)
|
33
37
|
end
|
34
38
|
end
|
35
39
|
end
|
36
40
|
end
|
41
|
+
|
42
|
+
HTTP = Motion::HTTP # alias as simply HTTP
|
data/lib/motion-http.rb
CHANGED
@@ -16,11 +16,7 @@ Motion::Project::App.setup do |app|
|
|
16
16
|
dependency "com.squareup.okhttp3:okhttp:3.9.0"
|
17
17
|
end
|
18
18
|
when :ios, :tvos, :osx, :watchos, :'ios-extension'
|
19
|
-
require "motion-cocoapods"
|
20
19
|
app.files.unshift(*Dir.glob(File.join(lib_dir_path, "cocoa/**/*.rb")))
|
21
|
-
app.pods do
|
22
|
-
pod "AFNetworking", "~> 3.1"
|
23
|
-
end
|
24
20
|
else
|
25
21
|
raise "Project template #{Motion::Project::App.template} not supported by motion-http"
|
26
22
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: motion-http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Havens
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A cross-platform HTTP client for RubyMotion that's quick and easy to
|
14
14
|
use.
|
@@ -22,9 +22,12 @@ files:
|
|
22
22
|
- lib/android/adapter.rb
|
23
23
|
- lib/android/json.rb
|
24
24
|
- lib/cocoa/adapter.rb
|
25
|
+
- lib/cocoa/form_data_serializer.rb
|
26
|
+
- lib/cocoa/json.rb
|
25
27
|
- lib/common/http.rb
|
26
28
|
- lib/common/http/client.rb
|
27
29
|
- lib/common/http/headers.rb
|
30
|
+
- lib/common/http/logger.rb
|
28
31
|
- lib/common/http/request.rb
|
29
32
|
- lib/common/http/response.rb
|
30
33
|
- lib/motion-http.rb
|
@@ -48,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
51
|
version: '0'
|
49
52
|
requirements: []
|
50
53
|
rubyforge_project:
|
51
|
-
rubygems_version: 2.6
|
54
|
+
rubygems_version: 2.7.6
|
52
55
|
signing_key:
|
53
56
|
specification_version: 4
|
54
57
|
summary: A cross-platform HTTP client for RubyMotion that's quick and easy to use.
|