koa-utils 0.0.9 → 0.0.10

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: 44d367611792021c6ccb24e9481033a15c957fa3
4
- data.tar.gz: b098b35bf9aadf96449c0f80c109e236865f7d98
3
+ metadata.gz: def7b9d5a92875c1d4aeec44696c474005e9651e
4
+ data.tar.gz: 7f5186e6fbffe96a55fa0e85ccbb8f9d44e2c476
5
5
  SHA512:
6
- metadata.gz: 0224a865f0e7a23c517f4dea81ccb6c9e9a6e66b640164a15a842b044d2024dfd4bf42242e3b2acb643c456b349d908aab132be7413fe9b1f88c3c3dc1c7265b
7
- data.tar.gz: afa0778b449961f71b50007278199b99e637ed374cb2ce64b30108f6f3dda047213a049b3d58e073344f8aa4d145b545e25dc31c8fe88e3f3f66d551a86eb665
6
+ metadata.gz: 1d3f1645d5364269097bab9ab2c43e36092e7c2ce86756c78785826b8de19673e2cf5186d2eb92c27fd4007af6f2d1789c4535ddddf047700115965a51fe4ca4
7
+ data.tar.gz: e23fdfaf0a66757744d1d880ac47f58affeca5ef789247ebc7250fdea506ff1893fbfaa25a4b56ce8c9313da2231fa6dbf63d32dc812e395a08d2b612d11c7db
data/lib/koa-utils/kik.rb CHANGED
@@ -1,3 +1,7 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'timeout'
4
+
1
5
  module KOAUtils::Kik
2
6
  def self.purchase(signedData, username, host)
3
7
  KOAUtils::Logger.measure_block("kikpurchase") do
@@ -0,0 +1,106 @@
1
+ require 'cgi'
2
+ require 'delegate'
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'timeout'
6
+ require 'uri'
7
+
8
+ module KOAUtils
9
+ class TimeoutResponse
10
+
11
+ def successful?
12
+ false
13
+ end
14
+
15
+ def code
16
+ '0'
17
+ end
18
+
19
+ def self.body_permitted?
20
+ false
21
+ end
22
+
23
+ def body
24
+ nil
25
+ end
26
+ end
27
+
28
+ class ResponseDecorator < SimpleDelegator
29
+ def successful?
30
+ code.to_i / 100 == 2
31
+ end
32
+ end
33
+
34
+ module Request
35
+ def self.make(opts)
36
+ opts[:tries] ||= 1
37
+ opts[:timeout] ||= 10
38
+
39
+ opts[:url] = build_url(opts)
40
+ opts[:body] = build_body(opts)
41
+
42
+ execute(opts)
43
+ end
44
+
45
+ private
46
+
47
+ def self.execute(opts)
48
+ opts[:tries].times do
49
+
50
+ begin
51
+ ::Timeout::timeout(opts[:timeout]) do
52
+ return ResponseDecorator.new(net_request(opts))
53
+ end
54
+ rescue ::Timeout::Error
55
+ end
56
+
57
+ end
58
+ KOAUtils::TimeoutResponse.new
59
+ end
60
+
61
+ def self.net_request(opts)
62
+ uri = ::URI.parse(opts[:url])
63
+ http = ::Net::HTTP.new(uri.host, uri.port)
64
+ http.use_ssl = uri.scheme == "https"
65
+ request = request_factory(opts[:type]).new(uri.request_uri)
66
+
67
+ request.basic_auth uri.user, uri.password if uri.user
68
+ request.basic_auth opts[:auth][:user], opts[:auth][:pass] if opts[:auth]
69
+
70
+ if opts[:type] == :post || opts[:type] == :put
71
+ request.add_field('Content-Type', 'application/json')
72
+ request.add_field('Content-Length', opts[:body].size)
73
+ request.body = opts[:body]
74
+ end
75
+
76
+ http.request(request)
77
+ end
78
+
79
+ def self.request_factory(type)
80
+ return ::Net::HTTP::Post if type == :post
81
+ return ::Net::HTTP::Put if type == :put
82
+ return ::Net::HTTP::Delete if type == :delete
83
+ return ::Net::HTTP::Get if type == :get
84
+ end
85
+
86
+ def self.build_body(opts)
87
+ body = ""
88
+ if opts[:type] == :post || opts[:type] == :put
89
+ body = ::JSON.dump(opts[:data]) if opts[:data]
90
+ end
91
+ body
92
+ end
93
+
94
+ def self.build_url(opts)
95
+ url = opts[:url]
96
+ if opts[:type] == :get || opts[:type] == :delete
97
+ url += "?"+hash_to_query(opts[:data]) if opts[:data]
98
+ end
99
+ url
100
+ end
101
+
102
+ def self.hash_to_query(hash)
103
+ hash.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join("&")
104
+ end
105
+ end
106
+ end
data/lib/koa-utils.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  module KOAUtils
2
2
  end
3
3
 
4
+ require 'koa-utils/health-check'
4
5
  require 'koa-utils/logger'
5
6
  require 'koa-utils/kik'
6
7
  require 'koa-utils/rack-request-timer'
7
- require 'koa-utils/health-check'
8
- require 'koa-utils/sequel-logger'
8
+ require 'koa-utils/request'
9
+ require 'koa-utils/sequel-logger'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koa-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Rykwalder
@@ -20,6 +20,7 @@ files:
20
20
  - lib/koa-utils/kik.rb
21
21
  - lib/koa-utils/logger.rb
22
22
  - lib/koa-utils/rack-request-timer.rb
23
+ - lib/koa-utils/request.rb
23
24
  - lib/koa-utils/sequel-logger.rb
24
25
  - lib/koa-utils.rb
25
26
  homepage: http://github.com/albumatic