Class: Bookmark
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Bookmark
- Defined in:
- app/models/bookmark.rb
Class Method Summary collapse
- .get_canonical_url(url) ⇒ Object
- .get_title(string) ⇒ Object
- .get_title_from_url(url) ⇒ Object
- .manifestations_count(start_date, end_date, manifestation) ⇒ Object
Instance Method Summary collapse
- #already_bookmarked? ⇒ Boolean
- #bookmarkable_url? ⇒ Boolean
- #create_manifestation ⇒ Object
- #get_manifestation ⇒ Object
- #get_title ⇒ Object
- #my_host_resource ⇒ Object
- #replace_space_in_tags ⇒ Object
- #save_tagger ⇒ Object
- #shelved? ⇒ Boolean
- #tag_index! ⇒ Object
Class Method Details
.get_canonical_url(url) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'app/models/bookmark.rb', line 96 def self.get_canonical_url(url) doc = Nokogiri::HTML(Faraday.get(url).body) canonical_url = doc.search("/html/head/link[@rel='canonical']").first['href'] # TODO: URLを相対指定している時 Addressable::URI.parse(canonical_url).normalize.to_s rescue nil end |
.get_title(string) ⇒ Object
55 56 57 |
# File 'app/models/bookmark.rb', line 55 def self.get_title(string) CGI.unescape(string).strip unless string.nil? end |
.get_title_from_url(url) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/bookmark.rb', line 68 def self.get_title_from_url(url) return if url.blank? return unless Addressable::URI.parse(url).host if manifestation_id = url.bookmarkable_id self.manifestation = Manifestation.find(manifestation_id) return manifestation.original_title end unless manifestation normalized_url = Addressable::URI.parse(url).normalize.to_s doc = Nokogiri::HTML(Faraday.get(normalized_url).body) # TODO: 日本語以外 # charsets = ['iso-2022-jp', 'euc-jp', 'shift_jis', 'iso-8859-1'] # if charsets.include?(page.charset.downcase) title = NKF.nkf('-w', CGI.unescapeHTML((doc.at("title").inner_text))).to_s.gsub(/\r\n|\r|\n/, '').gsub(/\s+/, ' ').strip if title.blank? title = url end # else # title = (doc/"title").inner_text # end title end rescue OpenURI::HTTPError # TODO: 404などの場合の処理 raise "unable to access: #{url}" # nil end |
.manifestations_count(start_date, end_date, manifestation) ⇒ Object
169 170 171 172 173 174 175 |
# File 'app/models/bookmark.rb', line 169 def self.manifestations_count(start_date, end_date, manifestation) if manifestation self.bookmarked(start_date, end_date).where(manifestation_id: manifestation.id).count else 0 end end |
Instance Method Details
#already_bookmarked? ⇒ Boolean
131 132 133 134 135 136 137 |
# File 'app/models/bookmark.rb', line 131 def already_bookmarked? if manifestation if manifestation.bookmarked?(user) errors[:base] << 'already_bookmarked' end end end |
#bookmarkable_url? ⇒ Boolean
111 112 113 114 115 116 117 118 119 120 |
# File 'app/models/bookmark.rb', line 111 def bookmarkable_url? if url.try(:my_host?) unless url.try(:bookmarkable_id) errors[:base] << I18n.t('bookmark.not_our_holding') end unless my_host_resource errors[:base] << I18n.t('bookmark.not_our_holding') end end end |
#create_manifestation ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/models/bookmark.rb', line 139 def create_manifestation manifestation = get_manifestation if manifestation self.manifestation_id = manifestation.id return end manifestation = Manifestation.new(access_address: url) manifestation.carrier_type = CarrierType.where(name: 'file').first if title.present? manifestation.original_title = title else manifestation.original_title = self.get_title end Manifestation.transaction do manifestation.save self.manifestation = manifestation item = Item.new item.shelf = Shelf.web item.manifestation = manifestation if defined?(EnjuCirculation) item.circulation_status = CirculationStatus.where(name: 'Not Available').first end item.save! if defined?(EnjuCirculation) item.use_restriction = UseRestriction.where(name: 'Not For Loan').first end end end |
#get_manifestation ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'app/models/bookmark.rb', line 122 def get_manifestation # 自館のページをブックマークする場合 if url.try(:my_host?) manifestation = self.my_host_resource else manifestation = Manifestation.where(access_address: url).first if url.present? end end |
#get_title ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'app/models/bookmark.rb', line 59 def get_title return if url.blank? if url.my_host? my_host_resource.original_title else Bookmark.get_title_from_url(url) end end |
#my_host_resource ⇒ Object
105 106 107 108 109 |
# File 'app/models/bookmark.rb', line 105 def my_host_resource if url.bookmarkable_id manifestation = Manifestation.find(url.bookmarkable_id) end end |
#replace_space_in_tags ⇒ Object
39 40 41 42 |
# File 'app/models/bookmark.rb', line 39 def # タグに含まれている全角スペースを除去する self.tag_list = tag_list.map{|tag| tag.gsub(' ', ' ').gsub(' ', ', ')} end |
#save_tagger ⇒ Object
44 45 46 47 48 49 |
# File 'app/models/bookmark.rb', line 44 def save_tagger taggings.each do |tagging| tagging.tagger = user tagging.save(validate: false) end end |
#shelved? ⇒ Boolean
51 52 53 |
# File 'app/models/bookmark.rb', line 51 def shelved? true if manifestation.items.on_web.first end |
#tag_index! ⇒ Object
177 178 179 180 181 182 |
# File 'app/models/bookmark.rb', line 177 def tag_index! manifestation.reload manifestation.index taggings.map{|tagging| Tag.find(tagging.tag_id).index} Sunspot.commit end |