Module: Amfetamine::RestHelpers::ClassMethods

Defined in:
lib/amfetamine/rest_helpers.rb

Instance Method Summary collapse

Instance Method Details

#base_uriObject



68
69
70
# File 'lib/amfetamine/rest_helpers.rb', line 68

def base_uri
  @base_uri || Amfetamine::Config.base_uri
end

#base_uri=(value) ⇒ Object



122
123
124
125
# File 'lib/amfetamine/rest_helpers.rb', line 122

def base_uri=(value)
  raise Amfetamine::ConfigurationInvalid, 'Invalid value for base uri' if !value.is_a?(String)
  @base_uri = value
end

#find_path(id, params = {}) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/amfetamine/rest_helpers.rb', line 58

def find_path(id, params={})
  params_for_rest_path = params.merge({:no_base_uri => true, :no_resource_suffix => true})
  result = "#{self.rest_path(params_for_rest_path)}/#{id.to_s}"

  result = base_uri + result unless params[:no_base_uri]
  result = result + resource_suffix unless params[:no_resource_suffix]
  return result
end

#handle_request(method, path, opts = {}) ⇒ Object

wraps rest requests to the corresponding service emerging



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/amfetamine/rest_helpers.rb', line 74

def handle_request(method, path, opts={})
  Amfetamine.logger.warn "Making request to #{path} with #{method} and #{opts.inspect}"
  case method
  when :get
    response = rest_client.get(path, opts)
  when :post
    response = rest_client.post(path, opts)
  when :put
    response = rest_client.put(path, opts)
  when :delete
    response = rest_client.delete(path, opts)
  else
    raise UnknownRESTMethod, "handle_request only responds to get, put, post and delete"
  end
  parse_response(response)
end

#parse_response(response) ⇒ Object

Returns a hash with human readable status and parsed body



92
93
94
95
96
97
98
99
100
101
# File 'lib/amfetamine/rest_helpers.rb', line 92

def parse_response(response)
  status = RESPONSE_STATUSES.fetch(response.code) { raise "Response not known" }
  raise Amfetamine::RecordNotFound if status == :notfound
  body = if response.body && !(response.body.blank?)
           response.parsed_response
         else
           self.to_json
         end
  { :status => status, :body => body }
end

#resource_suffixObject



107
108
109
# File 'lib/amfetamine/rest_helpers.rb', line 107

def resource_suffix
  @resource_suffix || Amfetamine::Config.resource_suffix || ""
end

#resource_suffix=(value) ⇒ Object



117
118
119
120
# File 'lib/amfetamine/rest_helpers.rb', line 117

def resource_suffix=(value)
  raise Amfetamine::ConfigurationInvalid, 'Invalid value for resource suffix' if !value.is_a?(String)
  @resource_suffix = value
end

#rest_clientObject



103
104
105
# File 'lib/amfetamine/rest_helpers.rb', line 103

def rest_client
  @rest_client || Amfetamine::Config.rest_client
end

#rest_client=(value) ⇒ Object

Allows setting a different rest client per class



112
113
114
115
# File 'lib/amfetamine/rest_helpers.rb', line 112

def rest_client=(value)
  raise Amfetamine::ConfigurationInvalid, 'Invalid value for rest_client' if ![:get,:put,:delete,:post].all? { |m| value.respond_to?(m) }
  @rest_client = value
end

#rest_path(params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/amfetamine/rest_helpers.rb', line 45

def rest_path(params={})
  result = if params[:relationship]
    relationship = params[:relationship]
    "/#{relationship.full_path}"
  else
    "/#{self.name.downcase.pluralize}"
  end

  result = base_uri + result unless params[:no_base_uri]
  result = result + resource_suffix unless params[:no_resource_suffix]
  return result
end