Class: Sinew::DSL

Inherits:
Object show all
Defined in:
lib/sinew/dsl.rb

Defined Under Namespace

Classes: LimitError

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#codeObject (readonly)

Returns the value of attribute code.



13
14
15
# File 'lib/sinew/dsl.rb', line 13

def code
  @code
end

#elapsedObject (readonly)

Returns the value of attribute elapsed.



13
14
15
# File 'lib/sinew/dsl.rb', line 13

def elapsed
  @elapsed
end

#rawObject (readonly)

Returns the value of attribute raw.



13
14
15
# File 'lib/sinew/dsl.rb', line 13

def raw
  @raw
end

#sinewObject (readonly)

Returns the value of attribute sinew.



13
14
15
# File 'lib/sinew/dsl.rb', line 13

def sinew
  @sinew
end

#uriObject (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.options[: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

#htmlObject

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, options = {})
  # 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, options)
  @uri, @raw, @code = response.uri, response.body, response.code

  # don't confuse the user
  nil
end

#jsonObject



91
92
93
# File 'lib/sinew/dsl.rb', line 91

def json
  @json ||= JSON.parse(raw, symbolize_names: true)
end

#nokoObject



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

#runObject



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.options[:recipe]
    instance_eval(File.read(recipe, mode: 'rb'), recipe)
  rescue LimitError
    # ignore - this is flow control for --limit
  end
  @elapsed = Time.now - tm
end

#urlObject



95
96
97
# File 'lib/sinew/dsl.rb', line 95

def url
  uri.to_s
end

#xmlObject



87
88
89
# File 'lib/sinew/dsl.rb', line 87

def xml
  @xml ||= Nokogiri::XML(html)
end