Class: Sinew::DSL
Defined Under Namespace
Classes: LimitError
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#elapsed ⇒ Object
readonly
Returns the value of attribute elapsed.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#sinew ⇒ Object
readonly
Returns the value of attribute sinew.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #csv_emit(row) ⇒ Object
-
#csv_header(*args) ⇒ Object
csv.
-
#get(url, query = {}) ⇒ Object
request.
-
#html ⇒ Object
response.
- #http(method, url, options = {}) ⇒ Object
-
#initialize(sinew) ⇒ DSL
constructor
A new instance of DSL.
- #json ⇒ Object
- #noko ⇒ Object
- #post(url, form = {}) ⇒ Object
- #post_json(url, json = {}) ⇒ Object
- #run ⇒ Object
- #url ⇒ Object
- #xml ⇒ Object
Constructor Details
#initialize(sinew) ⇒ DSL
Returns a new instance of DSL.
15 16 17 |
# File 'lib/sinew/dsl.rb', line 15 def initialize(sinew) @sinew = sinew end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
13 14 15 |
# File 'lib/sinew/dsl.rb', line 13 def code @code end |
#elapsed ⇒ Object (readonly)
Returns the value of attribute elapsed.
13 14 15 |
# File 'lib/sinew/dsl.rb', line 13 def elapsed @elapsed end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
13 14 15 |
# File 'lib/sinew/dsl.rb', line 13 def raw @raw end |
#sinew ⇒ Object (readonly)
Returns the value of attribute sinew.
13 14 15 |
# File 'lib/sinew/dsl.rb', line 13 def sinew @sinew end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
13 14 15 |
# File 'lib/sinew/dsl.rb', line 13 def uri @uri end |
Instance Method Details
#csv_emit(row) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/sinew/dsl.rb', line 107 def csv_emit(row) sinew.output.emit(row) if sinew.output.count == sinew.[:limit] raise LimitError.new end end |
#csv_header(*args) ⇒ Object
csv
103 104 105 |
# File 'lib/sinew/dsl.rb', line 103 def csv_header(*args) sinew.output.header(args) end |
#get(url, query = {}) ⇒ Object
request
34 35 36 |
# File 'lib/sinew/dsl.rb', line 34 def get(url, query = {}) http('get', url, query: query) end |
#html ⇒ Object
response
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sinew/dsl.rb', line 72 def html @html ||= begin s = raw.dup # squish! s.squish! # kill whitespace around tags s.gsub!(/ ?<([^>]+)> ?/, '<\\1>') s end end |
#http(method, url, options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/sinew/dsl.rb', line 54 def http(method, url, = {}) # these need to be cleared before each request %i[@html @noko @xml @json].each do |i| instance_variable_set(i, nil) end # fetch and make response available to callers response = sinew.http(method, url, ) @uri, @raw, @code = response.uri, response.body, response.code # don't confuse the user nil end |
#json ⇒ Object
91 92 93 |
# File 'lib/sinew/dsl.rb', line 91 def json @json ||= JSON.parse(raw, symbolize_names: true) end |
#noko ⇒ Object
83 84 85 |
# File 'lib/sinew/dsl.rb', line 83 def noko @noko ||= Nokogiri::HTML(html) end |
#post(url, form = {}) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/sinew/dsl.rb', line 38 def post(url, form = {}) body = form headers = { 'Content-Type' => 'application/x-www-form-urlencoded', } http('post', url, body: body, headers: headers) end |
#post_json(url, json = {}) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/sinew/dsl.rb', line 46 def post_json(url, json = {}) body = json.to_json headers = { 'Content-Type' => 'application/json', } http('post', url, body: body, headers: headers) end |
#run ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sinew/dsl.rb', line 19 def run tm = Time.now begin recipe = sinew.[:recipe] instance_eval(File.read(recipe, mode: 'rb'), recipe) rescue LimitError # ignore - this is flow control for --limit end @elapsed = Time.now - tm end |
#url ⇒ Object
95 96 97 |
# File 'lib/sinew/dsl.rb', line 95 def url uri.to_s end |
#xml ⇒ Object
87 88 89 |
# File 'lib/sinew/dsl.rb', line 87 def xml @xml ||= Nokogiri::XML(html) end |