proxima 1.1.1 → 2.0.0

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: cd5615b370a04a9c8c474fc142242e78fb9b5d81
4
- data.tar.gz: ad9ffa8bd99d82ad192971607dfea44ceb6e43d2
3
+ metadata.gz: 8d40a3e470e452d37b6d587e7925763a1ccf6424
4
+ data.tar.gz: 2c7a02576f7b706b24f5bfc1e7925259eff5cde6
5
5
  SHA512:
6
- metadata.gz: 7cd3f7973557921d3fdfbcc62607ac19f7eb29b6fffcc0171330ae068c045f29e87320b9941d2327988ab852831e2ff8eb3b9207882509b0849031c57e9ea00d
7
- data.tar.gz: 0e8f6b84a313e58bf8756a8c9f5ab2f31f2ff447baf90396a810182bf4b3d85882750d491e598155dade32b51d54a9f3faa4b1c08542357f1e8d280f261f4c75
6
+ metadata.gz: 0f6f84e8d906a2db653c068db1bbe46a35ae61d2c873b6f1dc10070ca7fa8e31f60924b179802d708b5ce0b37af04a1afb9ed1908a5b250187ff37567896379d
7
+ data.tar.gz: f0bb3bd5cb94e2193bb8d706e87b87659719fd648d97b81756823ced4f2f34f26820afeaf283f1b4d3200d3be70c20e8c555924a919bb2c801e705feffedb022
data/lib/proxima/api.rb CHANGED
@@ -1,50 +1,22 @@
1
- require 'rest-client'
2
-
3
1
 
4
2
  module Proxima
5
-
6
3
  class Api
7
4
 
8
- attr_accessor :url, :headers
5
+ attr_reader :base_uri, :headers
9
6
 
10
7
  def initialize(base_url, opts = {})
11
- @base_url = base_url
8
+ @base_uri = URI.parse base_url
12
9
  @headers = opts[:headers] || {}
13
10
  end
14
11
 
15
- def post(path, opts = {}, &block)
16
- self.request(:post, path, opts, &block)
17
- end
18
-
19
- def get(path, opts = {}, &block)
20
- self.request(:get, path, opts, &block)
21
- end
22
-
23
- def put(path, opts = {}, &block)
24
- self.request(:put, path, opts, &block)
25
- end
26
-
27
- def delete(path, opts = {}, &block)
28
- self.request(:delete, path, opts, &block)
12
+ HTTP_METHODS.each do |http_method|
13
+ define_method http_method do |path, opts = {}, &block|
14
+ self.request http_method, path, opts, &block
15
+ end
29
16
  end
30
17
 
31
18
  def request(method, path, opts = {}, &block)
32
- headers = @headers.clone
33
- headers.merge!(opts[:headers]) if opts[:headers]
34
- headers[:params] = opts[:query] if opts[:query]
35
- headers[:content_type] = :json if opts[:json]
36
- payload = opts[:json].to_json if opts[:json]
37
-
38
- begin
39
- RestClient::Request.execute({
40
- method: method,
41
- url: @base_url + path,
42
- headers: headers,
43
- payload: payload
44
- }, &block)
45
- rescue RestClient::Exception => e
46
- e.response
47
- end
19
+ Proxima::Request.new(self, method, path, opts, &block).response
48
20
  end
49
21
  end
50
22
  end
@@ -2,9 +2,11 @@
2
2
 
3
3
  module Proxima
4
4
 
5
- module Rest
5
+ HTTP_METHODS = [:head, :post, :get, :put, :delete]
6
6
 
7
- [:post, :get, :put, :delete].each do |http_method|
7
+ module HTTPMethods
8
+
9
+ HTTP_METHODS.each do |http_method|
8
10
  define_method :"#{http_method}" do |path, opts = {}, &block|
9
11
  @response = self.class.api.public_send(:"#{http_method}", path, opts, &block)
10
12
  @response
@@ -13,7 +15,7 @@ module Proxima
13
15
 
14
16
  module ClassMethods
15
17
 
16
- [:post, :get, :put, :delete].each do |http_method|
18
+ HTTP_METHODS.each do |http_method|
17
19
  define_method :"#{http_method}" do |path, opts = {}, &block|
18
20
  @response = self.api.public_send(:"#{http_method}", path, opts, &block)
19
21
  @response
data/lib/proxima/model.rb CHANGED
@@ -1,11 +1,3 @@
1
- require 'active_model'
2
- require 'proxima/watch'
3
- require 'proxima/rest'
4
- require 'proxima/attributes'
5
- require 'proxima/paths'
6
- require 'proxima/serialization'
7
- require 'proxima/validation'
8
-
9
1
 
10
2
  module Proxima
11
3
 
