Class: Scrapinghub::ApiMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/scrapinghub/api_method.rb

Instance Method Summary collapse

Constructor Details

#initialize(location, requires = [], format = :json) ⇒ ApiMethod

Returns a new instance of ApiMethod.



3
4
5
6
# File 'lib/scrapinghub/api_method.rb', line 3

def initialize(location, requires=[], format = :json)
  @location = "#{location}.#{format.to_s}"
  @requires = requires
end

Instance Method Details

#build(base_url, parameters = {}) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/scrapinghub/api_method.rb', line 8

def build(base_url, parameters = {})
  @parameters = parameters

  build_params!
  build_uri!(base_url)

  @uri
end

#build_params!Object



17
18
19
20
21
22
23
# File 'lib/scrapinghub/api_method.rb', line 17

def build_params!
  @requires.each do |required_parameter|
    unless @parameters[required_parameter]
      raise ArgumentError.new "#{required_parameter} is required to access #{@location}"
    end
  end
end

#build_uri!(base_url) ⇒ Object



25
26
27
28
# File 'lib/scrapinghub/api_method.rb', line 25

def build_uri!(base_url)
  @uri = URI( File.join(base_url, @location) )
  @uri.query = URI.encode_www_form(@parameters)
end