Class: Halchemy::BaseRequester

Inherits:
Object
  • Object
show all
Defined in:
lib/halchemy/requester.rb

Overview

The results of a Follower#to is a Requester. In the case of a GET for the home resource, the Requester is Read Only Otherwise it is a full Requester. Both requester types share much in common. This is defined in BaseRequester

Direct Known Subclasses

ReadOnlyRequester

Constant Summary collapse

LIST_STYLE_HANDLERS =
{
  "repeat_key" => ->(key, array) { array.map { |item| [key, URI.encode_www_form_component(item.to_s)] } },
  "bracket" => ->(key, array) { array.map { |item| ["#{key}[]", URI.encode_www_form_component(item.to_s)] } },
  "index" => lambda { |key, array|
    array.each_with_index.map do |item, index|
      ["#{key}[#{index}]", URI.encode_www_form_component(item.to_s)]
    end
  },
  "comma" => ->(key, array) { [[key, array.map { |item| URI.encode_www_form_component(item.to_s) }.join(",")]] },
  "pipe" => ->(key, array) { [[key, array.map { |item| URI.encode_www_form_component(item.to_s) }.join("|")]] }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api, target) ⇒ void

Parameters:



24
25
26
27
28
29
30
31
32
# File 'lib/halchemy/requester.rb', line 24

def initialize(api, target)
  @api = api
  @_data = nil
  @_headers = CICPHash.new
  @_template_values = {}
  @_parameters = {}

  process_target(target)
end

Instance Method Details

#request(method) ⇒ Object



64
65
66
67
# File 'lib/halchemy/requester.rb', line 64

def request(method)
  data = @_data.is_a?(Hash) ? @_data.to_json : @_data
  @api.request(method, url, @_headers, data)
end

#urlObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/halchemy/requester.rb', line 34

def url
  rtn = @_url

  if @_is_templated
    tpl = URITemplate.new(rtn)
    rtn = tpl.expand(@_template_values)
  end

  rtn = add_parameters_to_url rtn unless @_parameters.empty?
  rtn
end

#with_headers(headers) ⇒ Object

Parameters:

  • headers (Hash)


47
48
49
50
# File 'lib/halchemy/requester.rb', line 47

def with_headers(headers)
  @_headers.merge! headers
  self
end

#with_parameters(parameters) ⇒ Object

Parameters:

  • parameters (Hash)


59
60
61
62
# File 'lib/halchemy/requester.rb', line 59

def with_parameters(parameters)
  @_parameters.merge! parameters
  self
end

#with_template_values(values) ⇒ Object

Parameters:

  • values (Hash)


53
54
55
56
# File 'lib/halchemy/requester.rb', line 53

def with_template_values(values)
  @_template_values.merge! values
  self
end