Class: Muddyit::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/muddyit/base.rb

Direct Known Subclasses

Generic, Sites

Constant Summary collapse

REST_ENDPOINT =
'http://www.muddy.it'
@@http_open_timeout =
120
@@http_read_timeout =
120
@@digest1 =

Set the request signing method

OpenSSL::Digest::Digest.new("sha1")
@@digest256 =

Some installation may not support sha256

OpenSSL::Digest::Digest.new("sha256") rescue nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_hash_or_file) ⇒ Base

create a new muddyit object

You can either pass a hash with the following attributes:

  • :consumer_key (Required)

    the consumer key
    
  • :consumer_secret (Required)

    the consumer secret
    
  • :access_token (Required)

    the token
    
  • :access_token_secret (Required)

    the token secret
    
  • :rest_endpoint (Optional)

    the muddy.it rest service endpoint
    

or:

  • config_file (Required)

    yaml file to load configuration from
    

Config Example (yaml file)


consumer_key: AAA consumer_secret: BBB access_token: CCC access_token_secret: DDD



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/muddyit/base.rb', line 50

def initialize(config_hash_or_file)
  if config_hash_or_file.is_a? Hash
    config_hash_or_file.nested_symbolize_keys!
    @consumer_key = config_hash_or_file[:consumer_key]
    @consumer_secret = config_hash_or_file[:consumer_secret]
    @access_token = config_hash_or_file[:access_token]
    @access_token_secret = config_hash_or_file[:access_token_secret]
    @rest_endpoint = config_hash_or_file.has_key?(:rest_endpoint) ? config_hash_or_file[:rest_endpoint] : REST_ENDPOINT
    raise 'config_hash must contain consumer_key and consumer_secret' unless @consumer_key and @consumer_secret
  else
    config = YAML.load_file(config_hash_or_file)
    config.nested_symbolize_keys!
    @consumer_key = config[:consumer_key]
    @consumer_secret = config[:consumer_secret]
    @access_token = config[:access_token]
    @access_token_secret = config[:access_token_secret]
    @rest_endpoint = config.has_key?(:rest_endpoint) ? config[:rest_endpoint] : REST_ENDPOINT
    raise 'config file must contain consumer_key and consumer_secret' unless @consumer_key and @consumer_secret
  end

  @consumer = OAuth::Consumer.new(@consumer_key, @consumer_secret, {:site=>@rest_endpoint})
  @accesstoken = OAuth::AccessToken.new(@consumer, @access_token, @access_token_secret)

end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



11
12
13
# File 'lib/muddyit/base.rb', line 11

def access_token
  @access_token
end

#access_token_secretObject (readonly)

Returns the value of attribute access_token_secret.



11
12
13
# File 'lib/muddyit/base.rb', line 11

def access_token_secret
  @access_token_secret
end

#consumer_keyObject (readonly)

Returns the value of attribute consumer_key.



11
12
13
# File 'lib/muddyit/base.rb', line 11

def consumer_key
  @consumer_key
end

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



11
12
13
# File 'lib/muddyit/base.rb', line 11

def consumer_secret
  @consumer_secret
end

#rest_endpointObject

Returns the value of attribute rest_endpoint.



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

def rest_endpoint
  @rest_endpoint
end

Instance Method Details

#send_request(api_url, http_method = :get, opts = {}, body = nil) ⇒ Object

sends a request to the muddyit REST api

Params

  • api_url (Required)

    the request url (uri.path)
    
  • http_method (Optional)

    choose between GET (default), POST, PUT, DELETE http request.
    
  • options (Optional)

    hash of query parameters, you do not need to include access_key_id, secret_access_key because these are added automatically
    


85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/muddyit/base.rb', line 85

def send_request(api_url, http_method = :get, opts = {}, body = nil)

  raise 'no api_url supplied' unless api_url
  res = request_over_http(api_url, http_method, opts, body)
  # Strip any js wrapping methods

  if res.body =~ /^.+\((.+)\)$/
    r = JSON.parse($1)
  else
    r = JSON.parse(res.body)
  end
  
  return r
end

#sitesObject

creates and/or returns the Muddyit::Sites object



102
# File 'lib/muddyit/base.rb', line 102

def sites() @sites ||= Muddyit::Sites.new(self) end