Skip to content

Commit

Permalink
Now you can easily set any arbitrary HTTP headers for your resource r…
Browse files Browse the repository at this point in the history
…equests
  • Loading branch information
episod committed Mar 4, 2010
1 parent 340e855 commit 453898f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/controllers/api_request_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def make_request
if params[:request_content_type]
options[:headers] = { "Content-Type" => params[:request_content_type]}
end
some_headers = process_headers(4, params)
options[:headers].merge!(some_headers)
@these_headers = {} unless @these_headers

options[:method] = params[:method]
options[:postdata] = params[:request_body]
@api_request = ApiRequest.make_request(params[:resource_url], @access_token, options)
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ def clear_ghost_trap
GhostTrap.clear!
end

def process_headers(number, params)
process_kvs("header", number, params)
end

def process_kvs(pre_label, number, params)
parameters = { }
1.upto(number) do | i |
if params["#{pre_label}_value_#{i}"]
next if params["#{pre_label}_value_#{i}"] == ""
parameters[params["#{pre_label}_key_#{i}"]] = params["#{pre_label}_value_#{i}"]
end
end
parameters
end

protected
def rescues_path(template_name)
"#{RAILS_ROOT}/app/views/rescues/#{template_name}.erb"
Expand Down
16 changes: 15 additions & 1 deletion app/views/api_request/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@
</tr>
<tr valign="top">
<th>Request Content-Type</th>
<td><%= select_tag :request_content_type, request_content_types(:default => @service_provider.default_request_content_type) %>
<td><%= select_tag :request_content_type, request_content_types(:default => @service_provider.default_request_content_type) %></td>
</tr>
<tr valign="top">
<th>Additional Request Headers</th>
<td>
<table>
<% 1.upto(4) do | i | -%>
<tr>
<td><%= text_field_tag "header_key_#{i}" %><br/>Key</td>
<td><%= text_field_tag "header_value_#{i}" %><br/>Value</td>
</tr>
<% end -%>
</table>
</td>
</tr>
</table>
<%= submit_tag "Make the request!" %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<div id="main_content_top"><%= flash[:notice] %></div>
<div id="main_content">

<div class="content" style="margin-top:10px;margin-bottom:30px;">
<div class="content" style="margin-top:10px;margin-bottom:50px;">
<%= yield %>

</div>
Expand Down

0 comments on commit 453898f

Please sign in to comment.