add ability to get my channels via API

This commit is contained in:
Lee Lawlor 2014-05-19 12:31:31 -04:00
parent 5402830ec3
commit c28007ff2d
6 changed files with 156 additions and 4 deletions

View File

@ -53,6 +53,16 @@ class ApplicationController < ActionController::Base
end
end
# authenticates user based on the user's api_key
def authenticate_via_api_key!
# exit if no api_key
return false if params[:api_key].blank?
# get the user
user = User.find_by_api_key(params[:api_key])
# sign in the user if they exist
sign_in(user, store: false) if user.present?
end
# get the locale, but don't fail if header value doesn't exist
def get_locale
locale = get_header_value('HTTP_ACCEPT_LANGUAGE')

View File

@ -1,5 +1,6 @@
class ChannelsController < ApplicationController
include ChannelsHelper, ApiKeys
before_filter :authenticate_via_api_key!, :only => [:index]
before_filter :require_user, :except => [:realtime, :realtime_update, :show, :post_data, :social_show, :social_feed, :public]
before_filter :set_channels_menu
layout 'application', :except => [:social_show, :social_feed]
@ -140,7 +141,8 @@ class ChannelsController < ApplicationController
@channels = current_user.channels
respond_to do |format|
format.html
format.json { render :json => @channels.to_json(:root => false) }
format.json { render :json => @channels.to_json(Channel.private_options) }
format.xml { render :xml => @channels.to_xml(Channel.private_options) }
end
end

View File

@ -20,7 +20,7 @@
<li class="subitem"><a href="#get_feed">Get Channel Feed</a></li>
<li class="subitem"><a href="#get_field">Get Channel Field Feed</a></li>
<li class="subitem"><a href="#get_status">Get Status Updates</a></li>
<li class="subitem"><a href="#list_public">List Public Channels</a></li>
<li class="subitem"><a href="#index">List Channels</a></li>
<li class="subitem"><a href="#create">Create a Channel</a></li>
<li class="subitem"><a href="#clear">Clear a Channel</a></li>
<li class="subitem"><a href="#delete">Delete a Channel</a></li>

View File

@ -114,7 +114,7 @@
<%= render 'docs/channels/status' %>
<hr />
<%= render 'docs/channels/public_index' %>
<%= render 'docs/channels/index' %>
<hr />
<%= render 'docs/channels/create' %>

View File

@ -1,6 +1,6 @@
<div>
<%= render 'response' %>
<h2 id="list_public">List Public Channels</h2>
<h2 id="index">List Public Channels</h2>
</div>
<br>
@ -147,3 +147,131 @@ Example GET:
</div>
<div>
<%= render 'response' %>
<h2>List My Channels</h2>
</div>
<br>
To view a list of your Channels, send an HTTP GET to
<br>
<code><%= @ssl_api_domain %>channels<span class="format format-json">.json</span><span class="format format-xml">.xml</span></code> .
<br><br>
Valid parameters:
<ul>
<li><b>api_key</b> (string) - Your Account API Key (this is different from a Channel API Key, and can be found in your Account settings). (required)</li>
</ul>
<br>
Example GET:
<pre>GET <span class="str"><%= @ssl_api_domain %>channels<span class="format format-json">.json</span><span class="format format-xml">.xml</span></span></pre>
<br>
<div class="format format-block-xxxl format-text">
The response will be a webpage with a <a href="/channels">list of your Channels</a>.
</div>
<div class="format format-block-xxxl format-json">
The response will be a JSON object of your Channels, for example:
<pre class="prettyprint">
[
{
"id": 9,
"name": "my_house",
"description": "Netduino Plus connected to sensors around the house",
"latitude": "40.44",
"longitude": "-79.996",
"created_at": "2010-12-13T20:20:06-05:00",
"elevation": "",
"last_entry_id": 6062691,
"ranking" :100,
"username":"hans",
"tags": [],
"api_keys":
[
{
"api_key": "XXXXXXXXXXXXXXXX",
"write_flag": true
}
]
},
{
"id": 5683,
"name": "Residential Data Points",
"description": "Connected Arduino",
"latitude": "32.664548",
"longitude": "-73.654972",
"created_at": "2013-05-15T12:33:57-04:00",
"elevation": "100",
"last_entry_id": 731713,
"ranking": 100,
"username": "hans",
"tags": [],
"api_keys":
[
{
"api_key": "XXXXXXXXXXXXXXXX",
"write_flag": true
}
]
}
]
</pre>
</div>
<div class="format format-block-xxxl format-xml">
The response will be an XML object of your Channels, for example:
<pre class="prettyprint">
&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;channels type="array">
&lt;channel>
&lt;id type="integer">9&lt;/id>
&lt;name>my_house&lt;/name>
&lt;description>
Netduino Plus connected to sensors around the house
&lt;/description>
&lt;latitude type="decimal">40.44&lt;/latitude>
&lt;longitude type="decimal">-79.996&lt;/longitude>
&lt;created-at type="dateTime">2010-12-13T20:20:06-05:00&lt;/created-at>
&lt;elevation/>
&lt;last-entry-id type="integer">6062720&lt;/last-entry-id>
&lt;ranking type="integer">100&lt;/ranking>
&lt;username>hans&lt;/username>
&lt;tags type="array" />
&lt;api-keys type="array">
&lt;api-key>
&lt;api-key>XXXXXXXXXXXXXXXX&lt;/api-key>
&lt;write-flag type="boolean">true&lt;/write-flag>
&lt;/api-key>
&lt;/api-keys>
&lt;/channel>
&lt;channel>
&lt;id type="integer">5683&lt;/id>
&lt;name>Residential Data Points&lt;/name>
&lt;description>Connected Arduino&lt;/description>
&lt;latitude type="decimal">32.664548&lt;/latitude>
&lt;longitude type="decimal">-73.654972&lt;/longitude>
&lt;created-at type="dateTime">2013-05-15T12:33:57-04:00&lt;/created-at>
&lt;elevation>100&lt;/elevation>
&lt;last-entry-id type="integer">731720&lt;/last-entry-id>
&lt;ranking type="integer">100&lt;/ranking>
&lt;username>hans&lt;/username>
&lt;tags type="array" />
&lt;api-keys type="array">
&lt;api-key>
&lt;api-key>XXXXXXXXXXXXXXXX&lt;/api-key>
&lt;write-flag type="boolean">true&lt;/write-flag>
&lt;/api-key>
&lt;/api-keys>
&lt;/channel>
&lt;/channels>
</pre>
</div>

View File

@ -114,6 +114,18 @@ describe ChannelsController do
@feed = FactoryGirl.create(:feed, :field1 => 10, :channel => @channel)
end
describe "list channels" do
it "should not list my channels" do
get :index, {:api_key => 'INVALID', :format => 'json'}
response.status.should eq(401)
end
it "lists my channels" do
get :index, {:api_key => @user.api_key, :format => 'json'}
response.should be_successful
end
end
describe "create channel" do
it 'creates a channel' do
post :create, {:key => @user.api_key, :name => 'mychannel'}