Class: Sinew::Base

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

Overview

Sinew base class, for in standalone scripts or via the sinew binary.

Constant Summary collapse

RESET =

stdout

"\e[0m".freeze
RED =
"\e[1;37;41m".freeze
GREEN =
"\e[1;37;42m".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sinew/base.rb', line 12

def initialize(opts = {})
  @mutex = Mutex.new

  #
  # defaults for Sloptions
  #

  # default :rate_limit, typically 1
  default_rate_limit = ENV['SINEW_TEST'] ? 0 : 1

  #
  # note: uses HTTPDisk::Sloptions
  #

  @options = HTTPDisk::Sloptions.parse(opts) do
    # cli
    _1.integer :limit
    _1.integer :timeout, default: 30
    _1.boolean :silent
    _1.on :proxy, type: [:string, Array]
    _1.boolean :verbose

    # httpdisk
    _1.string :dir, default: File.join(ENV['HOME'], '.sinew')
    _1.integer :expires
    _1.boolean :force
    _1.boolean :force_errors
    _1.array :ignore_params

    # more runtime options
    _1.hash :headers
    _1.boolean :insecure
    _1.string :output, required: true
    _1.hash :params
    _1.float :rate_limit, default: default_rate_limit
    _1.integer :retries, default: 2
    _1.on :url_prefix, type: [:string, URI]
    _1.boolean :utf8, default: true
  end

  @csv = CSV.new(opts[:output])
end

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



10
11
12
# File 'lib/sinew/base.rb', line 10

def csv
  @csv
end

#mutexObject (readonly)

Returns the value of attribute mutex.



10
11
12
# File 'lib/sinew/base.rb', line 10

def mutex
  @mutex
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/sinew/base.rb', line 10

def options
  @options
end

Instance Method Details

Print a nice green banner.



161
162
163
164
165
166
# File 'lib/sinew/base.rb', line 161

def banner(msg, color: GREEN)
  msg = "#{msg} ".ljust(72, ' ')
  msg = "[#{Time.new.strftime('%H:%M:%S')}] #{msg}"
  msg = "#{color}#{msg}#{RESET}" if $stdout.tty?
  puts msg
end

#cached?(method, url, params = nil, body = nil) ⇒ Boolean

Returns true if request is cached. Defaults to form body type.

Returns:

  • (Boolean)


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

def cached?(method, url, params = nil, body = nil)
  status = status(method, url, params, body)
  status[:status] != 'miss'
end

#csv_emit(row) ⇒ Object

Output a csv row. Row should be any object that can turn into a hash - a hash, OpenStruct, etc.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sinew/base.rb', line 135

def csv_emit(row)
  row = row.to_h
  mutex.synchronize do
    # header if necessary
    csv_header(row.keys) if !csv.started?

    # emit
    print = csv.emit(row)
    puts print.ai if options[:verbose]

    # this is caught by Sinew::Main
    if csv.count == options[:limit]
      raise LimitError
    end
  end
end

#csv_header(*columns) ⇒ Object

Output a csv header. This usually happens automatically, but you can call this method directly to ensure a consistent set of columns.



129
130
131
# File 'lib/sinew/base.rb', line 129

def csv_header(*columns)
  csv.start(columns.flatten)
end

#faradayObject

Faraday connection for this recipe



83
84
85
86
87
# File 'lib/sinew/base.rb', line 83

def faraday
  mutex.synchronize do
    @faraday ||= create_faraday
  end
end

#fatal(msg) ⇒ Object

Print a scary red banner and exit.



169
170
171
172
# File 'lib/sinew/base.rb', line 169

def fatal(msg)
  banner(msg, color: RED)
  exit 1
end

#get(url, params = nil, headers = nil) ⇒ Object

http get, returns a Response



60
61
62
63
64
65
# File 'lib/sinew/base.rb', line 60

def get(url, params = nil, headers = nil)
  faraday_response = faraday.get(url, params, headers) do
    _1.options[:proxy] = random_proxy
  end
  Response.new(faraday_response)
end

#post(url, body = nil, headers = nil) ⇒ Object

http post, returns a Response. Defaults to form body type.



68
69
70
71
72
73
# File 'lib/sinew/base.rb', line 68

def post(url, body = nil, headers = nil)
  faraday_response = faraday.post(url, body, headers) do
    _1.options[:proxy] = random_proxy
  end
  Response.new(faraday_response)
end

#post_json(url, body = nil, headers = nil) ⇒ Object

http post json, returns a Response



76
77
78
79
80
# File 'lib/sinew/base.rb', line 76

def post_json(url, body = nil, headers = nil)
  body = body.to_json
  headers = (headers || {}).merge('Content-Type' => 'application/json')
  post(url, body, headers)
end

#status(method, url, params = nil, body = nil) ⇒ Object

Check httpdisk status for this request. Defaults to form body type.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sinew/base.rb', line 107

def status(method, url, params = nil, body = nil)
  # if hash, default to url encoded form
  # see lib/faraday/request/url_encoded.rb
  if body.is_a?(Hash)
    body = Faraday::Utils::ParamsHash[body].to_query
  end

  env = Faraday::Env.new.tap do
    _1.method = method.to_s.downcase.to_sym
    _1.request_headers = {}
    _1.request_body = body
    _1.url = faraday.build_url(url, params)
  end
  httpdisk.status(env)
end

#uncache(method, url, params = nil, body = nil) ⇒ Object

Remove cache file, if any. Defaults to form body type.



100
101
102
103
104
# File 'lib/sinew/base.rb', line 100

def uncache(method, url, params = nil, body = nil)
  status = status(method, url, params, body)
  path = status[:path]
  File.unlink(path) if File.exist?(path)
end