Class: CertificateFactory::Factory

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/certificate-factory/factory.rb

Direct Known Subclasses

CSVFactory

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @url = options[:feed]
  @limit = options[:limit]
  @campaign = options[:campaign]
  @count = 0
  @response = self.class.get(@url)
  @logger = options[:logger]
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/certificate-factory/factory.rb', line 7

def count
  @count
end

#responseObject (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

#buildObject



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

#eachObject



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_itemsObject



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_pageObject



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


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_pageObject



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

Returns:

  • (Boolean)


36
37
38
# File 'lib/certificate-factory/factory.rb', line 36

def over_limit?
  @limit && @limit <= @count
end