@@ -19,7 +11,7 @@ module Proxima
19
11
  include ActiveModel::Serializers::JSON
20
12
  include ActiveModel::Validations
21
13
 
22
- include Proxima::Rest
14
+ include Proxima::HTTPMethods
23
15
  include Proxima::Attributes
24
16
  include Proxima::Paths
25
17
  include Proxima::Serialization
@@ -0,0 +1,45 @@
1
+
2
+ module Proxima
3
+ class Request
4
+
5
+ attr_reader :response
6
+ attr_reader :method
7
+ attr_reader :uri
8
+ attr_reader :headers
9
+ attr_reader :body
10
+
11
+ def initialize(api, method, path, opts = {})
12
+ @api = api
13
+ @method = method.to_s.upcase
14
+
15
+ headers = opts[:headers] || {}
16
+
17
+ @body = if opts[:json]
18
+ headers[:content_type] = 'application/json'
19
+ opts[:json].to_json
20
+ elsif opts[:body]
21
+ opts[:body]
22
+ else
23
+ ''
24
+ end
25
+
26
+ headers.merge! @api.headers
27
+
28
+ @headers = headers.map{ |name, val| [to_header(name), val.to_s] }.to_h
29
+ query_str = opts[:query] ? "?#{opts[:query].to_query}" : ''
30
+ @uri = URI.join @api.base_uri, path, query_str
31
+ @http = Net::HTTP.new(@uri.host, @uri.port)
32
+ end
33
+
34
+ def response
35
+ raw_response = @http.send_request @method, @uri, @body, @headers
36
+ Response.new self, raw_response
37
+ end
38
+
39
+ private
40
+
41
+ def to_header symbol_or_string
42
+ symbol_or_string.to_s.split(/[_ -]/).map!{ |w| w.downcase.capitalize }.join '-'
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,36 @@
1
+
2
+ module Proxima
3
+ class Response
4
+
5
+ attr_reader :request
6
+
7
+ def initialize(request, raw_response)
8
+ @request = request
9
+ @raw_response = raw_response
10
+ end
11
+
12
+ def json
13
+ begin
14
+ JSON.parse @raw_response.body if @raw_response.body
15
+ rescue => e
16
+ raise "Failed to parse response body as JSON string: #{e.message}"
17
+ end
18
+ end
19
+
20
+ def body
21
+ @raw_response.body
22
+ end
23
+
24
+ def code
25
+ @raw_response.code.to_i
26
+ end
27
+
28
+ def message
29
+ @raw_response.message
30
+ end
31
+
32
+ def http_version
33
+ @raw_response.http_version
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,76 @@
1
+
2
+ module Proxima
3
+ TYPES = [
4
+ {
5
+ klass: String,
6
+ from_json: -> v { v.to_s },
7
+ to_json: -> v { v.to_s }
8
+ }, {
9
+ klass: Integer,
10
+ from_json: -> v { v.to_i },
11
+ to_json: -> v { v.to_i }
12
+ }, {
13
+ klass: Float,
14
+ from_json: -> v { v.to_f },
15
+ to_json: -> v { v.to_f }
16
+ }, {
17
+ klass: Rational,
18
+ from_json: -> v { v.to_r },
19
+ to_json: -> v { v.to_r }
20
+ }, {
21
+ klass: Complex,
22
+ from_json: -> v { v.to_c },
23
+ to_json: -> v { v.to_c }
24
+ }, {
25
+ klass: TrueClass,
26
+ from_json: -> v { v.to_s == 'true' },
27
+ to_json: -> v { v.to_s == 'true' }
28
+ }, {
29
+ klass: Array,
30
+ from_json: -> v { v.to_a },
31
+ to_json: -> v { v.to_a }
32
+ }, {
33
+ klass: Hash,
34
+ from_json: -> v { v.to_h },
35
+ to_json: -> v { v.to_h }
36
+ }, {
37
+ klass: DateTime,
38
+ from_json: -> v { DateTime.iso8601 v },
39
+ to_json: -> v { v.iso8601 3 }
40
+ }
41
+ ]
42
+
43
+ def self.add_type(klass, from = nil, to = nil)
44
+ TYPES.push({
45
+ klass: klass,
46
+ from: from,
47
+ to: to
48
+ })
49
+ end
50
+
51
+ def self.remove_type(klass)
52
+ TYPES.delete_if({
53
+ klass: klass,
54
+ from: from,
55
+ to: to
56
+ })
57
+ end
58
+
59
+ def self.type_from_json(klass, value)
60
+ type = TYPES.find { |t| t[:klass] == klass }
61
+ if type
62
+ type[:from_json].call(value)
63
+ else
64
+ value
65
+ end
66
+ end
67
+
68
+ def self.type_to_json(klass, value)
69
+ type = TYPES.find { |t| t[:klass] == klass }
70
+ if type
71
+ type[:to_json].call(value)
72
+ else
73
+ value
74
+ end
75
+ end
76
+ end
@@ -1,5 +1,5 @@
1
1
 
