Class: Foxy::Client
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#default_options ⇒ Object
readonly
Returns the value of attribute default_options.
Instance Method Summary collapse
- #cache ⇒ Object
- #eraw(options) ⇒ Object
- #fixed(id, legth = 2, fill = "0") ⇒ Object
-
#initialize(config = {}) ⇒ Client
constructor
A new instance of Client.
- #json(options) ⇒ Object
- #raw(options) ⇒ Object
- #request(options) ⇒ Object
- #url ⇒ Object
- #user_agent ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/foxy/client.rb', line 17 def initialize(config = {}) @config = config @default_options = config.fetch(:default_options, {}).recursive_hash @conn = Faraday.new(url: url) do |connection| connection.[:timeout] = config.fetch(:timeout, 120) connection.[:open_timeout] = config.fetch(:open_timeout, 20) connection.headers[:user_agent] = user_agent connection.use(Faraday::Response::Middleware) # connection.response :logger # connection.response :json # connection.use FaradayMiddleware::Gzip # connection.adapter(Faraday.default_adapter) connection.adapter :patron end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/foxy/client.rb', line 15 def config @config end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
15 16 17 |
# File 'lib/foxy/client.rb', line 15 def conn @conn end |
#default_options ⇒ Object (readonly)
Returns the value of attribute default_options.
15 16 17 |
# File 'lib/foxy/client.rb', line 15 def @default_options end |
Instance Method Details
#cache ⇒ Object
65 66 67 |
# File 'lib/foxy/client.rb', line 65 def cache @cache ||= FileCache.new(self.class.name.split("::").last.downcase) end |
#eraw(options) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/foxy/client.rb', line 54 def eraw() cacheopts = .delete(:cache) klass = .delete(:class) || Foxy::HtmlResponse = .merge(.delete(:response_params) || {}) klass.new(raw_with_cache(, cacheopts), ) end |
#fixed(id, legth = 2, fill = "0") ⇒ Object
69 70 71 |
# File 'lib/foxy/client.rb', line 69 def fixed(id, legth = 2, fill = "0") id.to_s.rjust(legth, fill) end |
#json(options) ⇒ Object
46 47 48 |
# File 'lib/foxy/client.rb', line 46 def json() JSON[raw()] end |
#raw(options) ⇒ Object
50 51 52 |
# File 'lib/foxy/client.rb', line 50 def raw() request().body end |
#request(options) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/foxy/client.rb', line 39 def request() wait! opts = .deep_merge() conn.get(opts.fetch(:path), opts.fetch(:params, {})) end |
#url ⇒ Object
61 62 63 |
# File 'lib/foxy/client.rb', line 61 def url "http://www.example.com" end |
#user_agent ⇒ Object
35 36 37 |
# File 'lib/foxy/client.rb', line 35 def user_agent "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" end |