Class: GreenStripes::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/greenstripes.rb,
ext/greenstripes/greenstripes.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Session

:nodoc:



36
37
# File 'lib/greenstripes.rb', line 36

def initialize(*args) # :nodoc:
end

Class Method Details

.new(application_key, user_agent, cache_location, settings_location) ⇒ nil

Returns a new session.

Returns:

  • (nil)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/greenstripes/greenstripes.c', line 81

static VALUE session_new(VALUE klass, VALUE application_key, VALUE user_agent, VALUE cache_location, VALUE settings_location)
{
  // TODO: session callbacks should not be hardcoded
  sp_session_callbacks callbacks =
  {
    logged_in_callback,
    logged_out_callback,
    metadata_updated_callback,
    connection_error_callback,
    message_to_user_callback,
    notify_main_thread_callback,
    NULL, // music_delivery
    NULL, // play_token_lost
    log_message_callback
  };

  sp_session_config config =
  {
    SPOTIFY_API_VERSION,
    StringValuePtr(cache_location),
    StringValuePtr(settings_location),
    RSTRING_PTR(application_key),
    RSTRING_LEN(application_key),
    StringValuePtr(user_agent),
    &callbacks,
    NULL
  };

  sp_session *session = NULL;
  sp_error error = sp_session_init(&config, &session);

  // TODO: handle error in a better way?
  if(error != SP_ERROR_OK)
  {
    fprintf(stderr, "error: %s\n", sp_error_message(error));
    return Qnil;
  }

  VALUE session_value = Data_Wrap_Struct(class_session, NULL, NULL, session);
  VALUE argv[4] = {application_key, user_agent, cache_location, settings_location};
  rb_obj_call_init(session_value, 4, argv);
  return session_value;
}

Instance Method Details

#connection_stateObject

Returns one of the constants defined in ConnectionState.



159
160
161
162
163
164
165
# File 'ext/greenstripes/greenstripes.c', line 159

static VALUE session_connection_state(VALUE self)
{
  sp_session *session;
  Data_Get_Struct(self, sp_session, session);
  int state = sp_session_connectionstate(session);
  return INT2FIX(state);
}

#login(username, password) ⇒ Object

Attempts to log in with the given username and password. Returns one of the constants defined in Error.



131
132
133
134
135
136
137
138
# File 'ext/greenstripes/greenstripes.c', line 131

static VALUE session_login(VALUE self, VALUE username, VALUE password)
{
  sp_session *session;
  Data_Get_Struct(self, sp_session, session);
  sp_error error = sp_session_login(session, StringValuePtr(username), StringValuePtr(password));
  // TODO: throw an exception instead?
  return INT2FIX(error);
}

#logoutObject

Attempts to log out. Returns one of the constants defined in Error.



145
146
147
148
149
150
151
152
# File 'ext/greenstripes/greenstripes.c', line 145

static VALUE session_logout(VALUE self)
{
  sp_session *session;
  Data_Get_Struct(self, sp_session, session);
  sp_error error = sp_session_logout(session);
  // TODO: throw an exception instead?
  return INT2FIX(error);
}

#playlist_containernil

Returns the playlist container for the session.

Returns:

  • (nil)


201
202
203
204
205
206
207
208
# File 'ext/greenstripes/greenstripes.c', line 201

static VALUE session_playlist_container(VALUE self)
{
  sp_session *s;
  Data_Get_Struct(self, sp_session, s);
  sp_playlistcontainer *pc = NULL;
  pc = sp_session_playlistcontainer(s);
  return pc ? Data_Wrap_Struct(class_playlist_container, NULL, NULL, pc) : Qnil;
}

#process_eventsFixnum

Makes the session process any pending events. Returns the number of milliseconds until you should call this method again.

Returns:

  • (Fixnum)


173
174
175
176
177
178
179
180
# File 'ext/greenstripes/greenstripes.c', line 173

static VALUE session_process_events(VALUE self)
{
  sp_session *session;
  Data_Get_Struct(self, sp_session, session);
  int next_timeout;
  sp_session_process_events(session, &next_timeout);
  return INT2FIX(next_timeout);
}


39
40
41
# File 'lib/greenstripes.rb', line 39

def to_link
  Link.new(self)
end

#usernil

Returns the user for the session.

Returns:

  • (nil)


187
188
189
190
191
192
193
194
# File 'ext/greenstripes/greenstripes.c', line 187

static VALUE session_user(VALUE self)
{
  sp_session *s;
  Data_Get_Struct(self, sp_session, s);
  sp_user *user = NULL;
  user = sp_session_user(s);
  return user ? Data_Wrap_Struct(class_user, NULL, NULL, user) : Qnil;
}