Module: Foobara::HttpApiCommand::Concerns::Url::ClassMethods

Defined in:
foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb

Instance Method Summary collapse

Instance Method Details

#base_url(url = nil, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb', line 40

def base_url(url = nil, &block)
  if block_given?
    unless url.nil?
      # :nocov:
      raise ArgumentError, "Cannot specify both url and block"
      # :nocov:
    end

    self.foobara_base_url_block = block
  elsif url
    self.foobara_base_url = url
  else
    # :nocov:
    raise ArgumentError, "No base url specified"
    # :nocov:
  end
end

#compute_api_url(command) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb', line 114

def compute_api_url(command)
  return @compute_api_url if @compute_api_url

  url = if foobara_url
          foobara_url
        elsif foobara_url_block
          command.instance_eval(&foobara_url_block)
        else
          path = if foobara_path
                   foobara_path
                 elsif foobara_path_block
                   command.instance_eval(&foobara_path_block)
                 end

          base = if foobara_base_url
                   foobara_base_url
                 elsif foobara_base_url_block
                   command.instance_eval(&foobara_base_url_block)
                 else
                   # :nocov:
                   raise "Not able to determine the api url. " \
                         "Did you remember to call .url or .path and .base_url?"
                   # :nocov:
                 end

          "#{base}#{path}"
        end

  unless foobara_base_url_block || foobara_path_block || foobara_url_block
    @compute_api_url = url
  end

  url
end

#compute_http(command) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb', line 149

def compute_http(command)
  # unclear why, but, seems like if we're doing SSL we have to be single-use, hmmm...
  return @net_http if @net_http && !@net_http.use_ssl?

  uri = URI(command.api_url)
  computed_http = Net::HTTP.new(uri.host, uri.port).tap do |http|
    http.use_ssl = uri.scheme == "https"
    if http_timeout
      http.read_timeout = http_timeout
    end
  end

  unless foobara_base_url_block || foobara_path_block || foobara_url_block
    @net_http = computed_http
  end

  computed_http
end

#compute_uri_object(command) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb', line 102

def compute_uri_object(command)
  return @compute_uri_object if @compute_uri_object

  uri = URI(compute_api_url(command))

  unless foobara_base_url_block || foobara_path_block || foobara_url_block
    @compute_uri_object = uri
  end

  uri
end

#http_method(method = nil) ⇒ Object



32
33
34
35
36
37
38
# File 'foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb', line 32

def http_method(method = nil)
  if method
    self.foobara_http_method = method
  else
    foobara_http_method || :get
  end
end

#http_timeout(timeout = nil) ⇒ Object



94
95
96
97
98
99
100
# File 'foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb', line 94

def http_timeout(timeout = nil)
  if timeout
    self.foobara_http_timeout = timeout
  else
    foobara_http_timeout
  end
end

#path(path = nil, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb', line 58

def path(path = nil, &block)
  if block_given?
    unless path.nil?
      # :nocov:
      raise ArgumentError, "Cannot specify both path and block"
      # :nocov:
    end

    self.foobara_path_block = block
  elsif path
    self.foobara_path = path
  else
    # :nocov:
    raise ArgumentError, "No path specified"
    # :nocov:
  end
end

#url(uri = nil, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb', line 76

def url(uri = nil, &block)
  if block_given?
    unless uri.nil?
      # :nocov:
      raise ArgumentError, "Cannot specify both uri and block"
      # :nocov:
    end

    self.foobara_url_block = block
  elsif uri
    self.foobara_url = uri
  else
    # :nocov:
    raise ArgumentError, "Must give either a url or a block that returns  url"
    # :nocov:
  end
end