Class: Webmaster::Base

Inherits:
Object show all
Includes:
Api::Authorization, Api::Connection, Api::Request
Defined in:
lib/webmaster/base.rb

Direct Known Subclasses

Client, Host, Hosts::Crawling, Hosts::Verification

Constant Summary

Constants included from Api::Request

Api::Request::METHODS, Api::Request::METHODS_WITH_BODIES

Constants included from Api::Connection

Api::Connection::ACCEPT, Api::Connection::ACCEPT_CHARSET, Api::Connection::ALLOWED_OPTIONS, Api::Connection::CONTENT_TYPE, Api::Connection::USER_AGENT

Instance Attribute Summary collapse

Attributes included from Api::Authorization

#scopes

Instance Method Summary collapse

Methods included from Api::Request

#delete_request, #get_request, #post_request, #put_request, #request

Methods included from Api::Connection

#connection

Methods included from Api::Authorization

#auth_code, #authenticate, #authenticated?, #authorize_url, #oauth, #token

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



17
18
19
20
# File 'lib/webmaster/base.rb', line 17

def initialize(attributes = {})
  self.configuration = Webmaster::Configuration.instance.setup(attributes.delete(:configuration) || {})
  self.attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Responds to attribute query or attribute clear



52
53
54
55
56
57
58
59
60
61
# File 'lib/webmaster/base.rb', line 52

def method_missing(method, *args, &block) # :nodoc:
  case method.to_s
  when /^(.*)\?$/
    return !!self.send($1.to_s)
  when /^clear_(.*)$/
    self.send("#{$1.to_s}=", nil)
  else
    super
  end
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



15
16
17
# File 'lib/webmaster/base.rb', line 15

def configuration
  @configuration
end

Instance Method Details

#arguments(args = (not_set = true), options = {}, &block) ⇒ Object

Acts as setter and getter for api requests arguments parsing.

Returns Arguments instance.



67
68
69
70
71
72
73
# File 'lib/webmaster/base.rb', line 67

def arguments(args=(not_set = true), options={}, &block)
  if not_set
    @arguments
  else
    @arguments = Arguments.new(self, options).parse(*args, &block)
  end
end

#attributes=(attributes = {}) ⇒ Object



22
23
24
25
26
# File 'lib/webmaster/base.rb', line 22

def attributes=(attributes = {})
  attributes.each do |attr,value|
    self.send("#{attr}=", value) if self.respond_to?("#{attr}=")
  end
end

#inspectObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/webmaster/base.rb', line 28

def inspect
  inspection = if self.respond_to?(:attributes) && self.attributes.any?
    self.attributes.collect { |attribute, value|
      "#{attribute}: #{value}" if value.present?
    }.compact.join(', ')
  else
    self.instance_variables.map{ |variable|
      "#{variable}: #{instance_variable_get(variable).inspect}"
    }.compact.join(', ')
  end

  "#<#{self.class} #{inspection}>"
end

#set(option, value = (not_set=true), ignore_setter = false, &block) ⇒ Object

Set an option to a given value

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/webmaster/base.rb', line 90

def set(option, value=(not_set=true), ignore_setter=false, &block)
  raise ArgumentError, 'value not set' if block and !not_set
  return self if !not_set and value.nil?

  if not_set
    set_options option
    return self
  end

  if respond_to?("#{option}=") and not ignore_setter
    return __send__("#{option}=", value)
  end

  define_accessors option, value
  self
end

#with(args) ⇒ Object

Scope for passing request required arguments.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/webmaster/base.rb', line 77

def with(args)
  case args
  when Hash
    set args
  when /.*\/.*/i
    user, repo = args.split('/')
    set :user => user, :repo => repo
  else
    ::Kernel.raise ArgumentError, 'This api does not support passed in arguments'
  end
end