remotr 0.1.3 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b86be1a17065f1e2ba62cbd6a57ff4dd4c771eed
4
- data.tar.gz: 6d97ba9c85f03a8da80f7e61815b09f51b88d036
3
+ metadata.gz: 12851189a061bb27e591ff38748c84e5145d930a
4
+ data.tar.gz: 2aab34dd644d9c60fd318c23ca57c3f0e143c881
5
5
  SHA512:
6
- metadata.gz: a32a14d7378d2a89d85d0c631096dc639d378ef68fee01f1480fff4a6927625cd8c987855cd4a759a8167a0e199c3b32f997305d4a6480c9aaf1d1c3ad748ed7
7
- data.tar.gz: 8cbed702ea2b1272bd968a21b6d31a4a07a4cac92d56427ff157312610fa782b36c61ab9d0e7fc70b8ad96ee9fcf25b834842a1d669a7178915de7685b062013
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 RemoteExpress
5
- module Hub
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('/hubs'), 'hubs'
22
+ respond_with get('/events'), :events
10
23
  end
11
-
12
24
  end
13
25
  end
14
26
  ```
@@ -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
- # Public: Returns the the configuration instance.
7
- #
8
- def config
9
- @config ||= ::Remotr::Configuration.new
10
- end
8
+ module ClassMethods
9
+ # Public: Returns the the configuration instance.
10
+ #
11
+ def config
12
+ @config ||= ::Remotr::Configuration.new
13
+ end
11
14
 
12
- # Public: Yields the configuration instance.
13
- #
14
- def configure(&block)
15
- yield config
16
- end
15
+ # Public: Yields the configuration instance.
16
+ #
17
+ def configure(&block)
18
+ yield config
19
+ end
17
20
 
18
- # Public: Reset the configuration (useful for testing).
19
- #
20
- def reset!
21
- @config = nil
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
@@ -1,5 +1,5 @@
1
1
  module Remotr
2
2
  class Configuration
3
- attr_accessor :base_uri, :base_path, :api_version, :api_key
3
+ attr_accessor :base_uri, :base_path, :api_version, :api_key, :default_timeout
4
4
  end
5
5
  end
@@ -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 :remote_request_succeeded, object: object, code: httparty_response.code, body: httparty_response.body
51
+ Operations.success :request_succeeded, object: httparty_response
57
52
  else
58
- Operations.failure :remote_request_failed, object: httparty_response, code: httparty_response.code
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 :remote_request_parsing_failed, object: httparty_response
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remotr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - bukowskis