Skip to content

Commit

Permalink
wip, history is in different format
Browse files Browse the repository at this point in the history
  • Loading branch information
Kagetsuki committed Nov 30, 2015
1 parent 474c396 commit 5e02d80
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 21 deletions.
44 changes: 37 additions & 7 deletions lib/emojidex/service/collection.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
require_relative '../data/collection'
require_relative 'transactor'

module Emojidex
module Service
# a modified collection class for collections tied to the emojidex service
class Collection < Emojidex::Data::Collection
attr_reader :endpoint, :page
attr_reader :endpoint, :page, :limit, :detailed
@username
@token

def initialize(opts = {})
@emoji = {}
@emoji = opts[:emoji] || {}

@page = opts[:page] || 1
@username = opts[:username] || nil
@token = opts[:token] || nil

if (opts.key? :endpoint)
@endpoint = opts.endpoint;
@emoji = Transactor.get(endpoint, opts)
end
@endpoint = opts[:endpoint] || 'emoji'
@page = opts[:page] || 0
@limit = opts[:limit] || 50
@detailed = opts[:detailed] || false

more()
@emoji
end

def more()
@page += 1

opts = {page: @page, limit: @limit, detailed: @detailed}
opts[:username] = @username unless @username.nil?
opts[:auth_token] = @token unless @token.nil?

page_moji = Emojidex::Service::Transactor.get(@endpoint, opts)

if page_moji.is_a? Hash
if !page_moji.key? :emoji
@page -= 1 #reset page beacuse we failed
return {}
end

add_emoji(page_moji[:emoji])
return page_moji[:emoji]
end
puts "ARRAY #{page_moji}"
add_emoji(page_moji)
return page_moji
end
end
end
end
2 changes: 1 addition & 1 deletion lib/emojidex/service/transactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.get(endpoint, params = {})
"#{self.api_url}#{endpoint}", params)

begin
data = JSON.parse(response.body)
data = JSON.parse(response.body, symbolize_names: true)
rescue JSON::ParserError
return {}
end
Expand Down
45 changes: 35 additions & 10 deletions lib/emojidex/service/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../../emojidex'
require_relative 'transactor'
require_relative 'collection'

module Emojidex
module Service
Expand All @@ -20,15 +21,47 @@ def initialize(opts = {})
@premium_exp = nil
@pro_exp = nil
@status = :none
@history = Emojidex::Data::Collection.new
@favorites = Emojidex::Data::Collection.new
end

def login(user, password)
auth_response = Emojidex::Service::Transactor.get('users/authenticate',
{user: user, password: password})
_process_auth_response(auth_response)
end

def authorize(username, token)
auth_response = Emojidex::Service::Transactor.get('users/authenticate',
{username: username, token: token})
if auth_response['auth_status'] == 'verified'
_process_auth_response(auth_response)
end

def authorized?
@@auth_status_codes[@status]
end

def sync_favorites(limit = 50, detailed = true)
return false unless authorized?

@favorites = Emojidex::Service::Collection.new(
{endpoint: 'users/favorites', limit: limit, detailed: detailed,
username: @username, token: @token})
true
end

def sync_history(limit = 50, detailed = true)
return false unless authorized?

@history = Emojidex::Service::Collection.new(
{endpoint: 'users/history', limit: limit, detailed: detailed,
username: @username, token: @token})
true
end

private
def _process_auth_response(auth_response)
if auth_response[:auth_status] == 'verified'
@status = :verified
@username = auth_response[:auth_user]
@token = auth_response[:auth_token]
Expand All @@ -37,7 +70,7 @@ def authorize(username, token)
@pro_exp = auth_response[:pro_exp]
@premium_exp = auth_response[:premium_exp]
return true
elsif auth_response['auth_status'] == 'unverified'
elsif auth_response[:auth_status] == 'unverified'
@status = :unverified
else
@status = :failure
Expand All @@ -49,14 +82,6 @@ def authorize(username, token)
@premium_exp = nil
false
end

def authorized?
@@auth_status_codes[@status]
end

def sync_favorites()
#Transactor.get(
end
end
end
end
7 changes: 5 additions & 2 deletions spec/emojidex/service/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

describe Emojidex::Service::Collection do
describe '.initialize' do
it 'defaults to an empty collection' do
it 'defaults to the emoji index with 50 results per page undetailed' do
sc = Emojidex::Service::Collection.new
expect(sc.emoji.count).to eq 0
expect(sc.emoji.count).to eq 50
expect(sc.detailed).to be false
expect(sc.endpoint).to eq 'emoji'
expect(sc.page).to eq 1
end
end
end
29 changes: 28 additions & 1 deletion spec/emojidex/service/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,41 @@
expect(user.authorized?).to be false
end

it 'fails authorization with a bad token, passes with a good token' do
it 'fails authorization with a bad token' do
user = Emojidex::Service::User.new
expect(user.authorize('test', '12345')).to be false
expect(user.status).to eq :unverified
end

it 'authorizes with a good token' do
user = Emojidex::Service::User.new
expect(user.authorize('test',
'1798909355d57c9a93e3b82d275594e7c7c000db05021138')).to be true
expect(user.status).to eq :verified
end

it 'fails to sync history when unauthorized' do
user = Emojidex::Service::User.new
expect(user.sync_history).to be false
end

it 'syncs history when authorized' do
user = Emojidex::Service::User.new
user.authorize('test', '1798909355d57c9a93e3b82d275594e7c7c000db05021138')
expect(user.sync_history).to be true
expect(user.history.emoji.length > 0).to be true
end

it 'fails to sync favorites when unauthorized' do
user = Emojidex::Service::User.new
expect(user.sync_favorites).to be false
end

it 'syncs history when authorized' do
user = Emojidex::Service::User.new
user.authorize('test', '1798909355d57c9a93e3b82d275594e7c7c000db05021138')
expect(user.sync_favorites).to be true
expect(user.favorites.emoji.length > 0).to be true
end
end
end

0 comments on commit 5e02d80

Please sign in to comment.