Class: WebFixtures::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/web_fixtures/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, options = {}, input = STDIN, output = STDOUT) ⇒ Request

Returns a new instance of Request.



13
14
15
16
17
18
19
20
# File 'lib/web_fixtures/request.rb', line 13

def initialize(method, uri, options = {}, input = STDIN, output = STDOUT)
  @method = method
  @uri = uri
  @options = options

  @input = input
  @output = output
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



8
9
10
# File 'lib/web_fixtures/request.rb', line 8

def input
  @input
end

#methodObject

Returns the value of attribute method.



4
5
6
# File 'lib/web_fixtures/request.rb', line 4

def method
  @method
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/web_fixtures/request.rb', line 6

def options
  @options
end

#outputObject

Returns the value of attribute output.



9
10
11
# File 'lib/web_fixtures/request.rb', line 9

def output
  @output
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/web_fixtures/request.rb', line 11

def password
  @password
end

#uriObject

Returns the value of attribute uri.



5
6
7
# File 'lib/web_fixtures/request.rb', line 5

def uri
  @uri
end

#usernameObject

Returns the value of attribute username.



11
12
13
# File 'lib/web_fixtures/request.rb', line 11

def username
  @username
end

Instance Method Details

#collect_passwordObject



64
65
66
67
68
69
70
# File 'lib/web_fixtures/request.rb', line 64

def collect_password
  return password if password
  return nil unless options[:authenticate]

  output.print "Password: "
  @password = input.gets.chomp
end

#collect_usernameObject



56
57
58
59
60
61
62
# File 'lib/web_fixtures/request.rb', line 56

def collect_username
  return username if username
  return nil unless options[:authenticate]

  output.print "Username: "
  @username = input.gets.chomp
end

#curl_commandObject



26
27
28
29
30
31
32
33
34
# File 'lib/web_fixtures/request.rb', line 26

def curl_command
  command = "curl -s"
  command << " -i" if options[:include_headers]
  command << " -u #{collect_username}:#{collect_password}" if options[:authenticate]
  command << " -X #{method.to_s.upcase}" if method != :get
  command << " -o \"#{output_file}\""
  command << " \"#{uri}\""
  command
end

#filenameObject



47
48
49
50
# File 'lib/web_fixtures/request.rb', line 47

def filename
  title = uri_components[3..-1].join("_")
  (title.empty? ? "root" : title) + ".txt"
end

#output_fileObject



52
53
54
# File 'lib/web_fixtures/request.rb', line 52

def output_file
  File.join(storage_path, filename)
end

#storage_pathObject



40
41
42
43
44
45
# File 'lib/web_fixtures/request.rb', line 40

def storage_path
  root = options[:root_path] || "."
  directory = uri_components[2]

  File.join(root, directory)
end

#store!Object



22
23
24
# File 'lib/web_fixtures/request.rb', line 22

def store!
  `mkdir -p \"#{storage_path}\" && #{curl_command}`
end

#uri_componentsObject



36
37
38
# File 'lib/web_fixtures/request.rb', line 36

def uri_components
  @uri_components ||= uri.split('/')
end