Skip to content

Commit

Permalink
shark tests passing now
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas LaCasse committed May 23, 2012
1 parent 7a759ee commit ef271df
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 102 deletions.
2 changes: 1 addition & 1 deletion bin/spire
Expand Up @@ -9,4 +9,4 @@ $:.unshift File.expand_path("../../lib", bin_file)

require "spire/commands"

Spire::Commands::CLI.run(*ARGV)
Spire::Commands::CLI.run(*ARGV)
20 changes: 2 additions & 18 deletions lib/spire/api.rb
Expand Up @@ -25,10 +25,11 @@ def self.deserialize(string)
end

attr_reader :client, :description, :schema
def initialize(url="https://api.spire.io", options={})
def initialize(url="https://api.spire.io", spire=nil, options={})
@version = options[:version] || "1.0"
@client = Excon
@url = url
@spire = spire
end

define_request(:discover) do
Expand Down Expand Up @@ -166,22 +167,5 @@ def get_application(application_key)
end
API::Application.new(self, response.data)
end

# Returns a billing object than contains a list of all the plans available
# @param [String] info optional object description
# @return [Billing]
def billing(info=nil)
response = request(:billing)
raise "Error getting billing plans: #{response.status}" if response.status != 200
API::Billing.new(self, response.data)
end

class Billing < Resource
def resource_name
"billing"
end
end

end

end
54 changes: 27 additions & 27 deletions lib/spire/api/application.rb
Expand Up @@ -9,7 +9,7 @@ def resource_name
"application"
end

def initialize(spire, data)
def initialize(api, data)
super
@resources = data["resources"]
end
Expand All @@ -31,8 +31,8 @@ def initialize(spire, data)
:body => body,
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("channel"),
"Content-Type" => @spire.mediaType("channel")
"Accept" => @api.mediaType("channel"),
"Content-Type" => @api.mediaType("channel")
}
}
end
Expand All @@ -46,7 +46,7 @@ def initialize(spire, data)
:url => url,
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("channels"),
"Accept" => @api.mediaType("channels"),
}
}
end
Expand All @@ -61,7 +61,7 @@ def initialize(spire, data)
:query => {:name => name},
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("channels"),
"Accept" => @api.mediaType("channels"),
}
}
end
Expand All @@ -76,7 +76,7 @@ def initialize(spire, data)
:url => url,
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("subscriptions"),
"Accept" => @api.mediaType("subscriptions"),
}
}
end
Expand All @@ -94,8 +94,8 @@ def initialize(spire, data)
}.to_json,
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("subscription"),
"Content-Type" => @spire.mediaType("subscription")
"Accept" => @api.mediaType("subscription"),
"Content-Type" => @api.mediaType("subscription")
}
}
end
Expand All @@ -110,7 +110,7 @@ def initialize(spire, data)
:query => {:name => name},
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("subscriptions"),
"Accept" => @api.mediaType("subscriptions"),
}
}
end
Expand All @@ -126,8 +126,8 @@ def initialize(spire, data)
:body => data.to_json,
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("member"),
"Content-Type" => @spire.mediaType("member")
"Accept" => @api.mediaType("member"),
"Content-Type" => @api.mediaType("member")
}
}
end
Expand All @@ -141,7 +141,7 @@ def initialize(spire, data)
:url => url,
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("members"),
"Accept" => @api.mediaType("members"),
}
}
end
Expand All @@ -156,7 +156,7 @@ def initialize(spire, data)
:query => {:login => login},
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("member"),
"Accept" => @api.mediaType("member"),
}
}
end
Expand All @@ -169,7 +169,7 @@ def initialize(spire, data)
:url => url,
:body => data.to_json,
:headers => {
"Accept" => @spire.mediaType("member"),
"Accept" => @api.mediaType("member"),
}
}
end
Expand All @@ -182,7 +182,7 @@ def initialize(spire, data)
:method => :get,
:url => url,
:headers => {
"Accept" => @spire.mediaType("member"),
"Accept" => @api.mediaType("member"),
"Authorization" => "Basic #{auth}"
}
}
Expand All @@ -204,7 +204,7 @@ def authenticate(login, password)
unless response.status == 200
raise "Error authenticating for application #{self.name}: (#{response.status}) #{response.body}"
end
API::Member.new(@spire, response.data)
API::Member.new(@api, response.data)
end

#Alternative application authentication, without using basic auth
Expand All @@ -213,7 +213,7 @@ def authenticate_with_post(login, password)
unless response.status == 201
raise "Error authenticating for application #{self.name}: (#{response.status}) #{response.body}"
end
API::Member.new(@spire, response.data)
API::Member.new(@api, response.data)
end

#If you do not give a new password, you will get back an authenticated member but have to change
Expand All @@ -225,15 +225,15 @@ def reset_password(reset_key, new_password = nil)
unless response.status == 201
raise "Error reseting password for application #{self.name}: (#{response.status}) #{response.body}"
end
API::Member.new(@spire, response.data)
API::Member.new(@api, response.data)
end

def create_member(member_data)
response = request(:create_member, member_data)
unless response.status == 201
raise "Error creating member for application #{self.name}: (#{response.status}) #{response.body}"
end
API::Member.new(@spire, response.data)
API::Member.new(@api, response.data)
end

