motion-http 0.1.0 → 0.1.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: ee2f69f478379a7838d88c114210624948da74f8
4
- data.tar.gz: d5ac4b833aec97568819341f555a07ef48507fb3
3
+ metadata.gz: 295321b18316c5b3a47c29692b4831e25ed4b1c7
4
+ data.tar.gz: afcdbfc33304f05cf290fcdd33e339ad693fcb74
5
5
  SHA512:
6
- metadata.gz: 32c6a1ade57a803e9a99416d6591d9b6afc510d3dbe06edba804c4f5c35cbe8b36f27282c718ed7231bfb0af22a98984831eba9c7256da2571310dc03a83f62a
7
- data.tar.gz: ca7d792bb704dcd23da1d1454c138f426eadeb1fba2003750243b053a8df07b9cd2a94494283d58fae2b2b8dde1789b36e3411412e1de4cdcfabb3de78ea820b
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::HTTPClient.new("http://www.example.com")
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
- # Use add_header to append multiple headers of the same name.
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
  ```
@@ -1,7 +1,7 @@
1
1
  class Motion
2
2
  class HTTP
3
3
  class Client
4
- attr_reader :base_url, :headers
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
@@ -11,7 +11,8 @@ class Motion
11
11
 
12
12
  def add(key, value)
13
13
  key = key.downcase
14
- if @headers[key] && !@headers[key].is_a?(Array)
14
+ @headers[key] ||= []
15
+ unless @headers[key].is_a?(Array)
15
16
  @headers[key] = [@headers[key]]
16
17
  end
17
18
  @headers[key] << value
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.0
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-16 00:00:00.000000000 Z
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.