Class: Helu::ProductInfoFetcher
- Inherits:
-
Object
- Object
- Helu::ProductInfoFetcher
- Defined in:
- lib/project/product_info_fetcher.rb
Constant Summary collapse
- @@mutex =
Mutex.new
- @@cache =
{}
Class Method Summary collapse
-
.call_synchronized(method, *args) ⇒ Object
make a sync call out of an async one.
- .clear_cache ⇒ Object
- .fetch(*products, &b) ⇒ Object
Instance Method Summary collapse
-
#initialize(*products, &b) ⇒ ProductInfoFetcher
constructor
A new instance of ProductInfoFetcher.
- #productsRequest(req, didReceiveResponse: resp) ⇒ Object
Constructor Details
#initialize(*products, &b) ⇒ ProductInfoFetcher
Returns a new instance of ProductInfoFetcher.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/project/product_info_fetcher.rb', line 36 def initialize(*products, &b) raise LocalJumpError, "block expected" if b.nil? @callback = b @products = products.flatten # all cached? skip the call... if (@@cache.keys & @products).sort == @products.sort h = @products.inject({}) { |m, prod| m[prod] = @@cache[prod]; m } @callback.call(h) else @sr = SKProductsRequest.alloc.initWithProductIdentifiers(@products) @sr.delegate = self @sr.start end self end |
Class Method Details
.call_synchronized(method, *args) ⇒ Object
make a sync call out of an async one
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/project/product_info_fetcher.rb', line 89 def call_synchronized(method, *args) finished = false result = nil send(method, *args) do |res| result = res finished = true end sleep 0.1 until finished result end |
.clear_cache ⇒ Object
109 110 111 |
# File 'lib/project/product_info_fetcher.rb', line 109 def clear_cache @@mutex.synchronize { @@cache = {} } end |
.fetch(*products, &b) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/project/product_info_fetcher.rb', line 100 def fetch(*products, &b) products.flatten! if b.nil? call_synchronized(:fetch, *products) else new(*products, &b) end end |
Instance Method Details
#productsRequest(req, didReceiveResponse: resp) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/project/product_info_fetcher.rb', line 54 def productsRequest(req, didReceiveResponse: resp) if resp.nil? @callback.call(nil) else h = resp.products.inject({}) { |m, prod| m.merge(sk_to_hash(prod)) } @@mutex.synchronize { @@cache.merge!(h) } @callback.call(h) end end |