Class: RHACK::Client

Inherits:
Object show all
Defined in:
lib/rhack/clients/base.rb,
lib/rhack/clients/storage.rb

Direct Known Subclasses

OAuthClient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rhack/clients/base.rb', line 70

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
    @f = Frame(rootpath || route(service) || route(:login), opts)
  end
end

Instance Attribute Details

#fObject

Returns the value of attribute f.



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

def f
  @f
end

#serviceObject (readonly)

Returns the value of attribute service.



9
10
11
# File 'lib/rhack/clients/base.rb', line 9

def service
  @service
end

#storageObject (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



22
23
24
25
26
27
# File 'lib/rhack/clients/base.rb', line 22

def inherited(child)
  child.class_eval {
    include RHACK
    __init__
  }
end

.method_missing(method, *args, &block) ⇒ Object



29
30
31
32
33
34
# File 'lib/rhack/clients/base.rb', line 29

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



135
136
137
# File 'lib/rhack/clients/base.rb', line 135

def (name)
  accounts[name]
end

#go(*args, &block) ⇒ Object



101
102
103
104
105
106
# File 'lib/rhack/clients/base.rb', line 101

def go(*args, &block)
  __send__(@service, *args, &block) 
rescue
  L < $!
  Curl.reload
end

#inspectObject



85
86
87
# File 'lib/rhack/clients/base.rb', line 85

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

Yields:



91
92
93
94
95
96
97
98
99
# File 'lib/rhack/clients/base.rb', line 91

def (*)
  Curl.run
  @f[0].cookies.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.copy_cookies!
end

#route(name, interpolation = nil) ⇒ Object Also known as: url, URI

shortcuts to class variables #



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rhack/clients/base.rb', line 120

def route(name, interpolation=nil)
  if url = routes[name]
    if interpolation
      url %= interpolation
    end
    if url !~ /^\w+:/
      url = File.join rootpath, url
    end
    url
  end
end

#scrape!(page) ⇒ Object



110
111
112
113
114
115
# File 'lib/rhack/clients/base.rb', line 110

def scrape!(page)
  __send__(:"scrape_#@service", page)
  if url = next_url(page)
    @f.get(url) {|next_page| scrape!(next_page)}
  end
end