Skip to content

Commit

Permalink
refactoring and add pure_ruby example
Browse files Browse the repository at this point in the history
  • Loading branch information
nov committed Oct 16, 2008
1 parent f4dc241 commit 402a162
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 123 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
*.gem
*.sh
43 changes: 43 additions & 0 deletions examples/pure_ruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'rubygems'
require 'iknow'

Iknow::Config.init do |conf|
conf.api_key = "" # Set your iKnow! API key, here.
conf.host = "api.iknow.co.jp"
end

please_get_api_key =<<EOS
This example needs your own iKnow! API key.
(for only Iknow::Item.extract example)
You can get iKnow! API key at iKnow! Developers.
iKnow! Developers (http://developer.iknow.co.jp/)
Thanks!
EOS

raise ArgumentError.new(please_get_api_key) if Iknow::Config.instance.api_key == ''

## User API
@user = Iknow::User.find('matake')
@user.items
@user.lists
@user.friends
@user.study_results
@matchied_users = Iknow::User.matching('matake')

# ## List API
@recent_lists = Iknow::List.recent
@matchied_lists = Iknow::List.matching("遺伝的アルゴリズム")
@ga_list.first.items
@ga_list.first.sentences

# ## Item API
@recent_items = Iknow::Item.recent
@matchied_items = Iknow::Item.matching('record')
@items = Iknow::Item.extract("sometimes, often, electrical")
@items.first.sentences

## Sentence API
@recent_sentences = Iknow::Sentence.recent
@matchied_sentences = Iknow::Sentence.matching('record')
4 changes: 2 additions & 2 deletions iknow.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["nov"]
s.date = %q{2008-10-15}
s.date = %q{2008-10-16}
s.description = %q{A rubygem for iKnow! APIs}
s.email = %q{developer@iknow.co.jp}
s.extra_rdoc_files = ["README", "ChangeLog"]
s.files = ["README", "ChangeLog", "Rakefile", "test/iknow_test.rb", "test/test_helper.rb", "lib/ext", "lib/ext/hash.rb", "lib/iknow", "lib/iknow/core", "lib/iknow/core/config.rb", "lib/iknow/core/version.rb", "lib/iknow/core.rb", "lib/iknow/model", "lib/iknow/model/base.rb", "lib/iknow/model/item.rb", "lib/iknow/model/list.rb", "lib/iknow/model/sentence.rb", "lib/iknow/model/user.rb", "lib/iknow/model.rb", "lib/iknow/rest_client", "lib/iknow/rest_client/base.rb", "lib/iknow/rest_client/item.rb", "lib/iknow/rest_client/list.rb", "lib/iknow/rest_client/sentence.rb", "lib/iknow/rest_client/user.rb", "lib/iknow/rest_client.rb", "lib/iknow.rb"]
s.files = ["README", "ChangeLog", "Rakefile", "test/iknow_test.rb", "test/test_helper.rb", "lib/ext", "lib/ext/hash.rb", "lib/iknow", "lib/iknow/core", "lib/iknow/core/config.rb", "lib/iknow/core/version.rb", "lib/iknow/core.rb", "lib/iknow/model", "lib/iknow/model/base.rb", "lib/iknow/model/item.rb", "lib/iknow/model/list.rb", "lib/iknow/model/sentence.rb", "lib/iknow/model/user.rb", "lib/iknow/model.rb", "lib/iknow/rest_client", "lib/iknow/rest_client/base.rb", "lib/iknow/rest_client/item.rb", "lib/iknow/rest_client/list.rb", "lib/iknow/rest_client/sentence.rb", "lib/iknow/rest_client/user.rb", "lib/iknow/rest_client.rb", "lib/iknow.rb", "examples/pure_ruby.rb"]
s.has_rdoc = true
s.homepage = %q{http://iknow.rubyforge.org}
s.rdoc_options = ["--title", "iknow documentation", "--charset", "utf-8", "--opname", "index.html", "--line-numbers", "--main", "README", "--inline-source", "--exclude", "^(examples|extras)/"]
Expand Down
11 changes: 11 additions & 0 deletions lib/iknow/model/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
class Iknow::Base
# TODO: define self.attributes
def self.attributes; self::ATTRIBUTES end
def attributes; self.class.attributes end

def self.deserialize(response, params = {})
klass = params[:as] ? params[:as] : self
response.is_a?(Array) ? response.inject([]) { |results, params| results << klass.new(params) } : klass.new(response)
end
def deserialize(response, params = {})
self.class.deserialize(response, params)
end

end
50 changes: 27 additions & 23 deletions lib/iknow/model/item.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
class Iknow::Item
attr_reader :sentences, :response, :cue, :id

class Iknow::Item < Iknow::Base
ATTRIBUTES = [:sentences, :response, :cue, :id]
attr_reader *ATTRIBUTES

class Response
attr_reader :text
ATTRIBUTES = [:text]
attr_reader *ATTRIBUTES

def initialize(params = {})
@text = params['text']
end
end

class Cue
attr_accessor :sound, :part_of_speech
attr_reader :text
ATTRIBUTES = [:sound, :part_of_speech, :text]
NOT_WRITABLE_ATTRIBUTES = [:text]
attr_accessor *(ATTRIBUTES - NOT_WRITABLE_ATTRIBUTES)
attr_reader *NOT_WRITABLE_ATTRIBUTES

def initialize(params = {})
@text = params['text']
@sound = params['sound']
@image = params['part_of_speech']
end
end

def self.recent(params = {})
responses = Iknow::RestClient::Item.recent(params)
items = []
responses.each do |response|
items << Iknow::Item.new(response)
end
items
response = Iknow::RestClient::Item.recent(params)
self.deserialize(response)
end

def self.matching(keyword, params = {})
params[:keyword] = keyword
responses = Iknow::RestClient::Item.matching(params)
items = []
responses.each do |response|
items << Iknow::Item.new(response)
end
items
response = Iknow::RestClient::Item.matching(params)
self.deserialize(response)
end

def self.extract(text, params = {})
params[:text] = text
response = Iknow::RestClient::Item.extract(params)
self.deserialize(response)
end

def initialize(params = {})
@id = params['id'].to_i
@sentences = []
Expand All @@ -46,5 +50,5 @@ def initialize(params = {})
@response = params['response'] ? Iknow::Item::Response.new(params['response']) : nil
@cue = Iknow::Item::Cue.new(params['cue'])
end

end
43 changes: 17 additions & 26 deletions lib/iknow/model/list.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
class Iknow::List < Iknow::Base
attr_accessor :title, :description, :link,
ATTRIBUTES = [:list_id, :title, :description, :link,
:language, :translation_language, :list_type, :transcript, :embed,
:tags, :media_entry, :author, :author_url, :attribution_license_id
attr_reader :list_id
:tags, :media_entry, :author, :author_url, :attribution_license_id]
NOT_WRITABLE_ATTRIBUTES = [:list_id]
attr_accessor *(ATTRIBUTES - NOT_WRITABLE_ATTRIBUTES)
attr_reader *NOT_WRITABLE_ATTRIBUTES

def self.recent(params = {})
responses = Iknow::RestClient::List.recent(params)
lists = []
responses.each do |response|
lists << Iknow::List.new(response)
end
lists
response = Iknow::RestClient::List.recent(params)
self.deserialize(response)
end

def self.matching(keyword, params = {})
params[:keyword] = keyword
responses = Iknow::RestClient::List.matching(params)
lists = []
responses.each do |response|
lists << Iknow::List.new(response)
end
lists
response = Iknow::RestClient::List.matching(params)
self.deserialize(response)
end

def self.create(params = {})
Expand All @@ -33,25 +27,22 @@ def initialize(params = {})
@title = params[:title] || params['title']
@description = params[:description] || params['description']
@link = params[:link] || params['link']
@items, @sentences = [], []
end

def items(params = {})
return @items unless @items.empty?
return @items if @items

responses = Iknow::RestClient::List.items(params.merge(:id => self.list_id))
responses.each do |item|
@items << Iknow::Item.new(item)
end
response = Iknow::RestClient::List.items(params.merge(:id => self.list_id))
@items = self.deserialize(response, :as => Iknow::Item)
@items
end

def sentences(params = {})
return @sentences unless @sentences.empty?
return @sentences if @sentences

responses = Iknow::RestClient::List.sentences(params.merge(:id => self.list_id))
responses.each do |sentence|
@sentences << Iknow::Sentence.new(sentence)
end
response = Iknow::RestClient::List.sentences(params.merge(:id => self.list_id))
@sentences = self.deserialize(response, :as => Iknow::Sentence)
@sentences
end

def save!
Expand Down
22 changes: 8 additions & 14 deletions lib/iknow/model/sentence.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
class Iknow::Sentence < Iknow::Base
attr_accessor :sound, :image
attr_reader :text
ATTRIBUTES = [:sound, :image, :text]
WRITABLE_ATTRIBUTES = [:sound, :image]
attr_accessor *WRITABLE_ATTRIBUTES
attr_reader *(ATTRIBUTES - WRITABLE_ATTRIBUTES)

def self.recent(params = {})
responses = Iknow::RestClient::Sentence.recent(params)
sentences = []
responses.each do |response|
sentences << Iknow::Sentence.new(response)
end
sentences
response = Iknow::RestClient::Sentence.recent(params)
self.deserialize(response)
end

def self.matching(keyword, params = {})
params[:keyword] = keyword
responses = Iknow::RestClient::Sentence.matching(params)
sentences = []
responses.each do |response|
sentences << Iknow::Sentence.new(response)
end
sentences
response = Iknow::RestClient::Sentence.matching(params)
self.deserialize(response)
end

def initialize(params = {})
Expand Down
90 changes: 39 additions & 51 deletions lib/iknow/model/user.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class Iknow::User < Iknow::Base
attr_reader :username, :profile

class Profile
attr_reader :name, :gender, :birthday, :description,
:blog_url, :profile_url, :foaf_url, :icon_url
ATTRIBUTES = [:username, :profile]
attr_reader *ATTRIBUTES

class Profile < Iknow::User
ATTRIBUTES = [:name, :gender, :birthday, :description, :blog_url, :profile_url, :foaf_url, :icon_url]
attr_reader *ATTRIBUTES

def initialize(params = {})
@name = params['name']
@gender = params['gender']
Expand All @@ -15,9 +17,10 @@ def initialize(params = {})
@icon_url = params['icon_url']
end
end

class StudyResult
attr_reader :timestamp, :seconds, :totals, :seen, :completed, :date

class StudyResult < Iknow::User
ATTRIBUTES = [:timestamp, :seconds, :totals, :seen, :completed, :date]
attr_reader *ATTRIBUTES

def initialize(params = {})
@timestamp = (params['timestamp'].to_i rescue nil)
Expand All @@ -34,63 +37,48 @@ def initialize(params = {})
end

def self.find(username)
response = Iknow::RestClient::User.show(:username => username)
self.new(response)
response = Iknow::RestClient::User.find(:username => username)
self.deserialize(response)
end

def self.matching(keyword, params = {})
params[:keyword] = keyword
responses = Iknow::RestClient::User.matching(params)
users = []
responses.each do |response|
users << Iknow::User.new(response)
end
users
response = Iknow::RestClient::User.matching(params)
self.deserialize(response)
end

def initialize(params)
@profile = Profile.new(params['profile'])
@username = params['username']
@study_results, @items, @lists = [], [], []
end

def items(params = {})
return @items unless @items.empty?

responses = Iknow::RestClient::User.items(params.merge(:username => self.username))
responses.each do |item|
@items << Iknow::Item.new(item)
end
@items
return @items if @items

response = Iknow::RestClient::User.items(params.merge(:username => self.username))
self.deserialize(response, :as => Iknow::Item)
end

def lists(params = {})
return @lists unless @lists.empty?

responses = Iknow::RestClient::User.lists(params.merge(:username => self.username))
responses.each do |list|
@lists << Iknow::List.new(list)
end
@lists
return @lists if @lists

response = Iknow::RestClient::User.lists(params.merge(:username => self.username))
self.deserialize(response, :as => Iknow::List)
end


def friends(params = {})
return @friends if @friends

response = Iknow::RestClient::User.friends(params.merge(:username => self.username))
self.deserialize(response)
end

def study_results(params = {})
return @study_results unless @study_results.empty?
params[:application] = :iknow
return @study_results if @study_results

params[:application] ||= :iknow
response = Iknow::RestClient::User.study_results(params.merge(:username => self.username))
response['study_results'].each do |study_result|
@study_results << StudyResult.new(study_result)
end
@study_results.sort! {|a, b| a.timestamp <=> b.timestamp}
self.deserialize(response, :as => Iknow::User::StudyResult)
end

def method_missing(method, *args)
if profile.respond_to? method
profile.send(method)
else
super
end
end


end
Loading

0 comments on commit 402a162

Please sign in to comment.