rhcf-utils 0.0.1 → 0.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: 6d1a0ea6f99f15f7e81b21291239462daf312138
4
- data.tar.gz: 965d1184ecf122ebd6f1202bdb10de4aa4c08ecf
3
+ metadata.gz: 1bf241e04517574674c112b8ae6e7112c754f795
4
+ data.tar.gz: f86503515dfd584020a03e45e1117cced9d73e55
5
5
  SHA512:
6
- metadata.gz: 18cb8e28818558e63942cdd47faa4faec1b5d45641535235ce63adad59a31148ad497ad349a1aa92930956e65fb43a5d9738c8f61e5f9aab84ca50b46db39d60
7
- data.tar.gz: d32f42f65b794238cbd6234e72a66896c80b521902cd5e21d81008975af5e2a885c09a31fac151c4c8146fe221ac655fed3ed5839cf6940f8ef467d0133d28bd
6
+ metadata.gz: 0342944c6a84423d3ac5393db024a4acdd5c39e647577fb792c302c8c705dcee2b1f3a26fee1954bfdb3c3d93c1e8df8a9137aa11bfd23ba726a088f2bd0b076
7
+ data.tar.gz: 145efcdaf360f62ee412272e72bf9cc7fdccdeaac7a6efbadde7fb27e296976608fa671a48d63f8514d2f03b25120cb9c920b31106094fe51fba4158aa682eda
data/.gitignore CHANGED
@@ -1,5 +1,7 @@
1
1
  *.gem
2
2
  *.rbc
3
+ .*swp
4
+ .*swo
3
5
  .bundle
4
6
  .config
5
7
  .yardoc
@@ -19,15 +19,17 @@ module Rhcf
19
19
  return outfile if self.class.hit_fname?(outfile, @ttl) # here goes the cache
20
20
  download!(url, outfile)
21
21
  rescue
22
- File.unkink(outfile) if File.exist?(outfile)
22
+ if File.exist?(outfile)
23
+ File.unlink(outfile)
24
+ end
23
25
  raise
24
26
  end
25
27
 
26
28
 
27
29
  def download!(url, outfile)
28
30
  mkdir_p(File.dirname(outfile))
29
- File.open(outfile, 'w') do |fd|
30
- open(url, "r") do |down|
31
+ File.open(outfile, 'wb') do |fd|
32
+ open(url, "rb") do |down|
31
33
  fd.write(down.read)
32
34
  end
33
35
  end
@@ -0,0 +1,52 @@
1
+ require 'resolv'
2
+ require 'stringio'
3
+ require 'net/smtp'
4
+ module Net
5
+ class SMTP
6
+ def socket
7
+ @socket
8
+ end
9
+
10
+ def bind_at(ip)
11
+ @bind_at = ip
12
+ end
13
+
14
+ def tcp_socket(address, port)
15
+ in_addr = Socket.pack_sockaddr_in(0, @bind_at) if @bind_at
16
+
17
+ out_addr = Socket.pack_sockaddr_in(port, address)
18
+ s = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
19
+ s.bind(in_addr) if @bind_at
20
+ s.connect(out_addr)
21
+ s
22
+ end
23
+ end
24
+ end
25
+
26
+
27
+ module Rhcf
28
+ module Utils
29
+ module Email
30
+ def self.get_mxs(domain)
31
+ Resolv::DNS.open do |dns|
32
+ dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect{|x| x.exchange.to_s}
33
+ end
34
+ end
35
+
36
+ def self.transmit(from, to, body, bind_interface, ehlo = 'localhost.localdomain')
37
+ domain = to.split('@').last
38
+
39
+ chat = StringIO.new
40
+ mx = get_mxs(domain).sample
41
+ smtp = Net::SMTP.new(mx, 25)
42
+ smtp.bind_at bind_interface
43
+ result = smtp.start(ehlo) do |smtp|
44
+ smtp.socket.debug_output = chat
45
+ smtp.send_message body, from, to
46
+ end
47
+
48
+ {status: result.status, string: result.string, chat: chat.string}
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,10 @@
1
+ require 'socket'
2
+ module Rhcf
3
+ module Utils
4
+ module Network
5
+ def self.my_interfaces
6
+ Socket::ip_address_list.select{|a| !a.ipv4_loopback? && a.ipv4_private? }.collect(&:ip_address)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  module Rhcf
2
2
  module Utils
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -4,7 +4,7 @@ require 'fileutils'
4
4
  require 'timecop'
