Class: Eson::HTTP::Request

Inherits:
Request
  • Object
show all
Defined in:
lib/eson/http/request.rb

Overview

This is the HTTP specific subclass of Eson::Request that encodes all HTTP interactions.

Defined Under Namespace

Classes: GetHackMiddleware

Instance Method Summary collapse

Instance Method Details

#base_resourceFaraday::Connection

Constructs the base resource to send requests to.

Returns:

  • (Faraday::Connection)

    the connection



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/eson/http/request.rb', line 27

def base_resource
  conn = Faraday.new(:url => client.node) do |builder|
    builder.use Faraday::Response::Logger, client.logger
    builder.use GetHackMiddleware

    builder.adapter :net_http_persistent
  end

  conn.basic_auth(*client.auth) if client.auth?
  conn
end

#callFaraday::Response

Emits a HTTP request based on the parameters and API information.

Returns:

  • (Faraday::Response)

    the response



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/eson/http/request.rb', line 43

def call
  resource = base_resource

  response = (
    case request_method
    when :get
      resource.get fill do |req|
        (req.body = source) if source
      end
    when :head
      resource.head fill
    when :delete
      resource.delete(fill) do |req|
        req.body = source if source
      end
    when :post
      resource.post(fill, source)
    when :put
      resource.put(fill, source)
    end
  )
  
  response
end

#fillString

Fills in the blanks in the path by using Addressable::Template.

Returns:

  • (String)

    the full uri



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/eson/http/request.rb', line 72

def fill
  template = Addressable::Template.new(path)

  template_keys = template.keys.map(&:to_sym)
  expansions = {}
  query_values = {}

  url_params.each do |p|
    val = self.send(p)

    if template_keys.include? p
      expansions[p] = val if val
    else
      query_values[p] = val if val
    end
  end

  uri = template.expand(expansions)
  uri.query_values = query_values unless query_values.empty?
  uri
end

#request_methodSymbol

Returns the request method. If none is given, it is assumed to be ‘:get`.

Returns:

  • (Symbol)

    The request method.



112
113
114
# File 'lib/eson/http/request.rb', line 112

def request_method
  @request_method || :get
end

#url_paramsArray

The parameters that are passed in the url. These are all params without the source params.

Returns:

  • (Array)

    the url parameters



98
99
100
101
102
103
104
105
106
# File 'lib/eson/http/request.rb', line 98

def url_params
  if self.respond_to? :source_param
    source_params = Array(source_param)
  else
    source_params = []
  end

  (parameters - source_params)
end