Class: CertificateFactory::Factory
- Inherits:
-
Object
- Object
- CertificateFactory::Factory
- Includes:
- HTTParty
- Defined in:
- lib/certificate-factory/factory.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #build ⇒ Object
- #ckan_url(api_url) ⇒ Object
- #each ⇒ Object
- #feed_items ⇒ Object
- #fetch_next_page ⇒ Object
- #get_link(item) ⇒ Object
-
#initialize(options) ⇒ Factory
constructor
A new instance of Factory.
- #next_page ⇒ Object
- #over_limit? ⇒ Boolean
Constructor Details
#initialize(options) ⇒ Factory
Returns a new instance of Factory.
9 10 11 12 13 14 15 16 |
# File 'lib/certificate-factory/factory.rb', line 9 def initialize() @url = [:feed] @limit = [:limit] @campaign = [:campaign] @count = 0 @response = self.class.get(@url) @logger = [:logger] end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
7 8 9 |
# File 'lib/certificate-factory/factory.rb', line 7 def count @count end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/certificate-factory/factory.rb', line 7 def response @response end |
Instance Method Details
#build ⇒ Object
18 19 20 21 22 23 |
# File 'lib/certificate-factory/factory.rb', line 18 def build each do |item| url = get_link(item) yield Certificate.new(url, campaign: @campaign).generate end end |
#ckan_url(api_url) ⇒ Object
63 64 65 |
# File 'lib/certificate-factory/factory.rb', line 63 def ckan_url(api_url) CertificateFactory::API.new(api_url).ckan_url end |
#each ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/certificate-factory/factory.rb', line 25 def each loop do feed_items.each do |item| yield item @count += 1 return if over_limit? end return unless fetch_next_page end end |
#feed_items ⇒ Object
40 41 42 |
# File 'lib/certificate-factory/factory.rb', line 40 def feed_items [response["feed"]["entry"]].flatten # In case there is only one feed item end |
#fetch_next_page ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/certificate-factory/factory.rb', line 48 def fetch_next_page @url = next_page if @url @logger.debug "feed: #{@url}" if @logger @response = self.class.get(@url) else return false end end |
#get_link(item) ⇒ Object
58 59 60 61 |
# File 'lib/certificate-factory/factory.rb', line 58 def get_link(item) api_url = item["link"].find { |l| l["rel"] == "enclosure" }["href"] ckan_url(api_url) end |
#next_page ⇒ Object
44 45 46 |
# File 'lib/certificate-factory/factory.rb', line 44 def next_page response["feed"]["link"].find { |l| l["rel"] == "next" }["href"] rescue nil end |
#over_limit? ⇒ Boolean
36 37 38 |
# File 'lib/certificate-factory/factory.rb', line 36 def over_limit? @limit && @limit <= @count end |