url2png-dc 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/lib/url2png/cache.rb CHANGED
@@ -2,8 +2,8 @@ module Url2PngDc
2
2
  # TODO Rename this, "Cache" sounds a bit misleading in this context
3
3
  #
4
4
  class Cache
5
- def initialize(json_payload)
6
- @payload = json_payload
5
+ def initialize(url_2json_url)
6
+ @url_2json_url = url_2json_url
7
7
  end
8
8
 
9
9
  def url
@@ -14,9 +14,18 @@ module Url2PngDc
14
14
  private
15
15
 
16
16
  def parse_out_cache_url_from_payload
17
- _json = JSON.parse(@payload)
17
+ return nil if (payload = request_to_url2png).nil?
18
+
19
+ _json = JSON.parse(payload)
18
20
  _png = _json['png']
19
21
  _png if _png && !_png.empty?
20
22
  end
23
+
24
+ def request_to_url2png
25
+ response = ::HTTParty.get(@url_2json_url)
26
+ response.body
27
+ rescue
28
+ nil
29
+ end
21
30
  end
22
31
  end
@@ -16,8 +16,7 @@ module Url2PngDc
16
16
  end
17
17
 
18
18
  def url_2cache(url, options = {})
19
- response = ::HTTParty.get(url_2json(url, options))
20
- _cache = Cache.new response.body
19
+ _cache = Cache.new url_2json(url, options)
21
20
  _cache.url || url_2png(url, options)
22
21
  end
23
22
 
data/test/cache_test.rb CHANGED
@@ -2,27 +2,44 @@ require 'test_helper'
2
2
 
3
3
  module Url2PngDc
4
4
  class TestCache < MiniTest::Unit::TestCase
5
+ def url_2json_url
6
+ Sandbox.new.url_2json "http://google.com"
7
+ end
8
+
9
+
5
10
  # This provides a solution to insert images properly for display on
6
11
  # Microsoft Outlook email clients
7
12
  #
8
13
  def test_url_to_return_cached_url
9
- cache = Cache.new json_payload
14
+ mock_my_httparty!
15
+
16
+ cache = Cache.new url_2json_url
10
17
  assert_match(/cache(-[\d]+)\.url2png\.com/, cache.url)
11
18
  end
12
19
 
13
20
  def test_url_returns_nil_if_png_key_is_empty_or_missing
14
- json = JSON.parse(json_payload)
21
+ cache = Cache.new url_2json_url
22
+ json = JSON.parse(json_payload)
23
+
15
24
  json['png'] = ''
16
- cache = Cache.new json.to_json
25
+ mock_my_httparty! json.to_json
17
26
  assert_nil cache.url
18
27
 
19
28
  json.delete('png')
20
- cache = Cache.new json.to_json
29
+ mock_my_httparty! json.to_json
21
30
  assert_nil cache.url
22
31
  end
23
32
 
24
- def test_test_url_returns_nil_if_request_to_url2png_is_not_successful
25
- flunk "We need to test this"
33
+ def test_test_url_returns_nil_if_request_to_url2png_raises_anything
34
+ # mocky
35
+ HTTParty.instance_eval do
36
+ def get(url)
37
+ raise StandardError
38
+ end
39
+ end
40
+
41
+ cache = Cache.new url_2json_url
42
+ assert_nil cache.url
26
43
  end
27
44
  end
28
45
  end
data/test/test_helper.rb CHANGED
@@ -15,8 +15,19 @@ def json_payload
15
15
  json
16
16
  end
17
17
 
18
+ def mock_my_httparty!(and_return_payload = json_payload)
19
+ # mocky
20
+ HTTParty.instance_eval do
21
+ (class << self; self; end).__send__ :define_method, :get do |url|
22
+ obj = Object.new
23
+ (class << obj; self; end).__send__ :define_method, :body do
24
+ and_return_payload
25
+ end
26
+ obj
27
+ end
28
+ end
29
+ end
18
30
 
19
31
  class Sandbox
20
32
  include Url2PngDc::UrlHelpers
21
33
  end
22
-
@@ -18,19 +18,6 @@ module Url2PngDc
18
18
  Digest::MD5.hexdigest(query_string+Url2PngDc.configuration.url2png_secret)
19
19
  end
20
20
 
21
- def mock_my_httparty!
22
- # mocky
23
- HTTParty.instance_eval do
24
- def get(url)
25
- obj = Object.new
26
- def obj.body
27
- json_payload
28
- end
29
- obj
30
- end
31
- end
32
- end
33
-
34
21
 
35
22
  def test_url_2png_generates_valid_url2png_url_for_png
36
23
  token = token(query_string_with_defaults)
data/url2png-dc.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "url2png-dc"
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Yung Hwa Kwon"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url2png-dc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
112
  version: '0'
113
113
  segments:
114
114
  - 0
115
- hash: 2435669331839851502
115
+ hash: -611436617416580097
116
116
  required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements: