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.



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
54
# File 'lib/sinew/base.rb', line 13

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.



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

def csv
  @csv
end

#mutexObject (readonly)

Returns the value of attribute mutex.



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

def mutex
  @mutex
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

Print a nice green banner.



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

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)


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

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.



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

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.



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

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

#faradayObject

Faraday connection for this recipe



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

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

#fatal(msg) ⇒ Object

Print a scary red banner and exit.



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

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

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

http get, returns a Response



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

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.



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

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



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

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.



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

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.



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

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