Class: Economic::BaseRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/economic/base_repo.rb

Constant Summary collapse

URL =
'https://restapi.e-conomic.com/'.freeze

Class Method Summary collapse

Class Method Details

.all(filter_text: '') ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/economic/base_repo.rb', line 36

def all(filter_text: '')
  pagination = {}
  pageindex = 0
  entries = []

  # Loop until last page, last page does not have a 'nextPage'
  while pagination['nextPage'] || pageindex.zero?
    response = fetch(pageindex: pageindex, filter_text: filter_text)

    hash = JSON.parse(response.body)
    hash['collection'].each do |entry_hash|
      entries.push model.new(entry_hash)
    end

    pagination = hash['pagination']
    pageindex += 1
  end
  entries
end

.endpoint_nameObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/economic/base_repo.rb', line 82

def endpoint_name
  end_p = name.sub('Economic::', '')
  if end_p.include?('::')
    end_p = end_p.gsub('Repo', '')
    end_p = end_p.gsub('::', '/')
  else
    end_p = end_p.gsub('Repo', 's')
  end
  end_p = end_p.gsub('Journals', 'Journals-Experimental')
  kebab(end_p)
end

.endpoint_urlObject



94
95
96
# File 'lib/economic/base_repo.rb', line 94

def endpoint_url
  URL + endpoint_name
end

.fetch(pageindex: 0, filter_text: '') ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/economic/base_repo.rb', line 14

def fetch(pageindex: 0, filter_text: '')
  url = endpoint_url
  url << "?skippages=#{pageindex}&pagesize=1000"
  url << "&filter=#{filter_text}" unless filter_text == ''

  response = RestClient.get(URI.escape(url), headers)
  test_response(response)
end

.filter(filter_text) ⇒ Object



56
57
58
# File 'lib/economic/base_repo.rb', line 56

def filter(filter_text)
  all(filter_text: filter_text)
end

.find(id) ⇒ Object



70
71
72
73
74
# File 'lib/economic/base_repo.rb', line 70

def find(id)
  response = test_response(RestClient.get(endpoint_url + '/' + id.to_s, headers))
  entry_hash = JSON.parse(response.body)
  model.new(entry_hash)
end

.headersObject



10
11
12
# File 'lib/economic/base_repo.rb', line 10

def headers
  { 'X-AppSecretToken': Session.app_secret_token, 'X-AgreementGrantToken': Session.agreement_grant_token, 'Content-Type': 'application/json' }
end

.kebab(string) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/economic/base_repo.rb', line 104

def kebab(string)
  string.gsub(/::/, '/')
        .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .tr('_', '-')
        .downcase
end

.modelObject



76
77
78
79
80
# File 'lib/economic/base_repo.rb', line 76

def model
  scopes = name.split('::')
  scopes[1] = scopes[1][0...-1] if scopes.count == 3
  Object.const_get("#{scopes[0]}::#{scopes[1].sub('Repo', '')}")
end

.save(model) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/economic/base_repo.rb', line 23

def save(model)
  post_or_put = model.id_key.nil? ? :post : :put

  response = RestClient.public_send(post_or_put, URI.escape(endpoint_url + '/' + model.id_key.to_s), model.to_h.to_json, headers)

  test_response(response)
end

.send(model) ⇒ Object



31
32
33
34
# File 'lib/economic/base_repo.rb', line 31

def send(model)
  response = RestClient.post(URI.escape(endpoint_url), model.to_h.to_json, headers)
  test_response(response)
end

.test_response(response) ⇒ Object



98
99
100
101
102
# File 'lib/economic/base_repo.rb', line 98

def test_response(response)
  raise response unless response.code.between?(200, 299)

  response
end

.to_iso8601z(date) ⇒ Object



64
65
66
67
68
# File 'lib/economic/base_repo.rb', line 64

def to_iso8601z(date)
  date = date.iso8601
  date = date[0...-5].tr('+', 'Z') if date.include?('+')
  date
end

.updated_after(date) ⇒ Object



60
61
62
# File 'lib/economic/base_repo.rb', line 60

def updated_after(date)
  filter("lastUpdated$gt:#{to_iso8601z(date)}")
end