Class: AbstractApiWrapper::Request

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

Defined Under Namespace

Classes: NoResourceGiven

Constant Summary collapse

METHODS_MAP =
{
  'all'     => 'get',
  'head'    => 'head',
  'find'    => 'get',
  'create'  => 'post',
  'update'  => 'put',
  'destroy' => 'delete'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Request

Returns a new instance of Request.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/abstract_api_wrapper.rb', line 33

def initialize(*options)
  @client  = options[1]
  @path    = []
  @chain   = []
  @filters = Hashie::Mash.new
  @params  = Hashie::Mash.new

  method_name = options[0]

  unless method_name.nil?
    @path  << method_name
    @chain << method_name
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *params, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/abstract_api_wrapper.rb', line 48

def method_missing(name, *params, &block)
  method_name = name.to_s
  return self if chain.last == method_name

  chain.push(method_name)

  case method_name
  when 'all'
    filters.merge!(params.first || {})
  when 'head'
    filters.merge!(params.first || {})
  when 'find'
    path.push(params.first.to_s)
  when 'create'
    @params = Hashie::Mash.new(params.first || {})
  when 'update'
    if chain[-2] == 'find'
      @params = Hashie::Mash.new(params.first || {})
    end
  when 'destroy'
    if chain[-2] != 'find'
      raise NoResourceGiven.new('Missing resource id to destroy')
    end
  else
    path.push(method_name)
  end

  self
end

Instance Attribute Details

#chainObject

Returns the value of attribute chain.



22
23
24
# File 'lib/abstract_api_wrapper.rb', line 22

def chain
  @chain
end

#filtersObject

Returns the value of attribute filters.



22
23
24
# File 'lib/abstract_api_wrapper.rb', line 22

def filters
  @filters
end

#paramsObject

Returns the value of attribute params.



22
23
24
# File 'lib/abstract_api_wrapper.rb', line 22

def params
  @params
end

#pathObject

Returns the value of attribute path.



22
23
24
# File 'lib/abstract_api_wrapper.rb', line 22

def path
  @path
end

Instance Method Details

#endpointObject



101
102
103
104
105
106
107
108
# File 'lib/abstract_api_wrapper.rb', line 101

def endpoint
  # Here is a Query String API versioning with the `apiver` param
  # You can change this to whatever your API versioning system is
  filters.apiver = @client.apiver
  query_string   = filters.map { |k, v| "#{k}=#{v}" }.join('&')

  "#{@client.base_url}/#{path.join('/')}?#{query_string}"
end

#headersObject



94
95
96
97
98
99
# File 'lib/abstract_api_wrapper.rb', line 94

def headers
  {
    'Authorization' => "token #{@client.access_token}",
    'Content-Type' => 'application/json'
  }
end

#runObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/abstract_api_wrapper.rb', line 78

def run
  method = METHODS_MAP[@chain.last] || 'get'
  params = @params.any? ? @params.to_json : nil

  request = Faraday.send(method, endpoint, params, headers)
  response = Response.new(request)

  # Reset variables
  path    = []
  chain   = []
  filters = Hashie::Mash.new
  params  = Hashie::Mash.new

  response.body
end