Class: CrowdFlower::Connection

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/crowdflower/base.rb

Overview

an object that stores connection details; does actual http talking

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, domain_base, version, ssl_port = 443, public_port = 80) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(key, domain_base, version, ssl_port = 443, public_port = 80)
  @domain_base = domain_base
  @version = version
  @domain = "#{@domain_base}/v#{version}"
  @key = key
  @ssl_port = ssl_port
  @public_port = public_port
  begin # pass yaml file
    key = YAML.load_file(key)
    @key = key[:key] || key["key"]
  rescue # pass key
    @key = key
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object

get, post, put and delete methods



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/crowdflower/base.rb', line 56

def method_missing(method_id, *args)
  if [:get, :post, :put, :delete].include?(method_id)
    path, options = *args
    options ||= {}
    options[:query] = (default_params.merge(options[:query] || {}))
    options[:headers] = (self.class.default_options[:headers].merge(options[:headers] || {}))
    self.class.send(method_id, url(path), options)
  else
    super
  end
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



38
39
40
# File 'lib/crowdflower/base.rb', line 38

def domain
  @domain
end

#domain_baseObject (readonly)

Returns the value of attribute domain_base.



38
39
40
# File 'lib/crowdflower/base.rb', line 38

def domain_base
  @domain_base
end

#keyObject (readonly)

Returns the value of attribute key.



38
39
40
# File 'lib/crowdflower/base.rb', line 38

def key
  @key
end

#public_portObject (readonly)

Returns the value of attribute public_port.



38
39
40
# File 'lib/crowdflower/base.rb', line 38

def public_port
  @public_port
end

#ssl_portObject (readonly)

Returns the value of attribute ssl_port.



38
39
40
# File 'lib/crowdflower/base.rb', line 38

def ssl_port
  @ssl_port
end

#versionObject (readonly)

Returns the value of attribute version.



38
39
40
# File 'lib/crowdflower/base.rb', line 38

def version
  @version
end

Instance Method Details

#crowdflower_baseString

Returns the base crowdflower domain from the api url’s domain.

Examples:

CrowdFlower::Connection.new("asdf", "https://api.crowdflower.com").crowdflower_base #=> "crowdflower.com"

Returns:

  • (String)

    the base crowdflower domain



74
75
76
77
# File 'lib/crowdflower/base.rb', line 74

def crowdflower_base
  uri = URI.parse(domain_base)
  "#{uri.host.gsub("api.", "")}"
end

#public_urlString

Returns the url to reach crowdflower regularly through a browser

Examples:

CrowdFlower::Connection.new("asdf", "https://api.crowdflower.com").public_url #=> "crowdflower.com:80"

Returns:

  • (String)

    the url to reach crowdflower in a browser



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

def public_url
  "#{crowdflower_base}:#{public_port}"
end