Class: Curl::Json
- Inherits:
-
Object
- Object
- Curl::Json
- Defined in:
- lib/curly/curl/json.rb
Overview
Class for CURLing a JSON response
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#compressed ⇒ Object
writeonly
Sets the attribute compressed.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#request_headers ⇒ Object
writeonly
Sets the attribute request_headers.
-
#symbolize_names ⇒ Object
writeonly
Sets the attribute symbolize_names.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #curl ⇒ Object
-
#initialize(url, options = {}) ⇒ Curl::Json
constructor
Create a new Curl::Json page object.
- #path(path, json = @json) ⇒ Object
- #to_data ⇒ Object
Constructor Details
#initialize(url, options = {}) ⇒ Curl::Json
Create a new Curl::Json page object
30 31 32 33 34 35 36 37 |
# File 'lib/curly/curl/json.rb', line 30 def initialize(url, = {}) @url = url @request_headers = [:headers] @compressed = [:compressed] @symbolize_names = [:symbolize_names] @curl = TTY::Which.which('curl') end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
10 11 12 |
# File 'lib/curly/curl/json.rb', line 10 def code @code end |
#compressed=(value) ⇒ Object (writeonly)
Sets the attribute compressed
8 9 10 |
# File 'lib/curly/curl/json.rb', line 8 def compressed=(value) @compressed = value end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
10 11 12 |
# File 'lib/curly/curl/json.rb', line 10 def headers @headers end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
10 11 12 |
# File 'lib/curly/curl/json.rb', line 10 def json @json end |
#request_headers=(value) ⇒ Object (writeonly)
Sets the attribute request_headers
8 9 10 |
# File 'lib/curly/curl/json.rb', line 8 def request_headers=(value) @request_headers = value end |
#symbolize_names=(value) ⇒ Object (writeonly)
Sets the attribute symbolize_names
8 9 10 |
# File 'lib/curly/curl/json.rb', line 8 def symbolize_names=(value) @symbolize_names = value end |
#url ⇒ Object
Returns the value of attribute url.
6 7 8 |
# File 'lib/curly/curl/json.rb', line 6 def url @url end |
Instance Method Details
#curl ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/curly/curl/json.rb', line 39 def curl page = curl_json raise "Error retrieving #{url}" if page.nil? || page.empty? @url = page[:url] @code = page[:code] @json = page[:json] @headers = page[:headers] end |
#path(path, json = @json) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/curly/curl/json.rb', line 50 def path(path, json = @json) parts = path.split(/./) target = json parts.each do |part| if part =~ /(?<key>[^\[]+)\[(?<int>\d+)\]/ target = target[key][int.to_i] else target = target[part] end end target end |
#to_data ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/curly/curl/json.rb', line 12 def to_data { url: @url, code: @code, json: @json, headers: @headers } end |