Class: ChainGang::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/chaingang/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, id = nil) ⇒ Proxy

Returns a new instance of Proxy.



5
6
7
8
9
# File 'lib/chaingang/proxy.rb', line 5

def initialize(client, id=nil)
  @client = client
  @params = {}
  @find_scope = id || :all
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Set the query string parameters on your request by calling them as methods with a question mark at the end.

Article.find(:all).where.author?("moonmaster9000") #/articles.xml?author=moonmaster9000


42
43
44
45
46
47
48
49
# File 'lib/chaingang/proxy.rb', line 42

def method_missing(method_name, *args, &block)
  if args.length == 1 && question?(method_name)
    @params[unquestion method_name] = args.first
    self
  else
    self.execute.send method_name, *args, &block
  end
end

Instance Attribute Details

#find_scopeObject

Returns the value of attribute find_scope.



3
4
5
# File 'lib/chaingang/proxy.rb', line 3

def find_scope
  @find_scope
end

Instance Method Details

#andObject

Simply improves readability. Does nothing but return self.

Article.find.where.author?("moonmaster9000").and.published?(true)


18
# File 'lib/chaingang/proxy.rb', line 18

def and;    self; end

#execute_with_formatObject Also known as: execute

execute the request in specified format, then revert it back after the request.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chaingang/proxy.rb', line 62

def execute_with_format
  fmt = @format
  if fmt && fmt.to_s != @client.format.extension
    old = @client.format.extension
    @client.format = fmt.to_sym
    result = execute_without_format
    @client.format = old.to_sym
    return result
  else
    execute_without_format
  end
end

#format(fmt) ⇒ Object

allow active resource to execute request with custom xml/json format from default.

Article.find(:all).by?("moonmaster9000").format(:json)


34
35
36
37
# File 'lib/chaingang/proxy.rb', line 34

def format(fmt)
  @format = fmt.to_sym
  self
end

#from(f) ⇒ Object

Specify a custom url path to retrieve send the service call to.

Article.find(:all).from :published


14
# File 'lib/chaingang/proxy.rb', line 14

def from(f); @from = f; self; end

#param?(name, value) ⇒ Boolean

Suppose you have a parameter name that collides with an existing method. Simply set it with this param? method:

Article.find(:all).param?(:from, "New York Times")

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/chaingang/proxy.rb', line 27

def param?(name, value)
  @params[name] = value
  self
end

#whereObject

Simply improves readability. Does nothing but return self.

Article.find.where.author?("moonmaster9000").and.published?(true)


22
# File 'lib/chaingang/proxy.rb', line 22

def where;  self; end