emmy-http 0.1.11 → 0.2

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: 8dfa617e0358d6ec58af123a7d50763b62dcdff5
4
- data.tar.gz: 8e7a0ce2e5985c4ab369a9457769c6c299065340
3
+ metadata.gz: 61c0e927e8a3819fe647cd8c6837495f9c0ed144
4
+ data.tar.gz: 5b767d81b61759ed37b9b97b13a73474e4d4cd62
5
5
  SHA512:
6
- metadata.gz: e9a8d1b8fe86fcf0eba9895d960a7518c9f5e47fed3dc0963e54e15535fb45703cc9e1d06e743020a51ded69eb21975972de04e7ece82604b6db9cf3141ea174
7
- data.tar.gz: c0b0013eb58b9e6375e69f962faaf299e7cb2b3129118681e93414ddcc88e3708d92e44cdbe5ca70275cf3659197174f621226e5c7091e44af8c5ce95651a32b
6
+ metadata.gz: 020f95e4484f46c9a4b6c4fea44333904c0b70d8a5b76ed9498aa8a7d2105201ed34969dd2e8cdd101d46a324897c13c787772da14e03bc29783ff68f7a2d5e5
7
+ data.tar.gz: 32a06da169fbfb4d99bbb38093623f110019e138aa180b19a4704be31dea13cc1abbf54e6c01f453b391922ed325bea1fb158b3a2ebab8ffbd1558b3d15143be
@@ -12,7 +12,13 @@ module EmmyHttp
12
12
 
13
13
  module InstanceMethods
14
14
  def request
15
- @request ||= EmmyHttp::Request.new
15
+ @request ||= self.class.superclass.respond_to?(:instance) ?
16
+ self.class.superclass.instance.request.copy :
17
+ EmmyHttp::Request.new
18
+ end
19
+
20
+ def api
21
+ @api ||= {}
16
22
  end
17
23
  end
18
24
 
@@ -45,16 +51,23 @@ module EmmyHttp
45
51
  instance.request.raise_error = flag
46
52
  end
47
53
 
54
+ def api
55
+ instance.api
56
+ end
57
+
48
58
  alias header headers
49
59
 
50
60
  EmmyHttp::HTTP_METHODS.each do |name|
51
61
  define_method name do |path, as: nil|
52
- cdef as.to_s do |*a|
53
- instance.var(as.to_s, request(path: path))
54
- end
62
+ instance.api[as] = {path: path}
63
+ if as
64
+ cdef as.to_s do |a=nil|
65
+ instance.var(as.to_s, request(path: path).tap { |r| r.update_attributes(a) if a })
66
+ end
55
67
 
56
- cdef "#{as}!" do |*a, &b|
57
- send(as, *a, &b)
68
+ cdef "#{as}!" do |*a, &b|
69
+ send(as, *a, &b)
70
+ end
58
71
  end
59
72
  end
60
73
  end
@@ -11,9 +11,14 @@ module EmmyHttp
11
11
  serialize: ->(v) { v.is_a?(Addressable::Template) ? v.pattern : v.to_s }
12
12
  dictionary :headers
13
13
 
14
- attribute :path # replace url.path
15
- attribute :query # replace url.query
16
- dictionary :params # params for url template
14
+ attribute :path, # replace url.path
15
+ writer: ->(v) { v.is_a?(String) ? Addressable::Template.new(v) : v },
16
+ serialize: ->(v) { v.is_a?(Addressable::Template) ? v.pattern : (v ? v.to_s : v) }
17
+
18
+ attribute :query # replace url.query
19
+ attribute :user # replace url.user
20
+ attribute :password # replace url.password
21
+ dictionary :params # params for url template
17
22
 
18
23
  # POST, PUT
19
24
  attribute :body # string, json hash
@@ -36,7 +41,11 @@ module EmmyHttp
36
41
  serialize: ->(value) { value.to_s }
37
42
 
38
43
  def operation
39
- @operation ||= EmmyHttp::Operation.new(self, adapter.new)
44
+ @operation ||= new_operation
45
+ end
46
+
47
+ def new_operation
48
+ EmmyHttp::Operation.new(self, adapter.new)
40
49
  end
41
50
 
42
51
  alias op operation
@@ -53,6 +62,15 @@ module EmmyHttp
53
62
  end
54
63
  end
55
64
 
65
+ def real_path
66
+ return nil unless path
67
+ if path.is_a?(Addressable::Template)
68
+ path.expand(params)
69
+ else
70
+ Addressable::URI.parse(path.to_s)
71
+ end
72
+ end
73
+
56
74
  EmmyHttp::HTTP_METHODS.each do |name|
57
75
  self.def name do |a={}|
58
76
  update_attributes(a.is_a?(String) ? {url: a} : a)
@@ -57,17 +57,21 @@ module EmmyHttp
57
57
 
58
58
  def content
59
59
  @content ||= case content_type
60
- when 'application/json'
61
- begin
62
- JSON.parse(body)
63
- rescue JSON::ParserError => e
64
- raise ParserError, e.to_s
65
- end
60
+ when 'application/json' then json
66
61
  else
67
62
  nil
68
63
  end
69
64
  end
70
65
 
66
+ def json(options={})
67
+ raise RequestError, 'Wrong Content-Type' unless content_type == 'application/json'
68
+ begin
69
+ JSON.parse(body, options)
70
+ rescue JSON::ParserError => e
71
+ raise ParserError, e.to_s
72
+ end
73
+ end
74
+
71
75
  def body?
72
76
  !body.empty?
73
77
  end
@@ -1,3 +1,3 @@
1
1
  module EmmyHttp
2
- VERSION = "0.1.11"
2
+ VERSION = "0.2"
3
3
  end
@@ -18,11 +18,17 @@ describe EmmyHttp::Model do
18
18
  post '/post', as: :post_request
19
19
  end
20
20
 
21
+ class HTTPSBin < HTTPBin
22
+ url "https://httpbin.org"
23
+
24
+ get '/get', as: :get_request
25
+ end
26
+
21
27
  around do |example|
22
28
  EmmyMachine.run_block &example
23
29
  end
24
30
 
25
- it "have requests from model" do
31
+ it "has requests from model" do
26
32
  res = {
27
33
  get_response: HTTPBin.get_request!,
28
34
  post_response: HTTPBin.post_request!(form: {a: 5, b: 6})
@@ -31,4 +37,12 @@ describe EmmyHttp::Model do
31
37
  expect(res[:get_response].status).to be(200)
32
38
  expect(res[:post_response].status).to be(200)
33
39
  end
40
+
41
+ it "inherits models" do
42
+ http_req = HTTPBin.get_request
43
+ https_req = HTTPSBin.get_request
44
+ https_req.sync
45
+ expect(http_req.real_url.to_s).to eq('http://httpbin.org')
46
+ expect(https_req.real_url.to_s).to eq('https://httpbin.org')
47
+ end
34
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emmy-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - inre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-25 00:00:00.000000000 Z
11
+ date: 2015-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: event_object