5
5
 
6
6
  describe Rhcf::Utils::DownloadCache do
7
- let(:img_url){"http://www.sunnyvision.com/images/cloud_cdn_icon.jpg"}
7
+ let(:img_url){"http://veja2.abrilm.com.br/assets/images/2014/3/212814/imagens-do-dia-20131023-04-original.jpg"}
8
8
  let(:cache_of_30s){Rhcf::Utils::DownloadCache.new('cache_of_30s', 30)}
9
9
  let(:foo_cache_of_30s){Rhcf::Utils::DownloadCache.new('cache_of_30s', 30, '/tmp/foo')}
10
10
  it "should be a class" do
@@ -31,11 +31,11 @@ describe Rhcf::Utils::DownloadCache do
31
31
  describe "#hit?" do
32
32
  before(:each) do
33
33
  FileUtils.rm_rf "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054ca0b0d5f8"
34
- FileUtils.mkdir_p "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054a0b0d5f8"
35
- FileUtils.touch "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg"
34
+ FileUtils.mkdir_p "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e7fb5389b6646ebb89aaa5502b75a440"
35
+ FileUtils.touch "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg"
36
36
  end
37
37
  it "should return true if file downloaded and ttl is null" do
38
- cache_of_30s.filename_for(img_url).should == '/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg'
38
+ cache_of_30s.filename_for(img_url).should == '/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg'
39
39
  cache_of_30s.hit?(img_url).should be_true
40
40
  end
41
41
  it "should return true if file downloaded and newer then ttl" do
@@ -47,7 +47,7 @@ describe Rhcf::Utils::DownloadCache do
47
47
  end
48
48
  end
49
49
  it "should return false if doesn't exist" do
50
- FileUtils.rm "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg"
50
+ FileUtils.rm "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg"
51
51
  cache_of_30s.hit?(img_url).should be_false
52
52
  end
53
53
 
@@ -56,7 +56,7 @@ describe Rhcf::Utils::DownloadCache do
56
56
 
57
57
  describe "#filename_for" do
58
58
  it "should return a path with cache id , file name hash and file hash component" do
59
- subject.filename_for(img_url).should == '/tmp/Rhcf::Utils::DownloadCache/default/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg'
59
+ subject.filename_for(img_url).should == '/tmp/Rhcf::Utils::DownloadCache/default/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg'
60
60
  end
61
61
  end
62
62
 
@@ -77,8 +77,8 @@ describe Rhcf::Utils::DownloadCache do
77
77
  end
78
78
 
79
79
  describe "#get" do
80
- let(:expected_path){ '/tmp/Rhcf::Utils::DownloadCache/default/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg' }
81
- let(:expected_30s_path){ '/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg' }
80
+ let(:expected_path){ '/tmp/Rhcf::Utils::DownloadCache/default/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg' }
81
+ let(:expected_30s_path){ '/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg' }
82
82
  before(:each) do
83
83
  File.unlink(expected_path) rescue nil
84
84
  end
@@ -86,6 +86,7 @@ describe Rhcf::Utils::DownloadCache do
86
86
  it "should download when not exist"do
87
87
  subject.get(img_url).should == expected_path
88
88
  File.exist?(expected_path).should be_true
89
+ expect(`file #{expected_path}`).to match_regex /JPEG/
89
90
  end
90
91
 
91
92
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhcf-utils
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
  - Romeu Fonseca
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-02 00:00:00.000000000 Z
11
+ date: 2014-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -138,9 +138,10 @@ files:
138
138
  - Rakefile
139
139
  - lib/rhcf/utils.rb
140
140
  - lib/rhcf/utils/download_cache.rb
141
+ - lib/rhcf/utils/email.rb
142
+ - lib/rhcf/utils/network.rb
141
143
  - lib/rhcf/utils/version.rb
142
144
  - rhcf-utils.gemspec
143
- - spec/lib/rhcf/utils/.download_cache_spec.rb.swp
144
145
  - spec/lib/rhcf/utils/download_cache_spec.rb
145
146
  - spec/spec_helper.rb
146
147
  homepage: ''
@@ -168,6 +169,5 @@ signing_key:
168
169
  specification_version: 4
169
170
  summary: Personal Ruby Utility Tools.
170
171
  test_files:
171
- - spec/lib/rhcf/utils/.download_cache_spec.rb.swp
172
172
  - spec/lib/rhcf/utils/download_cache_spec.rb
173
173
  - spec/spec_helper.rb