#Resets a members password based on email
Expand All @@ -256,7 +256,7 @@ def members!
end
@members = {}
response.data.each do |login, properties|
@members[login] = API::Member.new(@spire, properties)
@members[login] = API::Member.new(@api, properties)
end
@members
end
Expand All @@ -267,7 +267,7 @@ def get_member(member_login)
raise "Error finding member with login #{member_login}: (#{response.status}) #{response.body}"
end
properties = response.data[member_login]
member = API::Member.new(@spire, properties)
member = API::Member.new(@api, properties)
@members[member_login] = member if @members.is_a?(Hash)
member
end
Expand All @@ -280,7 +280,7 @@ def create_channel(name, options={})
raise "Error creating Channel: (#{response.status}) #{response.body}"
end
properties = response.data
channels[name] = API::Channel.new(@spire, properties)
channels[name] = API::Channel.new(@api, properties)
end

def create_subscription(subscription_name, channel_names)
Expand All @@ -290,7 +290,7 @@ def create_subscription(subscription_name, channel_names)
raise "Error creating Subscription: (#{response.status}) #{response.body}"
end
data = response.data
subscription = API::Subscription.new(@spire, data)
subscription = API::Subscription.new(@api, data)
if subscription_name
subscriptions[data["name"]] = subscription
end
Expand All @@ -305,7 +305,7 @@ def channels!
channels_data = response.data
@channels = {}
channels_data.each do |name, properties|
@channels[name] = API::Channel.new(@spire, properties)
@channels[name] = API::Channel.new(@api, properties)
end
@channels
end
Expand All @@ -325,7 +325,7 @@ def subscriptions!
end
@subscriptions = {}
response.data.each do |name, properties|
@subscriptions[name] = API::Subscription.new(@spire, properties)
@subscriptions[name] = API::Subscription.new(@api, properties)
end
@subscriptions
end
Expand All @@ -336,7 +336,7 @@ def get_channel(name)
raise "Error finding channel with name #{name}: (#{response.status}) #{response.body}"
end
properties = response.data[name]
channel = API::Channel.new(@spire, properties)
channel = API::Channel.new(@api, properties)
@channels[name] = channel if @channels.is_a?(Hash)
channel
end
Expand All @@ -347,7 +347,7 @@ def get_subscription(name)
raise "Error finding subscription with name #{name}: (#{response.status}) #{response.body}"
end
properties = response.data[name]
sub = API::Subscription.new(@spire, properties)
sub = API::Subscription.new(@api, properties)
@subscriptions[name] = sub if @subscriptions.is_a?(Hash)
sub
end
Expand Down
24 changes: 8 additions & 16 deletions lib/spire/api/channel.rb
Expand Up @@ -29,7 +29,7 @@ def resource_name
:url => url,
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("subscriptions"),
"Accept" => @api.mediaType("subscriptions"),
}
}
end
Expand All @@ -41,8 +41,8 @@ def resource_name
:body => string,
:headers => {
"Authorization" => "Capability #{@capabilities["publish"]}",
"Accept" => @spire.mediaType("message"),
"Content-Type" => @spire.mediaType("message")
"Accept" => @api.mediaType("message"),
"Content-Type" => @api.mediaType("message")
}
}
end
Expand All @@ -57,8 +57,8 @@ def resource_name
:body => {:name => name}.to_json,
:headers => {
"Authorization" => "Capability #{capability}",
"Accept" => @spire.mediaType("subscription"),
"Content-Type" => @spire.mediaType("subscription")
"Accept" => @api.mediaType("subscription"),
"Content-Type" => @api.mediaType("subscription")
}
}
end
Expand All @@ -74,7 +74,7 @@ def subscriptions!
end
@subscriptions = {}
response.data.each do |name, properties|
@subscriptions[name] = API::Subscription.new(@spire, properties)
@subscriptions[name] = API::Subscription.new(@api, properties)
end
@subscriptions
end
Expand All @@ -85,24 +85,16 @@ def publish(content)
unless response.status == 201
raise "Error publishing to #{self.class.name}: (#{response.status}) #{response.body}"
end
API::Message.new(@spire, response.data)
API::Message.new(@api, response.data)
end

def subscribe(name = nil)
response = request(:subscribe, name)
unless response.status == 201
raise "Error creating subscription for #{self.name}: (#{response.status}) #{response.body}"
end
API::Subscription.new(@spire, response.data)
API::Subscription.new(@api, response.data)
end

# Obtain a subscription for the channel
# @param [String] subscription_name Name of the subscription
# @return [Subscription]
def subscribe(subscription_name = nil)
@spire.subscribe(subscription_name, properties["name"])
end

end
end
end
4 changes: 2 additions & 2 deletions lib/spire/api/notification.rb
Expand Up @@ -16,8 +16,8 @@ def resource_name
}.to_json,
:headers => {
"Authorization" => "Capability #{@capabilities["push"]}",
"Accept" => @spire.mediaType("notification"),
"Content-Type" => @spire.mediaType("notification")
"Accept" => @api.mediaType("notification"),
"Content-Type" => @api.mediaType("notification")
}
}
end
Expand Down
8 changes: 4 additions & 4 deletions lib/spire/api/resource.rb
Expand Up @@ -41,9 +41,9 @@ class Resource

attr_reader :url, :properties, :capabilities, :capability

def initialize(spire, data)
@spire = spire
@client = spire.client
def initialize(api, data)
@api = api
@client = api.client
@url = data["url"]
@capabilities = data["capabilities"]
@properties = data
Expand Down Expand Up @@ -92,7 +92,7 @@ def delete
end

def schema
@spire.schema[resource_name]
@api.schema[resource_name]
end

def media_type
Expand Down

0 comments on commit ef271df

Please sign in to comment.