Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed up the way things are configured. Fleshed out the docs. Wrote a…

… few tests.
  • Loading branch information...
commit b2e82b75607ed97c85c9a4348c2d1fae9f26bd00 1 parent 652ddae
Jim Bancroft authored
View
2  Gemfile
@@ -3,7 +3,7 @@ source "http://rubygems.org"
# Example:
# gem "activesupport", ">= 2.3.5"
-gem 'activeresource', '> 2.3.8'
+gem 'activeresource', '>= 2.3.8'
gem 'configatron', '>= 2.6.4'
# Add dependencies to develop your gem here.
View
2  Gemfile.lock
@@ -35,7 +35,7 @@ PLATFORMS
ruby
DEPENDENCIES
- activeresource (> 2.3.8)
+ activeresource (>= 2.3.8)
bundler (~> 1.0.0)
configatron (>= 2.6.4)
jeweler (~> 1.5.2)
View
44 README.rdoc
@@ -4,9 +4,15 @@ Ruby wrapper for Wistia's API
== Installation
+Required gems:
+* activeresource >= 2.3.8
+* configatron >= 2.6.4
+
gem install wistia-api
-== Usage
+RDoc[http://rubydoc.info/gems/wistia-api/0.1.3/frames]
+
+== Basic Usage
Start by requiring wistia:
require 'wistia'
@@ -14,10 +20,44 @@ Start by requiring wistia:
Configure your API password:
Wistia.password = 'your-api-key-here'
-Now you can use the <tt>Wistia::Media</tt> and <tt>Wistia::Project</tt> classes as ActiveResource wrappers around Wistia's API.
+You can get an API password by following the instructions here: http://wistia.com/doc/api-enable
+
+Now you can use the +Wistia::Media+, +Wistia::Project+, and +Wistia::Projects::Sharing+ classes as ActiveResource wrappers around Wistia's API.
See http://wistia.com/doc/data-api for more info.
+== Configuration Options
+
+Set the format of the API:
+ Wistia.format = 'json' # This is the default.
+ Wistia.format = 'xml'
+
+Read configuration from an external YAML file:
+ Wistia.use_config!(path_to_yaml)
+
+For an example YAML config, see spec/support/config.local.yml
+
+Configure using a Hash:
+ Wistia.use_config!(:wistia => {
+ :api => {
+ :url => 'custom-api-url',
+ :user => 'custom-api-user',
+ :password => 'your-api-password',
+ :format => 'xml-or-json'
+ }
+ })
+
+== Examples
+
+List all Media in your account:
+ Wistia::Media.find(:all)
+
+List all Projects in your account:
+ Wistia::Project.find(:all)
+
+List all Sharing objects for project 23:
+ Wistia::Projects::Sharing.find(:all, :params => { :project_id => 23 })
+
== Copyright
Copyright (c) 2011 Wistia, Inc. See LICENSE.txt for
View
2  Rakefile
@@ -23,7 +23,7 @@ Jeweler::Tasks.new do |gem|
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
# gem.add_development_dependency 'rspec', '> 1.2.3'
- gem.add_runtime_dependency 'activeresource', '> 2.3.8'
+ gem.add_runtime_dependency 'activeresource', '>= 2.3.8'
gem.add_runtime_dependency 'configatron', '>= 2.6.4'
end
Jeweler::RubygemsDotOrgTasks.new
View
41 lib/wistia.rb
@@ -3,10 +3,47 @@
require 'wistia/base'
require 'wistia/media'
require 'wistia/project'
+require 'wistia/projects/sharing'
module Wistia
+
+ # Specifies a new configuration to use for the API.
+ # Accepts either a Hash or a String pointing to a YAML configuration file.
+ #
+ # Example using a Hash:
+ # Wistia.use_config!(:wistia => { :api => { :key => 'your-api-key', :format => 'json-or-xml' } })
+ def self.use_config!(config)
+ if config.is_a?(String) && File.exists?(config) && File.extname(config) == '.yml'
+ configatron.configure_from_yaml(config)
+ elsif config.is_a?(Hash)
+ configatron.configure_from_hash(config)
+ else
+ raise ArgumentError, 'Invalid config'
+ end
+
+ refresh_config!
+ end
+
+ # Set your API password using this method.
+ # For instructions to find your API password, go here: http://wistia.com/doc/api-enable
def self.password=(pw)
- Wistia::Media.password = pw
- Wistia::Project.password = pw
+ use_config!(:wistia => { :api => { :key => pw } })
+ end
+
+ # Set the format that the API uses to either 'json' or 'xml'.
+ # Accepts either a String or a Symbol.
+ def self.format=(fmt)
+ valid_formats = [ :json, :xml ]
+ raise ArgumentError, "Invalid format. Supported formats: #{valid_formats.join(', ')}." unless valid_formats.include?(fmt.to_sym)
+ use_config!(:wistia => { :api => { :format => fmt.to_sym } })
+ end
+
+ # Force each ActiveResource descendant to re-read its configuration from the configatron.
+ def self.refresh_config!
+ Wistia::Media.refresh_config!
+ Wistia::Project.refresh_config!
+ Wistia::Projects::Sharing.refresh_config!
end
+
end
+
View
14 lib/wistia/base.rb
@@ -2,10 +2,15 @@
module Wistia
class Base < ActiveResource::Base
- self.site = Wistia::Config.config.api.url
- self.user = Wistia::Config.config.api.user
- self.password = Wistia::Config.config.api.key
- self.format = Wistia::Config.config.api.format
+ # Resets all the ActiveResource configuration options to what's currently stored in the configatron.
+ def self.refresh_config!
+ self.site = Wistia::Config.config.api.url
+ self.user = Wistia::Config.config.api.user
+ self.password = Wistia::Config.config.api.key
+ self.format = Wistia::Config.config.api.format.to_sym
+ end
+
+ refresh_config!
def to_json(options = {})
return self.attributes.to_json(options)
@@ -13,3 +18,4 @@ def to_json(options = {})
end
end
+
View
8 lib/wistia/media.rb
@@ -13,14 +13,6 @@ def still(width = 640, options = {})
return nil
end
- # Needed when deserializing from cache:
- class Thumbnail < Wistia::Base
- end # Thumbnail
-
- # Needed when deserializing from cache:
- class Asset < Wistia::Base
- end # Asset
-
end # Media
end # Wistia
View
18 lib/wistia/projects/sharing.rb
@@ -0,0 +1,18 @@
+module Wistia
+ module Projects
+ class Sharing < Wistia::Base
+ def self.refresh_config!
+ super
+ self.site = "#{Wistia::Config.config.api.url}projects/:project_id/"
+ end
+
+ refresh_config!
+
+ def to_json(options = {})
+ return self.attributes.to_json(options)
+ end
+
+ end
+ end
+end
+
View
2  spec/spec_helper.rb
@@ -1,7 +1,7 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
-require 'wistia-api'
+require 'wistia'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
View
6 spec/support/config.test.yml
@@ -0,0 +1,6 @@
+wistia:
+ api:
+ key: 'test'
+ user: 'api'
+ format: 'json'
+ url: 'http://api.wistia.local:3000/v1/'
View
5 spec/support/local_config.rb
@@ -0,0 +1,5 @@
+configatron.wistia.api.key = 'test'
+configatron.wistia.api.user = 'api'
+configatron.wistia.api.format = :json
+configatron.wistia.api.url = 'http://api.wistia.local:3000/v1/'
+configatron.wistia.api.projects.url = 'http://api.wistia.local:3000/v1/projects'
View
7 spec/wistia-api_spec.rb
@@ -1,7 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-
-describe "WistiaApi" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
- end
-end
View
28 spec/wistia/base_spec.rb
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+module Wistia
+ describe Base do
+ describe 'refresh_config!' do
+ it 'sets the "site" public class attribute' do
+ Wistia::Base.should_receive(:site=)
+ Wistia::Base.refresh_config!
+ end
+
+ it 'sets the "user" public class attribute' do
+ Wistia::Base.should_receive(:user=)
+ Wistia::Base.refresh_config!
+ end
+
+ it 'sets the "password" public class attribute' do
+ Wistia::Base.should_receive(:password=)
+ Wistia::Base.refresh_config!
+ end
+
+ it 'sets the "format" public class attribute' do
+ Wistia::Base.should_receive(:format=)
+ Wistia::Base.refresh_config!
+ end
+ end
+ end
+end
+
View
17 spec/wistia/projects/sharing_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+module Wistia
+ module Projects
+ describe Sharing do
+ describe 'refresh_config!' do
+ it 'overrides the site value to indicate nesting under projects' do
+ api_url = Wistia::Config.config.api.url
+ Wistia::Projects::Sharing.should_receive(:site=).with(api_url).ordered
+
+ Wistia::Projects::Sharing.should_receive(:site=).with("#{api_url}projects/:project_id/").ordered
+ Wistia::Projects::Sharing.refresh_config!
+ end
+ end
+ end
+ end
+end
View
80 spec/wistia_spec.rb
@@ -0,0 +1,80 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe Wistia do
+
+ describe 'use_config!' do
+ context 'given a hash argument' do
+ it 'uses the hash to configure configatron' do
+ Wistia.use_config!( :wistia => { :api => { :key => 'test' } } )
+ configatron.wistia.api.key.should == 'test'
+ end
+ end
+
+ context 'given a path to a yaml file' do
+ it 'makes configatron use the yaml file for configuration' do
+ path_to_yaml = File.dirname(__FILE__) + '/support/config.test.yml'
+ configatron.should_receive(:configure_from_yaml).with(path_to_yaml)
+ Wistia.use_config!(path_to_yaml)
+ end
+ end
+
+ context 'given an invalid config' do
+ it 'raises an ArgumentError' do
+ lambda {
+ Wistia.use_config!(nil)
+ }.should raise_error(ArgumentError, 'Invalid config')
+ end
+ end
+
+ it 'uses the new configuration' do
+ Wistia.should_receive(:refresh_config!)
+ Wistia.use_config!( :wistia => { :api => { :key => 'test' } } )
+ end
+ end
+
+ describe 'password=' do
+ it 'reconfigures the API using the new password parameter' do
+ new_password = 'test'
+ Wistia.password = new_password
+ Wistia.should_receive(:use_config!).with(hash_including(:wistia => { :api => { :key => new_password } }))
+ Wistia.password = new_password
+ end
+ end
+
+ describe 'format=' do
+ context 'given a valid format' do
+ it 'reconfigures the API using the new format parameter' do
+ new_format = :xml
+ Wistia.should_receive(:use_config!).with(hash_including(:wistia => { :api => { :format => new_format } }))
+ Wistia.format = new_format
+ end
+ end
+
+ context 'given an invalid format' do
+ it 'raises an ArgumentError' do
+ lambda {
+ Wistia.format = 'invalid_format'
+ }.should raise_error(ArgumentError, /Invalid format/)
+ end
+ end
+ end
+
+ describe 'refresh_config!' do
+ it 'forces Wistia::Media to re-read its configuration from the configatron' do
+ Wistia::Media.should_receive(:refresh_config!)
+ Wistia.refresh_config!
+ end
+
+ it 'forces Wistia::Project to re-read its configuration from the configatron' do
+ Wistia::Project.should_receive(:refresh_config!)
+ Wistia.refresh_config!
+ end
+
+ it 'forces Wistia::Projects::Sharing to re-read its configuration from the configatron' do
+ Wistia::Projects::Sharing.should_receive(:refresh_config!)
+ Wistia.refresh_config!
+ end
+ end
+
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.