From c28007ff2d744b732309d8332bec0ae4ffebcda4 Mon Sep 17 00:00:00 2001 From: Lee Lawlor Date: Mon, 19 May 2014 12:31:31 -0400 Subject: [PATCH] add ability to get my channels via API --- app/controllers/application_controller.rb | 10 ++ app/controllers/channels_controller.rb | 4 +- app/views/docs/_sidebar.html.erb | 2 +- app/views/docs/channels.html.erb | 2 +- ..._public_index.html.erb => _index.html.erb} | 130 +++++++++++++++++- spec/controllers/channels_controller_spec.rb | 12 ++ 6 files changed, 156 insertions(+), 4 deletions(-) rename app/views/docs/channels/{_public_index.html.erb => _index.html.erb} (53%) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bdec2d1..3e9cac8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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') diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index f46134f..a92d968 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -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 diff --git a/app/views/docs/_sidebar.html.erb b/app/views/docs/_sidebar.html.erb index e82f268..ef31def 100644 --- a/app/views/docs/_sidebar.html.erb +++ b/app/views/docs/_sidebar.html.erb @@ -20,7 +20,7 @@
  • Get Channel Feed
  • Get Channel Field Feed
  • Get Status Updates
  • -
  • List Public Channels
  • +
  • List Channels
  • Create a Channel
  • Clear a Channel
  • Delete a Channel
  • diff --git a/app/views/docs/channels.html.erb b/app/views/docs/channels.html.erb index 977b2ab..daf24af 100644 --- a/app/views/docs/channels.html.erb +++ b/app/views/docs/channels.html.erb @@ -114,7 +114,7 @@ <%= render 'docs/channels/status' %>
    - <%= render 'docs/channels/public_index' %> + <%= render 'docs/channels/index' %>
    <%= render 'docs/channels/create' %> diff --git a/app/views/docs/channels/_public_index.html.erb b/app/views/docs/channels/_index.html.erb similarity index 53% rename from app/views/docs/channels/_public_index.html.erb rename to app/views/docs/channels/_index.html.erb index c604e4a..08d6247 100644 --- a/app/views/docs/channels/_public_index.html.erb +++ b/app/views/docs/channels/_index.html.erb @@ -1,6 +1,6 @@
    <%= render 'response' %> -

    List Public Channels

    +

    List Public Channels


    @@ -147,3 +147,131 @@ Example GET: +
    + <%= render 'response' %> +

    List My Channels

    +
    + +
    +To view a list of your Channels, send an HTTP GET to +
    +<%= @ssl_api_domain %>channels.json.xml . + +

    +Valid parameters: + + +
    +Example GET: + +
    GET <%= @ssl_api_domain %>channels.json.xml
    + +
    + +
    + The response will be a webpage with a list of your Channels. +
    + +
    + The response will be a JSON object of your Channels, for example: + +
    +[
    +  {
    +    "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
    +      }
    +    ]
    +  }
    +]
    +
    + +
    + +
    + The response will be an XML object of your Channels, for example: + +
    +<?xml version="1.0" encoding="UTF-8"?>
    +<channels type="array">
    +  <channel>
    +    <id type="integer">9</id>
    +    <name>my_house</name>
    +    <description>
    +    Netduino Plus connected to sensors around the house
    +    </description>
    +    <latitude type="decimal">40.44</latitude>
    +    <longitude type="decimal">-79.996</longitude>
    +    <created-at type="dateTime">2010-12-13T20:20:06-05:00</created-at>
    +    <elevation/>
    +    <last-entry-id type="integer">6062720</last-entry-id>
    +    <ranking type="integer">100</ranking>
    +    <username>hans</username>
    +    <tags type="array" />
    +    <api-keys type="array">
    +        <api-key>
    +            <api-key>XXXXXXXXXXXXXXXX</api-key>
    +            <write-flag type="boolean">true</write-flag>
    +        </api-key>
    +    </api-keys>
    +  </channel>
    +  <channel>
    +    <id type="integer">5683</id>
    +    <name>Residential Data Points</name>
    +    <description>Connected Arduino</description>
    +    <latitude type="decimal">32.664548</latitude>
    +    <longitude type="decimal">-73.654972</longitude>
    +    <created-at type="dateTime">2013-05-15T12:33:57-04:00</created-at>
    +    <elevation>100</elevation>
    +    <last-entry-id type="integer">731720</last-entry-id>
    +    <ranking type="integer">100</ranking>
    +    <username>hans</username>
    +    <tags type="array" />
    +    <api-keys type="array">
    +        <api-key>
    +            <api-key>XXXXXXXXXXXXXXXX</api-key>
    +            <write-flag type="boolean">true</write-flag>
    +        </api-key>
    +    </api-keys>
    +  </channel>
    +</channels>
    +
    + +
    + diff --git a/spec/controllers/channels_controller_spec.rb b/spec/controllers/channels_controller_spec.rb index 8ae25be..6a476da 100644 --- a/spec/controllers/channels_controller_spec.rb +++ b/spec/controllers/channels_controller_spec.rb @@ -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'}