Class: Spire::API

Inherits:
Object
  • Object
show all
Includes:
Requestable
Defined in:
lib/spire/api.rb,
lib/spire/api/event.rb,
lib/spire/api/member.rb,
lib/spire/api/account.rb,
lib/spire/api/channel.rb,
lib/spire/api/session.rb,
lib/spire/api/resource.rb,
lib/spire/api/application.rb,
lib/spire/api/requestable.rb,
lib/spire/api/notification.rb,
lib/spire/api/subscription.rb

Defined Under Namespace

Modules: Requestable Classes: Account, Application, Channel, Event, Join, Member, Message, Notification, Part, Resource, Session, Subscription

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Requestable

included

Constructor Details

#initialize(url = "https://api.spire.io", spire = nil, options = {}) ⇒ API

Returns a new instance of API.



35
36
37
38
39
40
# File 'lib/spire/api.rb', line 35

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

Instance Attribute Details

#clientObject (readonly)

HTTP client.



33
34
35
# File 'lib/spire/api.rb', line 33

def client
  @client
end

#descriptionObject (readonly)

Description of the Spire.io API from discovery.



33
# File 'lib/spire/api.rb', line 33

attr_reader :client, :description, :schema

#schemaObject (readonly)

Returns the value of attribute schema.



33
# File 'lib/spire/api.rb', line 33

attr_reader :client, :description, :schema

Class Method Details

.deserialize(string) ⇒ Object



23
24
25
# File 'lib/spire/api.rb', line 23

def self.deserialize(string)
  JSON.parse(string, :symbolize_names => false)
end

Instance Method Details

#create_account(info) ⇒ Object

Register for a new spire account, and authenticates as the newly created account

Parameters:

  • :email (String)

    Email address of new account

  • :password (String)

    Password of new account



151
152
153
154
155
156
157
158
# File 'lib/spire/api.rb', line 151

def (info)
  response = request(:create_account, info)
  if response.status != 201
    raise "Error attempting to register: (#{response.status}) #{response.body}"
  end
  session_data = response.data
  API::Session.new(self, session_data)
end

#create_session(secret) ⇒ Object



133
134
135
136
137
138
# File 'lib/spire/api.rb', line 133

def create_session(secret)
  response = request(:create_session, secret)
  raise "Error starting a secret-based session" if response.status != 201
  session_data = response.data
  API::Session.new(self, session_data)
end

#discoverObject



122
123
124
125
126
127
# File 'lib/spire/api.rb', line 122

def discover
  response = request(:discover)
  raise "Error during discovery: #{response.status}" if response.status != 200
  @description = response.data
  @schema = @description["schema"][@version]
end

#get_application(application_key) ⇒ Object

Gets an application resource from a key without requiring any authentication

Parameters:

  • application_key (String)

    The application key



170
171
172
173
174
175
176
# File 'lib/spire/api.rb', line 170

def get_application(application_key)
  response = request(:get_application, application_key)
  if response.status != 200
    raise "Error attempting to retrieve application (#{response.status}) #{response.body}"
  end
  API::Application.new(self, response.data)
end

#login(login, password) ⇒ Object

Authenticates a session using a login and password



141
142
143
144
145
146
# File 'lib/spire/api.rb', line 141

def (, password)
  response = request(:login, , password)
  raise "Error attemping to login:  (#{response.status}) #{response.body}" if response.status != 201
  session_data = response.data
  API::Session.new(self, session_data)
end

#mediaType(name) ⇒ Object



129
130
131
# File 'lib/spire/api.rb', line 129

def mediaType(name)
  schema[name]["mediaType"]
end

#password_reset_request(email) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/spire/api.rb', line 160

def password_reset_request(email)
  response = request(:password_reset)
  unless response.status == 202
    raise "Error requesting password reset: (#{response.status}) #{response.body}"
  end
  response
end