2
2
 
3
3
  module Proxima
4
- VERSION = "1.1.1"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/proxima.rb CHANGED
@@ -1,81 +1,17 @@
1
- require "proxima/version"
2
- require "proxima/api"
3
- require "proxima/model"
4
-
1
+ require 'net/http'
2
+ require 'active_model'
3
+ require 'proxima/version'
4
+ require 'proxima/types'
5
+ require 'proxima/request'
6
+ require 'proxima/response'
7
+ require 'proxima/http_methods'
8
+ require 'proxima/api'
9
+ require 'proxima/attributes'
10
+ require 'proxima/serialization'
11
+ require 'proxima/paths'
12
+ require 'proxima/validation'
13
+ require 'proxima/watch'
14
+ require 'proxima/model'
5
15
 
6
16
  module Proxima
7
-
8
- @types = [
9
- {
10
- klass: String,
11
- from_json: -> v { v.to_s },
12
- to_json: -> v { v.to_s }
13
- }, {
14
- klass: Integer,
15
- from_json: -> v { v.to_i },
16
- to_json: -> v { v.to_i }
17
- }, {
18
- klass: Float,
19
- from_json: -> v { v.to_f },
20
- to_json: -> v { v.to_f }
21
- }, {
22
- klass: Rational,
23
- from_json: -> v { v.to_r },
24
- to_json: -> v { v.to_r }
25
- }, {
26
- klass: Complex,
27
- from_json: -> v { v.to_c },
28
- to_json: -> v { v.to_c }
29
- }, {
30
- klass: TrueClass,
31
- from_json: -> v { v.to_s == 'true' },
32
- to_json: -> v { v.to_s == 'true' }
33
- }, {
34
- klass: Array,
35
- from_json: -> v { v.to_a },
36
- to_json: -> v { v.to_a }
37
- }, {
38
- klass: Hash,
39
- from_json: -> v { v.to_h },
40
- to_json: -> v { v.to_h }
41
- }, {
42
- klass: DateTime,
43
- from_json: -> v { DateTime.iso8601 v },
44
- to_json: -> v { v.iso8601 3 }
45
- }
46
- ]
47
-
48
- def self.add_type(klass, from = nil, to = nil)
49
- @types.push({
50
- klass: klass,
51
- from: from,
52
- to: to
53
- })
54
- end
55
-
56
- def self.remove_type(klass)
57
- @types.delete_if({
58
- klass: klass,
59
- from: from,
60
- to: to
61
- })
62
- end
63
-
64
- def self.type_from_json(klass, value)
65
- type = @types.find { |t| t[:klass] == klass }
66
- if type
67
- type[:from_json].call(value)
68
- else
69
- value
70
- end
71
- end
72
-
73
- def self.type_to_json(klass, value)
74
- type = @types.find { |t| t[:klass] == klass }
75
- if type
76
- type[:to_json].call(value)
77
- else
78
- value
79
- end
80
- end
81
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proxima
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Hurst
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-05 00:00:00.000000000 Z
11
+ date: 2017-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,26 +72,6 @@ dependencies:
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
74
  version: 4.0.0
75
- - !ruby/object:Gem::Dependency
76
- name: rest-client
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: '2.0'
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- version: 2.0.0
85
- type: :runtime
86
- prerelease: false
87
- version_requirements: !ruby/object:Gem::Requirement
88
- requirements:
89
- - - "~>"
90
- - !ruby/object:Gem::Version
91
- version: '2.0'
92
- - - ">="
93
- - !ruby/object:Gem::Version
94
- version: 2.0.0
95
75
  description: Proxima is a gem the provides models for use with REST endpoints. These
96
76
  models are based upon the Active Model interface
97
77
  email:
@@ -103,10 +83,13 @@ files:
103
83
  - lib/proxima.rb
104
84
  - lib/proxima/api.rb
105
85
  - lib/proxima/attributes.rb
86
+ - lib/proxima/http_methods.rb
106
87
  - lib/proxima/model.rb
107
88
  - lib/proxima/paths.rb
108
- - lib/proxima/rest.rb
89
+ - lib/proxima/request.rb
90
+ - lib/proxima/response.rb
109
91
  - lib/proxima/serialization.rb
92
+ - lib/proxima/types.rb
110
93
  - lib/proxima/validation.rb
111
94
  - lib/proxima/version.rb
112
95
  - lib/proxima/watch.rb