remotr 0.0.1 → 0.0.2

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: 6f76c90f0e5342f74e50a393cb1ac864f9393da5
4
- data.tar.gz: defd7a0013c259da30e8690e18673e4676d3d470
3
+ metadata.gz: fa3338323e20475acdafb664d9653e97ae8079f8
4
+ data.tar.gz: 8e7bb12c942214fd8286fea3398497d16980291d
5
5
  SHA512:
6
- metadata.gz: 9b36ed62825f3c9da8ba3caa4e13664d6a0fe2299d1a7a1b310e437e782806ee7ef1b4c4bc70664f2a2929a1e7ff4788302b0b604e4384fd7d4b1142b9280205
7
- data.tar.gz: ae5c6726f94261aeac66b1fcf0272ed17642312d07f2e7b745f7444eb197dcff90491762b1a060e87af41ee6270e3a2a7c780d84041563e61bba4b8ba81a367f
6
+ metadata.gz: 39400293b478e483f564e3ca2f691b86b9b2dbaae25b369c62b40c32597432e1af4cb05f2c11b7d786df15d08febd9efc9263bdf713638342be30ce849c3a030
7
+ data.tar.gz: c97885b428f64e0cd7773ee7b1445d6c3192570a2dd7473e72a61504b1875535fdfe7076a7af0a4d88b7f253cfb605739787ed1c514ace4506cebefb80d05f7c
data/README.md ADDED
@@ -0,0 +1,14 @@
1
+ Example usage:
2
+
3
+ ```ruby
4
+ module RemoteExpress
5
+ module Hub
6
+ include Remotr::Respondable
7
+
8
+ def self.all
9
+ respond_with get('/hubs'), 'hubs'
10
+ end
11
+
12
+ end
13
+ end
14
+ ```
@@ -0,0 +1,25 @@
1
+ require 'remotr/configuration'
2
+
3
+ module Remotr
4
+ module Configurable
5
+
6
+ # Public: Returns the the configuration instance.
7
+ #
8
+ def config
9
+ @config ||= ::Remotr::Configuration.new
10
+ end
11
+
12
+ # Public: Yields the configuration instance.
13
+ #
14
+ def configure(&block)
15
+ yield config
16
+ end
17
+
18
+ # Public: Reset the configuration (useful for testing).
19
+ #
20
+ def reset!
21
+ @config = nil
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Remotr
2
+ class Configuration
3
+ attr_accessor :base_uri, :base_path, :api_version, :api_key
4
+ end
5
+ end
@@ -0,0 +1,66 @@
1
+ require 'operation'
2
+ require 'httparty'
3
+ require 'active_support/concern'
4
+ require 'active_support/core_ext/string'
5
+
6
+ module Remotr
7
+ module Respondable
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ def config
12
+ self.name.deconstantize.constantize.config
13
+ end
14
+
15
+ def namespace
16
+ self.name.demodulize.pluralize.underscore
17
+ end
18
+
19
+ def application
20
+ self.name.deconstantize.underscore
21
+ end
22
+
23
+ def get(path = {}, params = {})
24
+ request :get, path, params
25
+ end
26
+
27
+ def post(path = {}, params = {}, body = nil)
28
+ request :post, path, params
29
+ end
30
+
31
+ def delete(path = {}, params = {})
32
+ request :delete, path, params
33
+ end
34
+
35
+ def put(path = {}, params = {}, body = nil)
36
+ request :put, path, params
37
+ end
38
+
39
+ def request(method, path, params, body = nil)
40
+ url = URI.join(config.base_uri, "#{config.base_path}#{path}").to_s
41
+ fail ArgumentError unless %w( get post delete put update ).include? method.to_s
42
+
43
+ HTTParty.send method, url,
44
+ query: params,
45
+ headers: { 'Accept' => 'application/json' },
46
+ basic_auth: { username: 'api', password: config.api_key }
47
+ end
48
+
49
+ def respond_with(httparty_response, custom_namespace = nil)
50
+ use_namespace = custom_namespace || namespace
51
+ object = httparty_response.parsed_response ? httparty_response.parsed_response[use_namespace.to_s] : nil
52
+
53
+ if httparty_response.code.to_i.between? 200, 299
54
+ Operations.success :remote_request_succeeded, object: object, code: httparty_response.code, body: httparty_response.body
55
+ else
56
+ Operations.failure :remote_request_failed, object: httparty_response, code: httparty_response.code
57
+ end
58
+
59
+ rescue JSON::ParserError
60
+ Operations.failure :remote_request_parsing_failed, object: httparty_response
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ 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.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - bukowskis
@@ -114,7 +114,11 @@ executables: []
114
114
  extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
+ - README.md
117
118
  - lib/remotr.rb
119
+ - lib/remotr/configurable.rb
120
+ - lib/remotr/configuration.rb
121
+ - lib/remotr/respondable.rb
118
122
  homepage: https://github.com/bukowskis/remotr
119
123
  licenses: []
120
124
  metadata: {}