Top Level Namespace

Defined Under Namespace

Modules: Configuration, FaradayMiddleware, Helpers, Serrano Classes: Hash

Constant Summary collapse

NETWORKABLE_EXCEPTIONS =
[Faraday::Error::ClientError,
URI::InvalidURIError,
Encoding::UndefinedConversionError,
ArgumentError,
NoMethodError,
TypeError]

Instance Method Summary collapse

Instance Method Details

#_redo_req(conn, path, js, opts, cu, max_avail, cursor_max) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/serrano/cursor_testing.rb', line 29

def _redo_req(conn, path, js, opts, cu, max_avail, cursor_max)
  if !cu.nil? and cursor_max > js['message']['items'].length
    res = [js]
    total = js['message']['items'].length
    while (!cu.nil? and cursor_max > total and total < max_avail)
      opts[:cursor] = cu
      out = _req(conn, path, opts)
      cu = out['message']['next-cursor']
      res << out
      total = res.collect { |x| x['message']['items'].length }.inject(:+)
    end
    return res
  else
    return js
  end
end

#_req(conn, path, opts) ⇒ Object



24
25
26
27
# File 'lib/serrano/cursor_testing.rb', line 24

def _req(conn, path, opts)
  res = conn.get path, opts
  return MultiJson.load(res.body)
end

#field_query_handler(x) ⇒ Object



11
12
13
14
# File 'lib/serrano/utils.rb', line 11

def field_query_handler(x)
	tmp = x.keep_if { |z| z.match(/query_/) }
  return rename_query_filters(tmp)
end

#get_stylesObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/serrano/styles.rb', line 4

def get_styles
  base = "https://api.github.com/repos/citation-style-language/styles"
  conn = Faraday.new(:url => base)
  args = { per_page: 1 }
  tt = conn.get 'commits', args
  commres = MultiJson.load(tt.body)
  sha = commres[0]['sha']
  sty = conn.get "git/trees/" + sha
  res = MultiJson.load(sty.body)
  files = res['tree'].collect { |x| x['path'] }
  matches = files.collect { |x|
    if x.match('csl').nil?
      nil
    else
      x.match('csl').string
    end
  }
  csls = matches.compact
  return csls.collect { |z| z.gsub('.csl', '') }
end

#make_request(ids, format, style, locale) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/serrano/cnrequest.rb', line 54

def make_request(ids, format, style, locale)
  type = $cn_format_headers.select { |x, _| x.include? format }.values[0]

  if format == "citeproc-json"
    endpt = "http://api.crossref.org/works/" + ids + "/" + type
    cr_works = Faraday.new(:url => endpt)
    cr_works.headers[:user_agent] = make_ua
    cr_works.headers["X-USER-AGENT"] = make_ua
    res = cr_works.get
  else
    if format == "text"
      type = type + "; style = " + style + "; locale = " + locale
    end

    res = $conn.get do |req|
      req.url ids
      req.headers['Accept'] = type
      req.headers[:user_agent] = make_ua
      req.headers["X-USER-AGENT"] = make_ua
    end
  end

  return res.body
end

#make_uaObject



1
2
3
4
5
6
7
8
9
# File 'lib/serrano/utils.rb', line 1

def make_ua
  requa = 'Faraday/v' + Faraday::VERSION
  habua = 'Serrano/v' + Serrano::VERSION
  ua = requa + ' ' + habua
  if Serrano.mailto
    ua = ua + " (mailto:%s)" % Serrano.mailto
  end
  return ua
end

#rename_query_filters(foo) ⇒ Object



16
17
18
19
20
21
# File 'lib/serrano/utils.rb', line 16

def rename_query_filters(foo)
	foo = foo.tostrings
	foo = foo.map { |x,y| [x.to_s.sub('container_title', 'container-title'), y] }.to_h
	foo = foo.map { |x,y| [x.to_s.sub('query_', 'query.'), y] }.to_h
  return foo
end