Class: Serrano::RequestCursor

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/serrano/filterhandler.rb,
lib/serrano/request_cursor.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#filter_handler, #rename_keys, #stringify

Constructor Details

#initialize(endpt, id, query, filter, offset, limit, sample, sort, order, facet, select, works, agency, options, verbose, cursor, cursor_max, args) ⇒ RequestCursor

Returns a new instance of RequestCursor.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/serrano/request_cursor.rb', line 37

def initialize(endpt, id, query, filter, offset,
  limit, sample, sort, order, facet, select,
  works, agency, options, verbose, cursor,
  cursor_max, args)

  self.endpt = endpt
  self.id = id
  self.query = query
  self.filter = filter
  self.offset = offset
  self.limit = limit
  self.sample = sample
  self.sort = sort
  self.order = order
  self.facet = facet
  self.select = select
  self.works = works
  self.agency = agency
  self.options = options
  self.verbose = verbose
  self.cursor = cursor
  self.cursor_max = cursor_max
  self.args = args
end

Instance Attribute Details

#agencyObject

Returns the value of attribute agency.



30
31
32
# File 'lib/serrano/request_cursor.rb', line 30

def agency
  @agency
end

#argsObject

Returns the value of attribute args.



35
36
37
# File 'lib/serrano/request_cursor.rb', line 35

def args
  @args
end

#cursorObject

Returns the value of attribute cursor.



33
34
35
# File 'lib/serrano/request_cursor.rb', line 33

def cursor
  @cursor
end

#cursor_maxObject

Returns the value of attribute cursor_max.



34
35
36
# File 'lib/serrano/request_cursor.rb', line 34

def cursor_max
  @cursor_max
end

#endptObject

Returns the value of attribute endpt.



18
19
20
# File 'lib/serrano/request_cursor.rb', line 18

def endpt
  @endpt
end

#facetObject

Returns the value of attribute facet.



27
28
29
# File 'lib/serrano/request_cursor.rb', line 27

def facet
  @facet
end

#filterObject

Returns the value of attribute filter.



21
22
23
# File 'lib/serrano/request_cursor.rb', line 21

def filter
  @filter
end

#idObject

Returns the value of attribute id.



19
20
21
# File 'lib/serrano/request_cursor.rb', line 19

def id
  @id
end

#limitObject

Returns the value of attribute limit.



23
24
25
# File 'lib/serrano/request_cursor.rb', line 23

def limit
  @limit
end

#offsetObject

Returns the value of attribute offset.



22
23
24
# File 'lib/serrano/request_cursor.rb', line 22

def offset
  @offset
end

#optionsObject

Returns the value of attribute options.



31
32
33
# File 'lib/serrano/request_cursor.rb', line 31

def options
  @options
end

#orderObject

Returns the value of attribute order.



26
27
28
# File 'lib/serrano/request_cursor.rb', line 26

def order
  @order
end

#queryObject

Returns the value of attribute query.



20
21
22
# File 'lib/serrano/request_cursor.rb', line 20

def query
  @query
end

#sampleObject

Returns the value of attribute sample.



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

def sample
  @sample
end

#selectObject

Returns the value of attribute select.



28
29
30
# File 'lib/serrano/request_cursor.rb', line 28

def select
  @select
end

#sortObject

Returns the value of attribute sort.



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

def sort
  @sort
end

#verboseObject

Returns the value of attribute verbose.



32
33
34
# File 'lib/serrano/request_cursor.rb', line 32

def verbose
  @verbose
end

#worksObject

Returns the value of attribute works.



29
30
31
# File 'lib/serrano/request_cursor.rb', line 29

def works
  @works
end

Instance Method Details

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/serrano/request_cursor.rb', line 124

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

#_req(conn, path, opts) ⇒ Object



141
142
143
144
# File 'lib/serrano/request_cursor.rb', line 141

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

#performObject



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
# File 'lib/serrano/request_cursor.rb', line 62

def perform
  filt = filter_handler(filter)
  fieldqueries = field_query_handler(args)
  self.select = select&.instance_of?(Array) ? select.join(",") : select

  unless cursor_max.class.nil?
    raise "cursor_max must be of class int" unless cursor_max.is_a?(Integer)
  end

  arguments = {query: query, filter: filt, offset: offset,
               rows: limit, sample: sample, sort: sort,
               order: order, facet: facet, select: select,
               cursor: cursor}.tostrings
  arguments = arguments.merge(fieldqueries)
  opts = arguments.delete_if { |_k, v| v.nil? }

  conn = if verbose
    Faraday.new(url: Serrano.base_url, request: options || {}) do |f|
      f.response :logger
      f.use Faraday::SerranoErrors::Middleware
    end
  else
    Faraday.new(url: Serrano.base_url, request: options || {}) do |f|
      f.use Faraday::SerranoErrors::Middleware
    end
  end

  conn.headers[:user_agent] = make_ua
  conn.headers["X-USER-AGENT"] = make_ua

  if id.nil?
    endpt2 = endpt
    js = _req(conn, endpt, opts)
    cu = js["message"]["next-cursor"]
    max_avail = js["message"]["total-results"]
    _redo_req(conn, js, opts, cu, max_avail, endpt2)

  else
    self.id = Array(id)
    # url encoding
    self.id = id.map { |x| ERB::Util.url_encode(x) }
    coll = []
    id.each do |x|
      endpt2 = if works
        endpt + "/" + x.to_s + "/works"
      else
        endpt2 = if agency
          endpt + "/" + x.to_s + "/agency"
        else
          endpt + "/" + x.to_s
        end
      end

      js = _req(conn, endpt2, opts)
      cu = js["message"]["next-cursor"]
      max_avail = js["message"]["total-results"]
      coll << _redo_req(conn, js, opts, cu, max_avail, endpt2)
    end
    coll
  end
end