Class: Curl::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/searchlink/curl/json.rb

Overview

Class for CURLing a JSON response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, data: nil, headers: nil, compressed: false, symbolize_names: false) ⇒ Curl::Json

Create a new Curl::Json page object

Parameters:

  • url (String)

    The url to curl

  • headers (Hash) (defaults to: nil)

    The headers to send

  • compressed (Boolean) (defaults to: false)

    Expect compressed results



17
18
19
20
21
22
23
24
# File 'lib/searchlink/curl/json.rb', line 17

def initialize(url, data: nil, headers: nil, compressed: false, symbolize_names: false)
  @curl = TTY::Which.which("curl")
  page = curl_json(url, data: data, headers: headers, compressed: compressed, symbolize_names: symbolize_names)
  @url = page[:url]
  @code = page[:code]
  @json = page[:json]
  @headers = page[:headers]
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/searchlink/curl/json.rb', line 6

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/searchlink/curl/json.rb', line 6

def headers
  @headers
end

#jsonObject (readonly)

Returns the value of attribute json.



6
7
8
# File 'lib/searchlink/curl/json.rb', line 6

def json
  @json
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/searchlink/curl/json.rb', line 6

def url
  @url
end

Instance Method Details

#path(path, json = @json) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/searchlink/curl/json.rb', line 26

def path(path, json = @json)
  parts = path.split(/./)
  target = json
  parts.each do |part|
    target = if part =~ /(?<key>[^\[]+)\[(?<int>\d+)\]/
        target[key][int.to_i]
      else
        target[part]
      end
  end

  target
end