Class: Scrapers::RubyTapas::DpdCart

Inherits:
Object
  • Object
show all
Defined in:
lib/scrapers/rubytapas/dpdcart.rb

Overview

DpdCart is a remote service gateway object (Gateway Pattern) that provides a connection to rubytapas.dpdcart.com where the RubyTapas episodes and download files are available, as well as the episode feed.

Constant Summary collapse

DPDCART_HOST_FORMAT =

The subscription name will be filled in depending on which subscription I’m downloading from. This is a stock sprintf-type fill in where you pass in the subscription parameter with a value, thusly:

DPDCART_HOST % {subscription: "rubytapas"}
"%{subscription}.dpdcart.com"
ENV_DPDCART_USER_FORMAT =
"%{subscription}_USER"
ENV_DPDCART_PASSWORD_FORMAT =
"%{subscription}_USER"
LOGIN_PATH =
'/subscriber/login'
FEED_PATH =
'/feed'
CONTENT_PATH =
"/subscriber/content"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user = nil, password = nil, subscription = 'rubytapas', options = {}) ⇒ DpdCart

Create a new instance of the DpdCart gateway.

email address. account. (e.g. ‘rubytapas’ or ‘elixirsips’)

If the user and password are empty, the information will be obtained in the following order:

  • reading the environment variables ‘<subscriptiion>_USER` and

‘<subscription>_PASSWORD`

Note that <subscription> will be the subscription passed in
above.
  • reading the user’s ‘$HOME/.netrc` file and pulling the

credentials that match the host name for the subscription account.

If no credentials can be found, it will raise an error: ‘NoCredentialsError`.

Parameters:

  • user (String) (defaults to: nil)
    • the DpdCart account name, typically an

  • password (String) (defaults to: nil)
    • password associated with the

  • subscription (String) (defaults to: 'rubytapas')
    • subscription name at DPD Cart



74
75
76
77
78
79
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 74

def initialize(user=nil, password=nil, subscription='rubytapas', options={})
  self.options = options
  self.subscription = subscription
  set_user_and_password(user, password)
  self.agent = Mechanize.new
end

Instance Attribute Details

#passwordObject

Password for dpdcart acount



39
40
41
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 39

def password
  @password
end

#subscriptionObject

Subscription at dbdcart



33
34
35
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 33

def subscription
  @subscription
end

#userObject

User name for dpdcart account



36
37
38
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 36

def user
  @user
end

Instance Method Details

#debugObject



44
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 44

def debug                ; @debug                ||= options[:debug]                                            ; end

#download!(file) ⇒ Object

Download the file from dpdcart



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 87

def download!(file)
  
  warn "DEBUG: downloading #{file}" if debug
  if dry_run
    warn "DEBUG: download skipped for dry run" if dry_run
    filename = file
    body = "no body"
  else
    page = agent.get(file)
    filename = page.filename
    body = page.body
  end
  [ filename, body ]
end

#dpdcart_hostObject



41
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 41

def dpdcart_host         ; @dpdcart_host         ||= DPDCART_HOST_FORMAT         % {subscription: subscription} ; end

#dry_runObject



45
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 45

def dry_run              ; @dry_run              ||= options[:dry_run]                                          ; end

#env_dpdcart_passwordObject



43
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 43

def env_dpdcart_password ; @env_dpdcart_password ||= ENV_DPDCART_PASSWORD_FORMAT % {subscription: subscription} ; end

#env_dpdcart_userObject



42
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 42

def env_dpdcart_user     ; @env_dpdcart_user     ||= ENV_DPDCART_PASSWORD_FORMAT % {subscription: subscription} ; end

#feed!Object

Retreive the episode feed from dpdcart



82
83
84
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 82

def feed!
  http_fetch(feed_url)
end

#feed_urlObject



46
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 46

def feed_url             ; @feed_url             ||= URI("https://#{dpdcart_host}#{FEED_PATH}")                 ; end

#login_urlObject



47
# File 'lib/scrapers/rubytapas/dpdcart.rb', line 47

def             ; @login_url            ||= URI("https://#{dpdcart_host}#{LOGIN_PATH}")                ; end