Class: Iknow::Sentence

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

Constant Summary collapse

ATTRIBUTES =
[:sound, :image, :square_image, :text, :language, :id, :transliterations, :translations, :item, :list]
READONLY_ATTRIBUTES =
[:id]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attributes, #attributes, deserialize, #deserialize

Constructor Details

#initialize(params = {}) ⇒ Sentence

Returns a new instance of Sentence.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/iknow/model/sentence.rb', line 28

def initialize(params = {})
  params[:translations] = [params[:translation]] if params[:translation]
  params[:transliterations] = [params[:transliteration]] if params[:transliteration]
  @id               = params[:id]
  @item             = params[:item]
  @list             = params[:list]
  @sound            = params[:sound]
  @image            = params[:image]
  @square_image     = params[:square_image]
  @text             = params[:text]
  @language         = params[:language]
  @transliterations = params[:transliterations]
  @translations     = self.deserialize(params[:translations], :as => Iknow::Sentence)
end

Class Method Details

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



24
25
26
# File 'lib/iknow/model/sentence.rb', line 24

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

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



12
13
14
15
16
# File 'lib/iknow/model/sentence.rb', line 12

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

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



18
19
20
21
22
# File 'lib/iknow/model/sentence.rb', line 18

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

.recent(params = {}) ⇒ Object



7
8
9
10
# File 'lib/iknow/model/sentence.rb', line 7

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

Instance Method Details

#add_image(iknow_auth, params) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/iknow/model/sentence.rb', line 52

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::Sentence.add_image(iknow_auth, post_params.merge(:id => self.id))
end

#add_sound(iknow_auth, params) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/iknow/model/sentence.rb', line 75

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::Sentence.add_sound(iknow_auth, post_params.merge(:id => self.id))
end

#save(iknow_auth) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/iknow/model/sentence.rb', line 43

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