remotr 0.1.3 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -4
- data/lib/remotr/configurable.rb +18 -14
- data/lib/remotr/configuration.rb +1 -1
- data/lib/remotr/respondable.rb +25 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12851189a061bb27e591ff38748c84e5145d930a
|
4
|
+
data.tar.gz: 2aab34dd644d9c60fd318c23ca57c3f0e143c881
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68010968966867568fd8ea17157cf17ccfe680a848710ba4e68a4c9f392bcda6d60196ca65f344a58d27b8eae5e83f44f614c3da2ce22492be72506373ede9c3
|
7
|
+
data.tar.gz: 1e235e0dca4cc4bbdd70006336540bbe959e8966a6401d73f6cf762af8d77b72b97a69543be921cb5b7ac04558d88d226d3732b2bb011075e3c6bc1a5e3c9b74
|
data/README.md
CHANGED
@@ -1,14 +1,26 @@
|
|
1
|
+
# Remotr
|
2
|
+
|
1
3
|
Example usage:
|
2
4
|
|
3
5
|
```ruby
|
4
|
-
module
|
5
|
-
|
6
|
+
module RemoteMyApp
|
7
|
+
include Remotr::Configurable
|
8
|
+
end
|
9
|
+
|
10
|
+
RemoteMyApp.configure do |config|
|
11
|
+
config.base_uri = 'https://myapp.example.com'
|
12
|
+
config.api_key = 'abcdef'
|
13
|
+
config.api_version = 1
|
14
|
+
config.base_path = "/api/v#{config.api_version}"
|
15
|
+
end
|
16
|
+
|
17
|
+
module RemoteMyApp
|
18
|
+
module Event
|
6
19
|
include Remotr::Respondable
|
7
20
|
|
8
21
|
def self.all
|
9
|
-
respond_with get('/
|
22
|
+
respond_with get('/events'), :events
|
10
23
|
end
|
11
|
-
|
12
24
|
end
|
13
25
|
end
|
14
26
|
```
|
data/lib/remotr/configurable.rb
CHANGED
@@ -1,24 +1,28 @@
|
|
1
1
|
require 'remotr/configuration'
|
2
|
+
require 'active_support/concern'
|
2
3
|
|
3
4
|
module Remotr
|
4
5
|
module Configurable
|
6
|
+
extend ActiveSupport::Concern
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
module ClassMethods
|
9
|
+
# Public: Returns the the configuration instance.
|
10
|
+
#
|
11
|
+
def config
|
12
|
+
@config ||= ::Remotr::Configuration.new
|
13
|
+
end
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
# Public: Yields the configuration instance.
|
16
|
+
#
|
17
|
+
def configure(&block)
|
18
|
+
yield config
|
19
|
+
end
|
17
20
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
# Public: Reset the configuration (useful for testing).
|
22
|
+
#
|
23
|
+
def reset!
|
24
|
+
@config = nil
|
25
|
+
end
|
22
26
|
end
|
23
27
|
|
24
28
|
end
|
data/lib/remotr/configuration.rb
CHANGED
data/lib/remotr/respondable.rb
CHANGED
@@ -45,23 +45,39 @@ module Remotr
|
|
45
45
|
url = URI.join(config.base_uri, path).to_s
|
46
46
|
|
47
47
|
fail ArgumentError unless %w( get post delete put update ).include? method.to_s
|
48
|
-
HTTParty.send method, url, query: query_params, body: body, headers: { 'Accept' => 'application/json' }
|
49
|
-
end
|
50
|
-
|
51
|
-
def respond_with(httparty_response, custom_namespace = nil)
|
52
|
-
use_namespace = custom_namespace || namespace
|
53
|
-
object = httparty_response.parsed_response ? httparty_response.parsed_response[use_namespace.to_s] : nil
|
48
|
+
httparty_response = HTTParty.send method, url, timeout: timeout_in_seconds, query: query_params, body: body, headers: { 'Accept' => 'application/json' }
|
54
49
|
|
55
50
|
if httparty_response.code.to_i.between? 200, 299
|
56
|
-
Operations.success :
|
51
|
+
Operations.success :request_succeeded, object: httparty_response
|
57
52
|
else
|
58
|
-
Operations.failure :
|
53
|
+
Operations.failure :request_failed, object: httparty_response
|
59
54
|
end
|
60
55
|
|
56
|
+
rescue => exception
|
57
|
+
Operations.failure :connection_failed, object: exception
|
58
|
+
end
|
59
|
+
|
60
|
+
def respond_with(request_operation, namespace_to_use = namespace)
|
61
|
+
return request_operation if request_operation.failure?
|
62
|
+
httparty_response = request_operation.object
|
63
|
+
|
64
|
+
return Operations.failure(:response_missing_content_type, object: httparty_response) unless httparty_response.content_type
|
65
|
+
return Operations.failure(:response_is_not_json, object: httparty_response) unless httparty_response.content_type == 'application/json'
|
66
|
+
parsed_response = httparty_response.parsed_response
|
67
|
+
return Operations.failure(:response_missing_success_flag, object: httparty_response) unless parsed_response && parsed_response.key?('success')
|
68
|
+
return Operations.failure(:response_unsuccessful, object: httparty_response) if parsed_response['success'].to_s != 'true'
|
69
|
+
|
70
|
+
object = parsed_response ? parsed_response[namespace_to_use.to_s] : nil
|
71
|
+
|
72
|
+
Operations.success :request_succeeded, object: object, code: httparty_response.code, body: httparty_response.body
|
73
|
+
|
61
74
|
rescue JSON::ParserError
|
62
|
-
Operations.failure :
|
75
|
+
Operations.failure :json_parsing_failed, object: httparty_response
|
63
76
|
end
|
64
77
|
|
78
|
+
def timeout_in_seconds
|
79
|
+
config.default_timeout
|
80
|
+
end
|
65
81
|
end
|
66
82
|
|
67
83
|
end
|