Class: Iknow::Item

Inherits:
Base
  • Object
show all
Defined in:
lib/iknow/model/item.rb

Defined Under Namespace

Classes: Cue, Response

Constant Summary collapse

ATTRIBUTES =
[:sentences, :responses, :cue, :id, :list]
READONLY_ATTRIBUTES =
[:sentences, :responses, :cue, :id]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attributes, #attributes, deserialize, #deserialize

Constructor Details

#initialize(params = {}) ⇒ Item

Returns a new instance of Item.



62
63
64
65
66
67
68
69
# File 'lib/iknow/model/item.rb', line 62

def initialize(params = {})
  params[:responses] = [params[:response]] if params[:response]
  @id        = params[:id].to_i
  @list      = params[:list]
  @cue       = self.deserialize(params[:cue], :as => Iknow::Item::Cue)
  @responses = self.deserialize(params[:responses], :as => Iknow::Item::Response)
  @sentences = self.deserialize(params[:sentences], :as => Iknow::Sentence)
end

Class Method Details

.create(iknow_auth, params = {}) ⇒ Object



58
59
60
# File 'lib/iknow/model/item.rb', line 58

def self.create(iknow_auth, params = {})
  self.new(params).save(iknow_auth)
end

.extract(text, params = {}) ⇒ Object



52
53
54
55
56
# File 'lib/iknow/model/item.rb', line 52

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

.find(item_id, params = {}) ⇒ Object



40
41
42
43
44
# File 'lib/iknow/model/item.rb', line 40

def self.find(item_id, params = {})
  params[:id] = item_id
  hash = Iknow::RestClient::Item.find(params)
  self.deserialize(hash)
end

.matching(keyword, params = {}) ⇒ Object



46
47
48
49
50
# File 'lib/iknow/model/item.rb', line 46

def self.matching(keyword, params = {})
  params[:keyword] = keyword
  hash = Iknow::RestClient::Item.matching(params)
  self.deserialize(hash) || []
end

.recent(params = {}) ⇒ Object



35
36
37
38
# File 'lib/iknow/model/item.rb', line 35

def self.recent(params = {})
  hash = Iknow::RestClient::Item.recent(params)
  self.deserialize(hash) || []
end

Instance Method Details

#add_image(iknow_auth, params) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/iknow/model/item.rb', line 80

def add_image(iknow_auth, params)
  post_params = if params.is_a?(String)
    { 'image[url]' => params }
  else
    image_params = { 
      'image[url]'     => params[:url],
      'image[list_id]' => params[:list_id]
    }
    if params[:attribution]
      attribution_params = { 
        'attribution[media_entity]'           => params[:attribution][:media_entity],
        'attribution[author]'                 => params[:attribution][:media_entity],
        'attribution[author_url]'             => params[:attribution][:media_entity],
        'attribution[attribution_license_id]' => params[:attribution][:media_entity]
      }
      image_params.merge(attribution_params)
    else
      image_params
    end
  end
  Iknow::RestClient::Item.add_image(iknow_auth, post_params.merge(:id => self.id))
end

#add_sound(iknow_auth, params) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/iknow/model/item.rb', line 103

def add_sound(iknow_auth, params)
  post_params = if params.is_a?(String)
    { 'sound[url]' => params }
  else
    sound_params = {
      'sound[url]' => params[:url],
      'sound[list_id]' => params[:list_id]
    }
    if params[:attribution]
      attribution_params = { 
        'attribution[media_entity]'           => params[:attribution][:media_entity],
        'attribution[author]'                 => params[:attribution][:media_entity],
        'attribution[author_url]'             => params[:attribution][:media_entity],
        'attribution[attribution_license_id]' => params[:attribution][:media_entity]
      }
      sound_params.merge(attribution_params)
    else
      sound_params
    end
  end
  Iknow::RestClient::Item.add_sound(iknow_auth, post_params.merge(:id => self.id))
end

#add_tags(iknow_auth, *tags) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/iknow/model/item.rb', line 126

def add_tags(iknow_auth, *tags)
  post_params = {}
  tags.each_with_index do |tag, idx|
    if tag.is_a?(String)
      post_params["semantic_tags[#{idx}][name]"] = tag
    else
      post_params["semantic_tags[#{idx}][name]"] = tag[:name]
      post_params["semantic_tags[#{idx}][disambiguation]"] = tag[:disambiguation]
    end
  end
  Iknow::RestClient::Item.add_tags(iknow_auth, post_params.merge(:id => self.id))
end

#save(iknow_auth) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/iknow/model/item.rb', line 71

def save(iknow_auth)
  begin
    item_id = Iknow::RestClient::Item.create(iknow_auth, self.to_post_data)
  rescue
    return false
  end
  Iknow::Item.find(item_id)
end