motion-http 0.1.0 → 0.1.1
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/README.md +23 -6
- data/lib/common/http/client.rb +14 -5
- data/lib/common/http/headers.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 295321b18316c5b3a47c29692b4831e25ed4b1c7
|
4
|
+
data.tar.gz: afcdbfc33304f05cf290fcdd33e339ad693fcb74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '095e68b01bca380833a6e93c2e25e5e6abd389727b6ff92aa1b39c9fef79132bec440ea2b280b4e0e5436535534dc158e234d1ed54bc9c99cc518a246fa5cf77'
|
7
|
+
data.tar.gz: 0667af5350564fb4f4199437a3ae827435f5ce942842ca333adba98ed46efaa7a8c4ff1dce85dc67c17b9fa382abdf28b7ba5645fa0e6064581cf405e9e3e5ed
|
data/README.md
CHANGED
@@ -6,6 +6,12 @@ Supported platforms:
|
|
6
6
|
- iOS, macOS, tvOS, watchOS
|
7
7
|
- Android
|
8
8
|
|
9
|
+
This gem depends on two really popular networking libraries:
|
10
|
+
- [AFNetworking](https://github.com/AFNetworking/AFNetworking) (for Cocoa platforms)
|
11
|
+
- [OkHttp](http://square.github.io/okhttp/) (for Android)
|
12
|
+
|
13
|
+
Please note that this library is still a work in progress. Please report bugs and suggestions for improvement!
|
14
|
+
|
9
15
|
## Installation
|
10
16
|
|
11
17
|
Add this line to your application's Gemfile:
|
@@ -18,6 +24,12 @@ And then execute:
|
|
18
24
|
$ rake pod:install # for iOS apps
|
19
25
|
$ rake gradle:install # for Android apps
|
20
26
|
|
27
|
+
### iOS Specific Configuration
|
28
|
+
|
29
|
+
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:
|
30
|
+
|
31
|
+
app.info_plist['NSAppTransportSecurity'] = { 'NSAllowsArbitraryLoads' => true }
|
32
|
+
|
21
33
|
## Usage
|
22
34
|
|
23
35
|
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.
|
@@ -80,9 +92,9 @@ end
|
|
80
92
|
|
81
93
|
`PUT`, `PATCH`, and `DELETE` requests work the same way:
|
82
94
|
```ruby
|
83
|
-
Motion::HTTP.put(url, params)
|
84
|
-
Motion::HTTP.patch(url, params)
|
85
|
-
Motion::HTTP.delete(url, params)
|
95
|
+
Motion::HTTP.put(url, params) { ... }
|
96
|
+
Motion::HTTP.patch(url, params) { ... }
|
97
|
+
Motion::HTTP.delete(url, params) { ... }
|
86
98
|
```
|
87
99
|
|
88
100
|
### Advanced Usage
|
@@ -90,11 +102,16 @@ Motion::HTTP.delete(url, params)
|
|
90
102
|
A common use case is to create a reusable HTTP client that uses a common base URL or request headers.
|
91
103
|
|
92
104
|
```ruby
|
93
|
-
client = Motion::
|
94
|
-
# Set or replace a header
|
105
|
+
client = Motion::HTTP::Client.new("http://www.example.com")
|
106
|
+
# Set or replace a single header:
|
95
107
|
client.header "X-API-TOKEN", "abc123xyz"
|
108
|
+
|
109
|
+
# To set or replace multiple headers:
|
110
|
+
client.headers "X-API-TOKEN" => "abc123xyz",
|
111
|
+
"Accept" => "application/json"
|
112
|
+
|
96
113
|
# It is valid for some headers to appear multiple times (Accept, Vary, etc).
|
97
|
-
#
|
114
|
+
# To append multiple headers of the same key:
|
98
115
|
client.add_header "Accept", "application/json"
|
99
116
|
client.add_header "Accept", "application/vnd.api+json"
|
100
117
|
```
|
data/lib/common/http/client.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
class Motion
|
2
2
|
class HTTP
|
3
3
|
class Client
|
4
|
-
attr_reader :base_url
|
4
|
+
attr_reader :base_url
|
5
5
|
|
6
6
|
def initialize(base_url = nil)
|
7
7
|
@base_url = base_url || ""
|
@@ -9,11 +9,20 @@ class Motion
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def header(key, value)
|
12
|
-
headers.set(key, value)
|
12
|
+
@headers.set(key, value)
|
13
13
|
end
|
14
14
|
|
15
15
|
def add_header(key, value)
|
16
|
-
headers.add(key, value)
|
16
|
+
@headers.add(key, value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def headers(hash = nil)
|
20
|
+
if hash
|
21
|
+
hash.each do |key, value|
|
22
|
+
@headers.set(key, value)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
@headers
|
17
26
|
end
|
18
27
|
|
19
28
|
# FIXME: doesn't work on Android
|
@@ -24,11 +33,11 @@ class Motion
|
|
24
33
|
# end
|
25
34
|
|
26
35
|
def get(path, params = nil, &callback)
|
27
|
-
Request.new(:get, base_url + path, headers, params, &callback).call
|
36
|
+
Request.new(:get, base_url + path, @headers, params, &callback).call
|
28
37
|
end
|
29
38
|
|
30
39
|
def post(path, params = nil, &callback)
|
31
|
-
Request.new(:post, base_url + path, headers, params, &callback).call
|
40
|
+
Request.new(:post, base_url + path, @headers, params, &callback).call
|
32
41
|
end
|
33
42
|
end
|
34
43
|
end
|
data/lib/common/http/headers.rb
CHANGED
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.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Havens
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-26 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.
|