Class: Serrano::Request

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/serrano/request.rb,
lib/serrano/filterhandler.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) ⇒ Request

Returns a new instance of Request.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/serrano/request.rb', line 32

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

  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
end

Instance Attribute Details

#agencyObject

Returns the value of attribute agency.



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

def agency
  @agency
end

#endptObject

Returns the value of attribute endpt.



16
17
18
# File 'lib/serrano/request.rb', line 16

def endpt
  @endpt
end

#facetObject

Returns the value of attribute facet.



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

def facet
  @facet
end

#filterObject

Returns the value of attribute filter.



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

def filter
  @filter
end

#idObject

Returns the value of attribute id.



17
18
19
# File 'lib/serrano/request.rb', line 17

def id
  @id
end

#limitObject

Returns the value of attribute limit.



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

def limit
  @limit
end

#offsetObject

Returns the value of attribute offset.



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

def offset
  @offset
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#orderObject

Returns the value of attribute order.



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

def order
  @order
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

#sampleObject

Returns the value of attribute sample.



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

def sample
  @sample
end

#selectObject

Returns the value of attribute select.



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

def select
  @select
end

#sortObject

Returns the value of attribute sort.



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

def sort
  @sort
end

#verboseObject

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

#worksObject

Returns the value of attribute works.



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

def works
  @works
end

Instance Method Details

#performObject



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

def perform
  filt = filter_handler(filter)

  self.select = select&.instance_of?(Array) ? select.join(",") : select

  args = {query: query, filter: filt, offset: offset, rows: limit,
          sample: sample, sort: sort, order: order, facet: facet,
          select: select}
  opts = args.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?
    res = conn.get endpt, opts
    MultiJson.load(res.body)
  else
    self.id = Array(id)
    # url encoding
    self.id = id.map { |x| ERB::Util.url_encode(x) }
    coll = []
    id.each do |x|
      endpt = if works
        self.endpt + "/" + x.to_s + "/works"
      elsif agency
        self.endpt + "/" + x.to_s + "/agency"
      else
        self.endpt + "/" + x.to_s
      end

      res = conn.get endpt, opts
      coll << MultiJson.load(res.body)
    end
    coll
  end
end