This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_importer.rb
126 lines (96 loc) · 2.87 KB
/
application_importer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class ApplicationImporter
attr_accessor :options
def logger
@logger = Rails.logger
end
def initialize(options={})
self.options = HashWithIndifferentAccess.new(options)
end
def source_name
raise NotImplementedError.new("Subclass must implement source_name")
end
def distributor_tag
ENV['DISTRIBUTOR_TAG'] || 'PRX'
end
def import(options)
self.options.merge!(options)
end
def pmp
@pmp ||= PMP::Client.new(client_id: pmp_client_id, client_secret: pmp_client_secret, endpoint: pmp_endpoint)
end
def reset_pmp
@pmp = nil
end
def pmp_client_id
options[:pmp_client_id] || ENV['PMP_CLIENT_ID']
end
def pmp_client_secret
options[:pmp_client_secret] || ENV['PMP_CLIENT_SECRET']
end
def pmp_endpoint
options[:pmp_endpoint] || ENV['PMP_ENDPOINT'] || 'https://api.pmp.io/'
end
def pmp_url(*path)
URI.join(pmp_endpoint, *path.collect(&:to_s).join('/')).to_s
end
def pmp_doc_by_guid(guid)
response = pmp.query["urn:collectiondoc:hreftpl:docs"].where(guid: guid).get
response = nil if (response.response.raw['status'] == '404')
response = nil if response.response.raw['body'].blank?
response
end
def pmp_doc_find_first(conditions)
response = pmp.query["urn:collectiondoc:query:docs"].where(conditions).get
response.items.first
end
def retrieve_doc(type, url)
doc = nil
guid = PMPGuidMapping.find_guid(source_name, type, url)
if guid
doc = pmp_doc_by_guid(guid)
end
# no guid yet? look to see if a doc has the right tag
if !doc
itag = tag_for_url(source_name, url)
doc = pmp_doc_find_first(itag: itag)
if doc && !guid
PMPGuidMapping.create(source_name: source_name, source_type: type, source_id: url, guid: doc.guid)
end
end
doc
end
def set_standard_tags(tag_doc, url)
add_itag_to_doc(tag_doc, "#{distributor_tag.downcase}_test") unless Rails.env.production?
add_itag_to_doc(tag_doc, tag_for_url(source_name, url))
end
def tag_for_url(source, url)
"#{source}:#{url}"
end
def pmp_doc_profile(doc)
# puts "doc.links['profile']: #{doc.links['profile'].inspect}"
if profile_link = doc.links['profile']
profile_link = profile_link.is_a?(Array) ? profile_link.first : profile_link
profile_link.href.split('/').last.downcase
end
end
def add_tag_to_doc(doc, tag)
tag = tag.strip
doc.tags ||= []
return if doc.tags.include?(tag)
doc.tags << tag
end
def add_itag_to_doc(doc, tag)
tag = tag.strip
doc.itags ||= []
return if doc.itags.include?(tag)
doc.itags << tag
end
def add_link_to_doc(doc, rel, link_attrs)
doc.links[rel] ||= []
return if Array(doc.links[rel]).detect{|l| l[:href] == link_attrs[:href]}
doc.links[rel] << PMP::Link.new(link_attrs)
end
def strip_tags(text)
ActionController::Base.helpers.strip_tags(text)
end
end