Module: HypertextClient

Extended by:
ActiveSupport::Concern
Defined in:
lib/hypertext_client/hypertext_client.rb

Overview

Hypertext provides an HTTP client based on HTTParty, with a few assumptions baked in.

Defined Under Namespace

Modules: ClassMethods Classes: MissingArgumentError, NotFoundException

Constant Summary collapse

VERSION =
'0.0.10'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Make a GET request to provide a requested property, if we have a URL for that name.

See Also:

  • HTTParty


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hypertext_client/hypertext_client.rb', line 42

def method_missing(name, *args)
	if name =~ /^has_(\w+)_url\?$/
		super(name, *args)
	elsif name =~ /^has_(\w+)\?$/
		field = $1.to_sym
		super(name, *args) ? super(field, *args) : has_related?(field)
	elsif self.respond_to?((name.to_s + '_url').to_sym)
		self.get_related(name)
	else
		super(name, *args)
	end
end

Instance Method Details

Get a related property.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hypertext_client/hypertext_client.rb', line 72

def get_related(related)
	if related.to_s =~ /[Aa]lias/ # patch over bug in ActiveSupport::Inflector
		klass = (self.class.name.split('::').first + '::' + 'Alias').constantize
	else
		klass = (self.class.name.split('::').first + '::' + related.to_s.singularize.classify).constantize
	end
	result = klass.get(send (related.to_s+'_url').to_sym)

	(class << self; self; end).instance_eval do
		define_method related do
			result
		end
	end

	result
end

#has_related?(related) ⇒ Boolean

A simple wrapper, we have a property if we have it’s URL.

Returns:

  • (Boolean)


56
57
58
# File 'lib/hypertext_client/hypertext_client.rb', line 56

def has_related?(related)
	send "has_#{related}_url?"
end

#persisted?Boolean

HypertextClient isn’t persisted, yet.

Returns:

  • (Boolean)


67
68
69
# File 'lib/hypertext_client/hypertext_client.rb', line 67

def persisted?
	false
end

#to_modelObject

Simply returns self

Returns:

  • self



62
63
64
# File 'lib/hypertext_client/hypertext_client.rb', line 62

def to_model
	self
end