Module: Singly::Endpoint

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/singly/endpoint.rb', line 12

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/singly/endpoint.rb', line 11

def path
  @path
end

Instance Method Details

#fetch(opts = {}) ⇒ Object

The opts argument is for Typhoeus overrides



20
21
22
23
# File 'lib/singly/endpoint.rb', line 20

def fetch(opts={})
  validate
  Singly::Http.fetch(path, options.merge(opts))
end

#initialize(params = {}) ⇒ Object



14
15
16
17
# File 'lib/singly/endpoint.rb', line 14

def initialize(params={})
  init_path(params)
  init_options(params)
end

#urlObject

String representation of the composed endpoint. Parameter order is deterministic so it can be used as a key in the /multi endpoint.



37
38
39
40
41
42
43
44
45
# File 'lib/singly/endpoint.rb', line 37

def url
  validate
  query_string = options[:params].sort.inject([]) do |queries, param|
    queries << ("#{CGI.escape(param[0].to_s)}=#{CGI.escape(param[1].to_s)}")
  end.join("&")
  url = "#{Singly::Http.base_url}#{path}"
  url += "?#{query_string}" unless query_string.empty?
  url
end

#validateObject

Raises an error if any required parameters have not been supplied or if any path components are missing.



27
28
29
30
31
32
# File 'lib/singly/endpoint.rb', line 27

def validate
  Singly::Error.y_u_no?("has all route params: #{path}") { path.include? ":" }
  missing_options = self.class.required_params - options[:params].keys
  Singly::Error.y_u_no?("has required params :#{missing_options.join(', :')}") { missing_options.any? }
  true  
end