emmy-http 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db44189442e0a565ce17f51583a2bd6c9e590bab
4
+ data.tar.gz: 3d4d37199263fa3acc5e7192061077cb5bc5fd60
5
+ SHA512:
6
+ metadata.gz: 51ebd83234a5928d4bf406feab270487c34c38a39c79e0640ea0bb89c05fca91ab24e0869a3842e15c7735da58db0ef2ee1b0c953b38fc2c2b451da5903de285
7
+ data.tar.gz: 5c7f1b066fb51714cc5ba4f7a5f24b7e96b23d3ffdc283120421a9e20434d5ed3e5cf9d8feac3111ca62b4cb6992185833236a5344c600d821ead166a063c509
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in emmy-http.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 che
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # EmmyHttp
2
+
3
+ Don't use directly use emmy gem instead
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/emmy-http.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'emmy_http/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "emmy-http"
8
+ spec.version = EmmyHttp::VERSION
9
+ spec.authors = ["che"]
10
+ spec.email = ["max@kupibilet.ru"]
11
+ spec.summary = %q{Emmy Http}
12
+ #spec.description = %q{TODO: Write a longer description. Optional.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "emmy-machine", "~> 0.1"
22
+ spec.add_dependency "fibre", "~> 0.9.1"
23
+ spec.add_dependency "model_pack", "~> 0.9"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3"
28
+ end
@@ -0,0 +1,11 @@
1
+ module EmmyHttp
2
+ class Adapter
3
+ attr_accessor :delegate
4
+
5
+ %w(status headers body setup to_a).each do |method|
6
+ define_method method do
7
+ raise 'method not implemented'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,73 @@
1
+ module EmmyHttp
2
+ class Operation
3
+ using EventObject
4
+
5
+ attr_reader :request
6
+ attr_reader :response
7
+ attr_reader :adapter
8
+ attr_reader :connection
9
+
10
+ events :init, :head, :success, :error
11
+
12
+ def initialize(request, adapter)
13
+ @request = request
14
+ @adapter = adapter
15
+ @adapter.delegate = self
16
+ @response = Response.new
17
+ setup
18
+ end
19
+
20
+ def connect
21
+ @connection ||= EmmyMachine.connect(*self)
22
+ end
23
+
24
+ def sync
25
+ Fiber.sync do |fiber|
26
+ # create connection
27
+ connect
28
+
29
+ on :success do |operation, conn|
30
+ fiber.resume operation.response
31
+ end
32
+
33
+ on :error do |error, operation, conn|
34
+ fiber.leave ConnectionError, error.to_s
35
+ end
36
+ end
37
+ end
38
+
39
+ def to_a
40
+ adapter.to_a
41
+ end
42
+
43
+ def serializable_hash
44
+ {
45
+ request: request && request.serializable_hash,
46
+ response: response && response.serializable_hash
47
+ }
48
+ end
49
+
50
+ private
51
+
52
+ def setup
53
+ adapter.setup
54
+
55
+ on :init do |connection|
56
+ @connection = connection
57
+ end
58
+
59
+ on :head do |operation, conn|
60
+ response.headers = operation.adapter.headers
61
+ end
62
+
63
+ on :success do |operation, conn|
64
+ response.status = operation.adapter.status
65
+ response.body = operation.adapter.body
66
+ end
67
+
68
+ on :error do |error, operation, conn|
69
+ response.status = operation.adapter.status
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,9 @@
1
+ module EmmyHttp
2
+ class SSL
3
+ include ModelPack::Document
4
+
5
+ attribute :private_key_file
6
+ attribute :cert_chain_file
7
+ attribute :verify_peer, predicate: true
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module EmmyHttp
2
+ class Timeouts
3
+ include ModelPack::Document
4
+
5
+ attribute :connect, default: 10
6
+ attribute :inactivity, default: 30
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ module EmmyHttp
2
+ class Request
3
+ include ModelPack::Document
4
+
5
+ attribute :method, default: "get"
6
+ attribute :url, writer: ->(url) { url.is_a?(URI) ? url : URI.parse(url) }
7
+ dictionary :headers
8
+ attribute :body, default: ""
9
+ attribute :connect_timeout, default: 5
10
+ attribute :inactivity_timeout, default: 30
11
+ object :timeouts, class_name: Timeouts
12
+ object :ssl, class_name: SSL
13
+
14
+ # FIXME: move to model_pack
15
+ def ssl?
16
+ !!ssl
17
+ end
18
+
19
+ def timeouts
20
+ @timeouts ||= Timeouts.new
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module EmmyHttp
2
+ class Response
3
+ include ModelPack::Document
4
+
5
+ attribute :status
6
+ dictionary :headers
7
+ attribute :body
8
+
9
+ def to_a
10
+ [status, headers, body]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module EmmyHttp
2
+ VERSION = "0.1.0"
3
+ end
data/lib/emmy_http.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "model_pack"
2
+ require "emmy_http/version"
3
+
4
+ module EmmyHttp
5
+ class ConnectionError < StandardError; end
6
+
7
+ autoload :Adapter, "emmy_http/adapter"
8
+ autoload :Timeouts, "emmy_http/request/timeouts"
9
+ autoload :SSL, "emmy_http/request/ssl"
10
+ autoload :Request, "emmy_http/request"
11
+ autoload :Operation, "emmy_http/operation"
12
+ autoload :Response, "emmy_http/response"
13
+ end
@@ -0,0 +1,78 @@
1
+ require "spec_helper"
2
+
3
+ describe EmmyHttp do
4
+ Connection = EmmyMachine::Connection
5
+
6
+ module FakeEventMachine
7
+ extend EmmyMachine::ClassMethods
8
+
9
+ def connect(adapter, init)
10
+ conn = Class.new
11
+ conn.extend Connection
12
+ init.call
13
+ adapter.connected(conn)
14
+ conn
15
+ end
16
+
17
+ extend self
18
+ end
19
+
20
+ class FakeAdapter < EmmyHttp::Adapter
21
+ using EventObject
22
+
23
+ def status
24
+ 200
25
+ end
26
+
27
+ def headers
28
+ {'Content-Type' => 'text/html'}
29
+ end
30
+
31
+ def body
32
+ "OK"
33
+ end
34
+
35
+ def setup
36
+ end
37
+
38
+ def initialize_connection
39
+ self.delegate.init! delegate
40
+ end
41
+
42
+ def connected(conn)
43
+ # replace request with timer
44
+ EventMachine.add_timer(0) do
45
+ self.delegate.head!(delegate, conn)
46
+ self.delegate.success!(delegate, conn)
47
+ end
48
+ end
49
+
50
+ def to_a
51
+ [self, method(:initialize_connection)]
52
+ end
53
+ end
54
+
55
+ before do
56
+ stub_const("EmmyMachine", FakeEventMachine)
57
+ end
58
+
59
+ around do |example|
60
+ EventMachine.run do
61
+ EmmyMachine.fiber_block &example
62
+ end
63
+ end
64
+
65
+ it "should do http request with fake adapter" do
66
+ request = EmmyHttp::Request.new(
67
+ method: 'get',
68
+ url: 'http://google.com'
69
+ )
70
+ operation = EmmyHttp::Operation.new(request, FakeAdapter.new)
71
+ response = operation.sync
72
+ EventMachine.stop
73
+
74
+ expect(response.status).to be 200
75
+ expect(response.headers).to include("Content-Type")
76
+ expect(response.body).to eq "OK"
77
+ end
78
+ end
@@ -0,0 +1,10 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require File.expand_path("./lib/emmy_http")
5
+ require "emmy_machine"
6
+ require "fibre"
7
+
8
+ RSpec.configure do |config|
9
+ config.color = true
10
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: emmy-http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - che
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: emmy-machine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fibre
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: model_pack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3'
97
+ description:
98
+ email:
99
+ - max@kupibilet.ru
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - emmy-http.gemspec
110
+ - lib/emmy_http.rb
111
+ - lib/emmy_http/adapter.rb
112
+ - lib/emmy_http/operation.rb
113
+ - lib/emmy_http/request.rb
114
+ - lib/emmy_http/request/ssl.rb
115
+ - lib/emmy_http/request/timeouts.rb
116
+ - lib/emmy_http/response.rb
117
+ - lib/emmy_http/version.rb
118
+ - spec/emmy_http_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage: ''
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Emmy Http
144
+ test_files:
145
+ - spec/emmy_http_spec.rb
146
+ - spec/spec_helper.rb