Class: RHACK::Client
- Defined in:
- lib/rhack/clients/base.rb,
lib/rhack/clients/storage.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#f ⇒ Object
Returns the value of attribute f.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Class Method Summary collapse
- .inherited(child) ⇒ Object
- .method_missing(method, *args, &block) ⇒ Object
- .store(type, name, opts = {}) ⇒ Object
Instance Method Summary collapse
- #account(name) ⇒ Object
- #go(*args, &block) ⇒ Object
-
#initialize(*args) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
-
#login {|@f.get(route :login)| ... } ⇒ Object
Usable only for sync requests.
-
#route(name, interpolation = nil) ⇒ Object
(also: #url)
shortcuts to class variables #.
- #scrape!(page) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Client
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rhack/clients/base.rb', line 86 def initialize(*args) service, opts = args.get_opts [routes.include?(:api) ? :api : nil] @service = service # Deprectated. Use different classes to implement different services. # first argument should be a string so that frame won't be static if opts.is_a?(Frame) @f = opts else opts = frame_defaults.merge(opts) if self.class.const_defined? :Result opts[:result] = self.class::Result end if scouts_initializers = self.scouts_initializers.presence opts[:on_scout_initialize] ||= lambda {|scout| scouts_initializers.each {|initializer| initializer.call scout } } end @f = Frame(rootpath || route(service) || route(:login), opts) end end |
Instance Attribute Details
#f ⇒ Object
Returns the value of attribute f.
6 7 8 |
# File 'lib/rhack/clients/base.rb', line 6 def f @f end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
5 6 7 |
# File 'lib/rhack/clients/base.rb', line 5 def service @service end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
7 8 9 |
# File 'lib/rhack/clients/storage.rb', line 7 def storage @storage end |
Class Method Details
.inherited(child) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rhack/clients/base.rb', line 20 def inherited(child) child.class_eval do include RHACK __init__ # Proxying Rails router methods digging into the client namespace if defined? ::Rails.application.routes.url_helpers url_helpers = ::Rails.application.routes.url_helpers routes_namespace = name.gsub(/::/, '').underscore.sub(/_client$/, '') route_pattern = /^(.+_)?#{routes_namespace}_(.+)/ url_helpers.my_methods.each {|name| if name.to_s[route_pattern] full_route_name = $& define_method "#$1#$2" do |*| url_helpers.send full_route_name, * end end } end end end |
.method_missing(method, *args, &block) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/rhack/clients/base.rb', line 42 def method_missing(method, *args, &block) if personal_instance_methods.include? method return new.__send__(method, *args, &block) end super end |
.store(type, name, opts = {}) ⇒ Object
9 10 11 |
# File 'lib/rhack/clients/storage.rb', line 9 def self.store(type, name, opts={}) storage[name] = RHACK::Storage(type, (opts[:prefix] || self.name.sub('RHACK::', '').underscore)+':'+name) end |
Instance Method Details
#account(name) ⇒ Object
156 157 158 |
# File 'lib/rhack/clients/base.rb', line 156 def account(name) accounts[name] end |
#go(*args, &block) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/rhack/clients/base.rb', line 124 def go(*args, &block) __send__(@service, *args, &block) rescue L < $! Curl.reload end |
#inspect ⇒ Object
108 109 110 |
# File 'lib/rhack/clients/base.rb', line 108 def inspect "<##{self.class.name}#{":#{@service.to_s.camelize} service" if @service} via #{@f.inspect}>" end |
#login {|@f.get(route :login)| ... } ⇒ Object
Usable only for sync requests
114 115 116 117 118 119 120 121 122 |
# File 'lib/rhack/clients/base.rb', line 114 def login(*) Curl.run @f[0]..clear json, wait, @f.opts[:json], @f.opts[:wait] = @f.opts[:json], @f.opts[:wait], false, true yield @f.get(route :login) @f.get(route :home) if route :home @f.opts[:json], @f.opts[:wait] = json, wait @f. end |
#route(name, interpolation = nil) ⇒ Object Also known as: url
shortcuts to class variables #
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/rhack/clients/base.rb', line 143 def route(name, interpolation=nil) if url = routes[name] if interpolation url %= interpolation.symbolize_keys end if url !~ /^\w+:/ url = File.join rootpath, url end url end end |
#scrape!(page) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/rhack/clients/base.rb', line 133 def scrape!(page) __send__(:"scrape_#@service", page) if url = next_url(page) @f.get(url) {|next_page| scrape!(next_page)} end end |